common.go

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