Skip to content

Commit 95e91fa

Browse files
authored
Merge pull request #92 from moloch--/master
Reduce Flickering Text
2 parents f2b0cd1 + d3ac1d6 commit 95e91fa

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

internal/completion/display.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
func Display(eng *Engine, maxRows int) {
1616
eng.usedY = 0
1717

18-
defer fmt.Print(term.ClearScreenBelow)
19-
2018
// The completion engine might be inactive but still having
2119
// a non-empty list of completions. This is on purpose, as
2220
// sometimes it's better to keep completions printed for a

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: 46 additions & 9 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,22 +298,55 @@ 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+
315+
prevHintRows := e.hintRows
316+
prevCompRows := e.compRows
317+
303318
// Display hint and completions.
304319
ui.DisplayHint(e.hint)
305320
e.hintRows = ui.CoordinatesHint(e.hint)
306-
completion.Display(e.completer, e.AvailableHelperLines())
307-
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+
}
328+
329+
if e.hintRows+e.compRows < prevHintRows+prevCompRows {
330+
fmt.Print(term.ClearScreenBelow)
331+
}
308332

309333
// Go back to the first line below the input line.
310334
term.MoveCursorBackwards(term.GetWidth())
311335
term.MoveCursorUp(e.compRows)
312-
term.MoveCursorUp(ui.CoordinatesHint(e.hint))
336+
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)
313350
}
314351

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

0 commit comments

Comments
 (0)