common.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package common
  2. import (
  3. "fmt"
  4. "image"
  5. "os"
  6. "github.com/charmbracelet/crush/internal/app"
  7. "github.com/charmbracelet/crush/internal/config"
  8. "github.com/charmbracelet/crush/internal/ui/styles"
  9. uv "github.com/charmbracelet/ultraviolet"
  10. )
  11. // MaxAttachmentSize defines the maximum allowed size for file attachments (5 MB).
  12. const MaxAttachmentSize = int64(5 * 1024 * 1024)
  13. // AllowedImageTypes defines the permitted image file types.
  14. var AllowedImageTypes = []string{".jpg", ".jpeg", ".png"}
  15. // Common defines common UI options and configurations.
  16. type Common struct {
  17. App *app.App
  18. Styles *styles.Styles
  19. }
  20. // Config returns the configuration associated with this [Common] instance.
  21. func (c *Common) Config() *config.Config {
  22. return c.App.Config()
  23. }
  24. // DefaultCommon returns the default common UI configurations.
  25. func DefaultCommon(app *app.App) *Common {
  26. s := styles.DefaultStyles()
  27. return &Common{
  28. App: app,
  29. Styles: &s,
  30. }
  31. }
  32. // CenterRect returns a new [Rectangle] centered within the given area with the
  33. // specified width and height.
  34. func CenterRect(area uv.Rectangle, width, height int) uv.Rectangle {
  35. centerX := area.Min.X + area.Dx()/2
  36. centerY := area.Min.Y + area.Dy()/2
  37. minX := centerX - width/2
  38. minY := centerY - height/2
  39. maxX := minX + width
  40. maxY := minY + height
  41. return image.Rect(minX, minY, maxX, maxY)
  42. }
  43. // IsFileTooBig checks if the file at the given path exceeds the specified size
  44. // limit.
  45. func IsFileTooBig(filePath string, sizeLimit int64) (bool, error) {
  46. fileInfo, err := os.Stat(filePath)
  47. if err != nil {
  48. return false, fmt.Errorf("error getting file info: %w", err)
  49. }
  50. if fileInfo.Size() > sizeLimit {
  51. return true, nil
  52. }
  53. return false, nil
  54. }