on main help sign in
refactor(ui): pass app.App to common.Common and access config via common
ba9e452Ayman Bagabas
internal/cmd/root.go+2−2
@@ -87,8 +87,8 @@ crush -y
87
87
88 // Set up the TUI.
88 // Set up the TUI.
89 var env uv.Environ = os.Environ()
89 var env uv.Environ = os.Environ()
90− com := common.DefaultCommon(app.Config())
90+ com := common.DefaultCommon(app)
91− ui := ui.New(com, app)
91+ ui := ui.New(com)
92 ui.QueryVersion = shouldQueryTerminalVersion(env)
92 ui.QueryVersion = shouldQueryTerminalVersion(env)
93 program := tea.NewProgram(
93 program := tea.NewProgram(
94 ui,
94 ui,
internal/ui/common/common.go+9−3
@@ -3,6 +3,7 @@ package common
3import (
3import (
4 "image"
4 "image"
5
5
6+ "github.com/charmbracelet/crush/internal/app"
6 "github.com/charmbracelet/crush/internal/config"
7 "github.com/charmbracelet/crush/internal/config"
7 "github.com/charmbracelet/crush/internal/ui/styles"
8 "github.com/charmbracelet/crush/internal/ui/styles"
8 uv "github.com/charmbracelet/ultraviolet"
9 uv "github.com/charmbracelet/ultraviolet"
@@ -10,15 +11,20 @@ import (
10
11
11// Common defines common UI options and configurations.
12// Common defines common UI options and configurations.
12type Common struct {
13type Common struct {
13− Config *config.Config
14+ App *app.App
14 Styles *styles.Styles
15 Styles *styles.Styles
15}
16}
16
17
18+// Config returns the configuration associated with this [Common] instance.
19+func (c *Common) Config() *config.Config {
20+ return c.App.Config()
21+}
22+
17// DefaultCommon returns the default common UI configurations.
23// DefaultCommon returns the default common UI configurations.
18−func DefaultCommon(cfg *config.Config) *Common {
24+func DefaultCommon(app *app.App) *Common {
19 s := styles.DefaultStyles()
25 s := styles.DefaultStyles()
20 return &Common{
26 return &Common{
21− Config: cfg,
27+ App: app,
22 Styles: &s,
28 Styles: &s,
23 }
29 }
24}
30}
internal/ui/model/editor.go+4−7
@@ -6,7 +6,6 @@ import (
6 "charm.land/bubbles/v2/key"
6 "charm.land/bubbles/v2/key"
7 "charm.land/bubbles/v2/textarea"
7 "charm.land/bubbles/v2/textarea"
8 tea "charm.land/bubbletea/v2"
8 tea "charm.land/bubbletea/v2"
9− "github.com/charmbracelet/crush/internal/app"
10 "github.com/charmbracelet/crush/internal/ui/common"
9 "github.com/charmbracelet/crush/internal/ui/common"
11)
10)
12
11
@@ -61,7 +60,6 @@ func DefaultEditorKeyMap() EditorKeyMap {
61// EditorModel represents the editor UI model.
60// EditorModel represents the editor UI model.
62type EditorModel struct {
61type EditorModel struct {
63 com *common.Common
62 com *common.Common
64− app *app.App
65
63
66 keyMap EditorKeyMap
64 keyMap EditorKeyMap
67 textarea *textarea.Model
65 textarea *textarea.Model
@@ -73,7 +71,7 @@ type EditorModel struct {
73}
71}
74
72
75// NewEditorModel creates a new instance of EditorModel.
73// NewEditorModel creates a new instance of EditorModel.
76−func NewEditorModel(com *common.Common, app *app.App) *EditorModel {
74+func NewEditorModel(com *common.Common) *EditorModel {
77 ta := textarea.New()
75 ta := textarea.New()
78 ta.SetStyles(com.Styles.TextArea)
76 ta.SetStyles(com.Styles.TextArea)
79 ta.ShowLineNumbers = false
77 ta.ShowLineNumbers = false
@@ -82,7 +80,6 @@ func NewEditorModel(com *common.Common, app *app.App) *EditorModel {
82 ta.Focus()
80 ta.Focus()
83 e := &EditorModel{
81 e := &EditorModel{
84 com: com,
82 com: com,
85− app: app,
86 keyMap: DefaultEditorKeyMap(),
83 keyMap: DefaultEditorKeyMap(),
87 textarea: ta,
84 textarea: ta,
88 }
85 }
@@ -108,12 +105,12 @@ func (m *EditorModel) Update(msg tea.Msg) (*EditorModel, tea.Cmd) {
108 cmds = append(cmds, cmd)
105 cmds = append(cmds, cmd)
109
106
110 // Textarea placeholder logic
107 // Textarea placeholder logic
111− if m.app.AgentCoordinator != nil && m.app.AgentCoordinator.IsBusy() {
108+ if m.com.App.AgentCoordinator != nil && m.com.App.AgentCoordinator.IsBusy() {
112 m.textarea.Placeholder = m.workingPlaceholder
109 m.textarea.Placeholder = m.workingPlaceholder
113 } else {
110 } else {
114 m.textarea.Placeholder = m.readyPlaceholder
111 m.textarea.Placeholder = m.readyPlaceholder
115 }
112 }
116− if m.app.Permissions.SkipRequests() {
113+ if m.com.App.Permissions.SkipRequests() {
117 m.textarea.Placeholder = "Yolo mode!"
114 m.textarea.Placeholder = "Yolo mode!"
118 }
115 }
119
116
@@ -183,7 +180,7 @@ func (m *EditorModel) SetSize(width, height int) {
183}
180}
184
181
185func (m *EditorModel) setEditorPrompt() {
182func (m *EditorModel) setEditorPrompt() {
186− if m.app.Permissions.SkipRequests() {
183+ if m.com.App.Permissions.SkipRequests() {
187 m.textarea.SetPromptFunc(4, m.yoloPromptFunc)
184 m.textarea.SetPromptFunc(4, m.yoloPromptFunc)
188 return
185 return
189 }
186 }
internal/ui/model/sidebar.go+1−1
@@ -32,7 +32,7 @@ func NewSidebarModel(com *common.Common) *SidebarModel {
32 return &SidebarModel{
32 return &SidebarModel{
33 com: com,
33 com: com,
34 compact: true,
34 compact: true,
35− cwd: com.Config.WorkingDir(),
35+ cwd: com.Config().WorkingDir(),
36 }
36 }
37}
37}
38
38
internal/ui/model/ui.go+3−6
@@ -10,7 +10,6 @@ import (
10 "charm.land/bubbles/v2/key"
10 "charm.land/bubbles/v2/key"
11 tea "charm.land/bubbletea/v2"
11 tea "charm.land/bubbletea/v2"
12 "charm.land/lipgloss/v2"
12 "charm.land/lipgloss/v2"
13− "github.com/charmbracelet/crush/internal/app"
14 "github.com/charmbracelet/crush/internal/ui/common"
13 "github.com/charmbracelet/crush/internal/ui/common"
15 "github.com/charmbracelet/crush/internal/ui/dialog"
14 "github.com/charmbracelet/crush/internal/ui/dialog"
16 uv "github.com/charmbracelet/ultraviolet"
15 uv "github.com/charmbracelet/ultraviolet"
@@ -27,7 +26,6 @@ const (
27
26
28// UI represents the main user interface model.
27// UI represents the main user interface model.
29type UI struct {
28type UI struct {
30− app *app.App
31 com *common.Common
29 com *common.Common
32
30
33 state uiState
31 state uiState
@@ -52,13 +50,12 @@ type UI struct {
52}
50}
53
51
54// New creates a new instance of the [UI] model.
52// New creates a new instance of the [UI] model.
55−func New(com *common.Common, app *app.App) *UI {
53+func New(com *common.Common) *UI {
56 return &UI{
54 return &UI{
57− app: app,
58 com: com,
55 com: com,
59 dialog: dialog.NewOverlay(),
56 dialog: dialog.NewOverlay(),
60 keyMap: DefaultKeyMap(),
57 keyMap: DefaultKeyMap(),
61− editor: NewEditorModel(com, app),
58+ editor: NewEditorModel(com),
62 side: NewSidebarModel(com),
59 side: NewSidebarModel(com),
63 help: help.New(),
60 help: help.New(),
64 }
61 }
@@ -183,7 +180,7 @@ func (m *UI) View() tea.View {
183 layers = append(layers, mainLayer)
180 layers = append(layers, mainLayer)
184
181
185 v.Content = lipgloss.NewCanvas(layers...)
182 v.Content = lipgloss.NewCanvas(layers...)
186− if m.sendProgressBar && m.app != nil && m.app.AgentCoordinator != nil && m.app.AgentCoordinator.IsBusy() {
183+ if m.sendProgressBar && m.com.App != nil && m.com.App.AgentCoordinator != nil && m.com.App.AgentCoordinator.IsBusy() {
187 // HACK: use a random percentage to prevent ghostty from hiding it
184 // HACK: use a random percentage to prevent ghostty from hiding it
188 // after a timeout.
185 // after a timeout.
189 v.ProgressBar = tea.NewProgressBar(tea.ProgressBarIndeterminate, rand.Intn(100))
186 v.ProgressBar = tea.NewProgressBar(tea.ProgressBarIndeterminate, rand.Intn(100))