Skip to content

Commit d3ac1d6

Browse files
committed
Attempt to fix flickering text
1 parent 3133168 commit d3ac1d6

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

internal/completion/engine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ func (e *Engine) Matches() int {
288288
return comps
289289
}
290290

291+
// DisplaySkipped reports whether completion display is suppressed.
292+
func (e *Engine) DisplaySkipped() bool {
293+
return e.skipDisplay
294+
}
295+
296+
// ResetUsedRows clears the cached displayed row count.
297+
func (e *Engine) ResetUsedRows() {
298+
e.usedY = 0
299+
}
300+
291301
// Line returns the relevant input line at the time this function is called:
292302
// if a candidate is currently selected, the line returned is the one containing
293303
// the candidate. If no candidate is selected, the normal input line is returned.

internal/display/engine.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ func (e *Engine) Refresh() {
9292

9393
// Display hints and completions, go back
9494
// to the start of the line, then to cursor.
95-
e.displayHelpers()
96-
e.cursorHintToLineStart()
97-
e.lineStartToCursorPos()
95+
helpersMoved := e.displayHelpers()
96+
if helpersMoved {
97+
e.cursorHintToLineStart()
98+
e.lineStartToCursorPos()
99+
} else {
100+
e.lineEndToCursorPos()
101+
}
98102
fmt.Print(term.ShowCursor)
99103
}
100104

@@ -294,20 +298,33 @@ func (e *Engine) displayMultilinePrompts() {
294298
// displayHelpers renders the hint and completion sections.
295299
// It assumes that the cursor is on the last line of input,
296300
// and goes back to this same line after displaying this.
297-
func (e *Engine) displayHelpers() {
298-
fmt.Print(term.NewlineReturn)
299-
301+
func (e *Engine) displayHelpers() bool {
300302
// Recompute completions and hints if autocompletion is on.
301303
e.completer.Autocomplete()
302304

305+
hintRows := ui.CoordinatesHint(e.hint)
306+
compMatches := e.completer.Matches()
307+
compSkip := e.completer.DisplaySkipped()
308+
309+
if e.hintRows == 0 && e.compRows == 0 && hintRows == 0 && (compMatches == 0 || compSkip) {
310+
return false
311+
}
312+
313+
fmt.Print(term.NewlineReturn)
314+
303315
prevHintRows := e.hintRows
304316
prevCompRows := e.compRows
305317

306318
// Display hint and completions.
307319
ui.DisplayHint(e.hint)
308320
e.hintRows = ui.CoordinatesHint(e.hint)
309-
completion.Display(e.completer, e.AvailableHelperLines())
310-
e.compRows = completion.Coordinates(e.completer)
321+
if compMatches > 0 && !compSkip {
322+
completion.Display(e.completer, e.AvailableHelperLines())
323+
e.compRows = completion.Coordinates(e.completer)
324+
} else {
325+
e.completer.ResetUsedRows()
326+
e.compRows = 0
327+
}
311328

312329
if e.hintRows+e.compRows < prevHintRows+prevCompRows {
313330
fmt.Print(term.ClearScreenBelow)
@@ -317,6 +334,19 @@ func (e *Engine) displayHelpers() {
317334
term.MoveCursorBackwards(term.GetWidth())
318335
term.MoveCursorUp(e.compRows)
319336
term.MoveCursorUp(e.hintRows)
337+
338+
return true
339+
}
340+
341+
// lineEndToCursorPos moves the cursor from the end of the input line
342+
// to the current cursor position.
343+
func (e *Engine) lineEndToCursorPos() {
344+
if e.lineRows > e.cursorRow {
345+
term.MoveCursorUp(e.lineRows - e.cursorRow)
346+
}
347+
348+
term.MoveCursorBackwards(term.GetWidth())
349+
term.MoveCursorForwards(e.cursorCol)
320350
}
321351

322352
// AvailableHelperLines returns the number of lines available below the hint section.

0 commit comments

Comments
 (0)