| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package common
- import (
- "fmt"
- "image"
- "os"
- "github.com/charmbracelet/crush/internal/app"
- "github.com/charmbracelet/crush/internal/config"
- "github.com/charmbracelet/crush/internal/ui/styles"
- 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)
- }
- // 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
- }
|