common.go

 1package common
 2
 3import (
 4	"image"
 5
 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
12// Common defines common UI options and configurations.
13type Common struct {
14	App    *app.App
15	Styles *styles.Styles
16}
17
18// Config returns the configuration associated with this [Common] instance.
19func (c *Common) Config() *config.Config {
20	return c.App.Config()
21}
22
23// DefaultCommon returns the default common UI configurations.
24func DefaultCommon(app *app.App) *Common {
25	s := styles.DefaultStyles()
26	return &Common{
27		App:    app,
28		Styles: &s,
29	}
30}
31
32// CenterRect returns a new [Rectangle] centered within the given area with the
33// specified width and height.
34func 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}