on main help sign in
Merge pull request #195 from charmbracelet/charm-327
7816fe5Kujtim Hoxha
internal/tui/components/completions/completions.go+13−1
@@ -40,6 +40,8 @@ type Completions interface {
40 Query() string // Returns the current filter query
40 Query() string // Returns the current filter query
41 KeyMap() KeyMap
41 KeyMap() KeyMap
42 Position() (int, int) // Returns the X and Y position of the completions popup
42 Position() (int, int) // Returns the X and Y position of the completions popup
43+ Width() int
44+ Height() int
43}
45}
44
46
45type completionsCmp struct {
47type completionsCmp struct {
@@ -54,6 +56,8 @@ type completionsCmp struct {
54 query string // The current filter query
56 query string // The current filter query
55}
57}
56
58
59+const maxCompletionsWidth = 80 // Maximum width for the completions popup
60+
57func New() Completions {
61func New() Completions {
58 completionsKeyMap := DefaultKeyMap()
62 completionsKeyMap := DefaultKeyMap()
59 keyMap := list.DefaultKeyMap()
63 keyMap := list.DefaultKeyMap()
@@ -92,7 +96,7 @@ func (c *completionsCmp) Init() tea.Cmd {
92func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
96func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
93 switch msg := msg.(type) {
97 switch msg := msg.(type) {
94 case tea.WindowSizeMsg:
98 case tea.WindowSizeMsg:
95− c.width = min(msg.Width-c.x, 80)
99+ c.width = min(msg.Width-c.x, maxCompletionsWidth)
96 c.height = min(msg.Height-c.y, 15)
100 c.height = min(msg.Height-c.y, 15)
97 return c, nil
101 return c, nil
98 case tea.KeyPressMsg:
102 case tea.KeyPressMsg:
@@ -194,3 +198,11 @@ func (c *completionsCmp) KeyMap() KeyMap {
194func (c *completionsCmp) Position() (int, int) {
198func (c *completionsCmp) Position() (int, int) {
195 return c.x, c.y - c.height
199 return c.x, c.y - c.height
196}
200}
201+
202+func (c *completionsCmp) Width() int {
203+ return c.width
204+}
205+
206+func (c *completionsCmp) Height() int {
207+ return c.height
208+}
internal/tui/tui.go+10
@@ -116,6 +116,16 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
116 case completions.OpenCompletionsMsg, completions.FilterCompletionsMsg, completions.CloseCompletionsMsg:
116 case completions.OpenCompletionsMsg, completions.FilterCompletionsMsg, completions.CloseCompletionsMsg:
117 u, completionCmd := a.completions.Update(msg)
117 u, completionCmd := a.completions.Update(msg)
118 a.completions = u.(completions.Completions)
118 a.completions = u.(completions.Completions)
119+ switch msg := msg.(type) {
120+ case completions.OpenCompletionsMsg:
121+ x, _ := a.completions.Position()
122+ if a.completions.Width()+x >= a.wWidth {
123+ // Adjust X position to fit in the window.
124+ msg.X = a.wWidth - a.completions.Width() - 1
125+ u, completionCmd = a.completions.Update(msg)
126+ a.completions = u.(completions.Completions)
127+ }
128+ }
119 return a, completionCmd
129 return a, completionCmd
120
130
121 // Dialog messages
131 // Dialog messages