| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package common
- import (
- "fmt"
- "image"
- "os"
- tea "charm.land/bubbletea/v2"
- "github.com/atotto/clipboard"
- "github.com/charmbracelet/crush/internal/app"
- "github.com/charmbracelet/crush/internal/config"
- "github.com/charmbracelet/crush/internal/ui/styles"
- "github.com/charmbracelet/crush/internal/ui/util"
- uv "github.com/charmbracelet/ultraviolet"
- )
- // MaxAttachmentSize defines the maximum allowed size for file attachments (5 MB).
- const MaxAttachmentSize = int64(5 * 1024 * 1024)
- // AllowedImageTypes defines the permitted image file types.
- var AllowedImageTypes = []string{".jpg", ".jpeg", ".png"}
- // Common defines common UI options and configurations.
- type Common struct {
- App *app.App
- Styles *styles.Styles
- }
- // Config returns the configuration associated with this [Common] instance.
- func (c *Common) Config() *config.Config {
- return c.App.Config()
- }
- // DefaultCommon returns the default common UI configurations.
- func DefaultCommon(app *app.App) *Common {
- s := styles.DefaultStyles()
- return &Common{
- App: app,
- Styles: &s,
- }
- }
- // CenterRect returns a new [Rectangle] centered within the given area with the
- // specified width and height.
- func CenterRect(area uv.Rectangle, width, height int) uv.Rectangle {
- centerX := area.Min.X + area.Dx()/2
- centerY := area.Min.Y + area.Dy()/2
- minX := centerX - width/2
- minY := centerY - height/2
- maxX := minX + width
- maxY := minY + height
- return image.Rect(minX, minY, maxX, maxY)
- }
- // BottomLeftRect returns a new [Rectangle] positioned at the bottom-left within the given area with the
- // specified width and height.
- func BottomLeftRect(area uv.Rectangle, width, height int) uv.Rectangle {
- minX := area.Min.X
- maxX := minX + width
- maxY := area.Max.Y
- minY := maxY - height
- return image.Rect(minX, minY, maxX, maxY)
- }
- // IsFileTooBig checks if the file at the given path exceeds the specified size
- // limit.
- func IsFileTooBig(filePath string, sizeLimit int64) (bool, error) {
- fileInfo, err := os.Stat(filePath)
- if err != nil {
- return false, fmt.Errorf("error getting file info: %w", err)
- }
- if fileInfo.Size() > sizeLimit {
- return true, nil
- }
- return false, nil
- }
- // CopyToClipboard copies the given text to the clipboard using both OSC 52
- // (terminal escape sequence) and native clipboard for maximum compatibility.
- // Returns a command that reports success to the user with the given message.
- func CopyToClipboard(text, successMessage string) tea.Cmd {
- return CopyToClipboardWithCallback(text, successMessage, nil)
- }
- // CopyToClipboardWithCallback copies text to clipboard and executes a callback
- // before showing the success message.
- // This is useful when you need to perform additional actions like clearing UI state.
- func CopyToClipboardWithCallback(text, successMessage string, callback tea.Cmd) tea.Cmd {
- return tea.Sequence(
- tea.SetClipboard(text),
- func() tea.Msg {
- _ = clipboard.WriteAll(text)
- return nil
- },
- callback,
- util.ReportInfo(successMessage),
- )
- }
|