on main help sign in
fix(tui): adjust completions popup to fit within window width

This commit modifies the completions popup to reposition itself if it exceeds the window width and moves it to the right edge of the window.

commit8eb20fa690aac9be69e828063ab741d51c781cf6 authorAyman Bagabas <ayman.bagabas@gmail.com> dateTue 15 Jul 2025, 11:09 (1 year ago) parent9dabfe2
2 files changed+23−1
internal/tui/components/completions/completions.go+13−1 internal/tui/tui.go+10
internal/tui/components/completions/completions.go+13−1
@@ -40,6 +40,8 @@ type Completions interface {
4040 Query() string // Returns the current filter query
4141 KeyMap() KeyMap
4242 Position() (int, int) // Returns the X and Y position of the completions popup
43+ Width() int
44+ Height() int
4345}
4446
4547type completionsCmp struct {
@@ -54,6 +56,8 @@ type completionsCmp struct {
5456 query string // The current filter query
5557}
5658
59+const MaxCompletionsWidth = 80 // Maximum width for the completions popup
60+
5761func New() Completions {
5862 completionsKeyMap := DefaultKeyMap()
5963 keyMap := list.DefaultKeyMap()
@@ -92,7 +96,7 @@ func (c *completionsCmp) Init() tea.Cmd {
9296func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9397 switch msg := msg.(type) {
9498 case tea.WindowSizeMsg:
95− c.width = min(msg.Width-c.x, 80)
99+ c.width = min(msg.Width-c.x, MaxCompletionsWidth)
96100 c.height = min(msg.Height-c.y, 15)
97101 return c, nil
98102 case tea.KeyPressMsg:
@@ -201,3 +205,11 @@ func (c *completionsCmp) KeyMap() KeyMap {
201205func (c *completionsCmp) Position() (int, int) {
202206 return c.x, c.y - c.height
203207}
208+
209+func (c *completionsCmp) Width() int {
210+ return c.width
211+}
212+
213+func (c *completionsCmp) Height() int {
214+ return c.height
215+}
internal/tui/tui.go+10
@@ -116,6 +116,16 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
116116 case completions.OpenCompletionsMsg, completions.FilterCompletionsMsg, completions.CloseCompletionsMsg:
117117 u, completionCmd := a.completions.Update(msg)
118118 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+ }
119129 return a, completionCmd
120130
121131 // Dialog messages