Skip to content

Commit c6a83e5

Browse files
authored
Merge pull request #93 from reeflective/display-refactor
Refactoring and fixing display issues
2 parents f819680 + 66a5446 commit c6a83e5

File tree

16 files changed

+339
-150
lines changed

16 files changed

+339
-150
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gemini

inputrc/inputrc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func buildOpts(t *testing.T, buf []byte) []Option {
207207
lines := bytes.Split(bytes.TrimSpace(buf), []byte{'\n'})
208208
var opts []Option
209209

210-
for i := 0; i < len(lines); i++ {
210+
for i := range lines {
211211
line := bytes.TrimSpace(lines[i])
212212
// If the line is empty, keep going
213213
if len(line) == 0 {
@@ -406,4 +406,4 @@ func readTestdata(name string) ([]byte, error) {
406406
}
407407

408408
//go:embed testdata/*.inputrc
409-
var testdata embed.FS
409+
var testdata embed.FS

inputrc/parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,11 @@ func findStringEnd(seq []rune, pos, end int) (int, bool) {
507507
quote := seq[pos]
508508

509509
for pos++; pos < end; pos++ {
510-
switch char = seq[pos]; {
511-
case char == '\\':
510+
switch char = seq[pos]; char {
511+
case '\\':
512512
pos++
513513
continue
514-
case char == quote:
514+
case quote:
515515
return pos + 1, true
516516
}
517517
}

internal/core/cursor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ func (c *Cursor) LineMove(lines int) {
247247
}
248248

249249
if lines < 0 {
250-
for i := 0; i < -1*lines; i++ {
250+
for i := 0; i < -lines; i++ {
251251
c.moveLineUp()
252252
c.CheckCommand()
253253
}
254254
} else {
255-
for i := 0; i < lines; i++ {
255+
for range lines {
256256
c.moveLineDown()
257257
c.CheckCommand()
258258
}
@@ -287,7 +287,7 @@ func (c *Cursor) AtBeginningOfLine() bool {
287287

288288
newlines := c.line.newlines()
289289

290-
for line := 0; line < len(newlines); line++ {
290+
for line := range newlines {
291291
epos := newlines[line][0]
292292
if epos == c.pos-1 {
293293
return true
@@ -306,7 +306,7 @@ func (c *Cursor) AtEndOfLine() bool {
306306

307307
newlines := c.line.newlines()
308308

309-
for line := 0; line < len(newlines); line++ {
309+
for line := range newlines {
310310
epos := newlines[line][0]
311311
if epos == c.pos+1 {
312312
return true
@@ -394,7 +394,7 @@ func (c *Cursor) moveLineDown() {
394394

395395
newlines := c.line.newlines()
396396

397-
for line := 0; line < len(newlines); line++ {
397+
for line := range newlines {
398398
end := newlines[line][0]
399399
if line < c.LinePos() {
400400
begin = end

internal/core/keys.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"regexp"
88
"sync"
99

10+
"github.com/rivo/uniseg"
11+
1012
"github.com/reeflective/readline/inputrc"
1113
"github.com/reeflective/readline/internal/strutil"
12-
"github.com/rivo/uniseg"
1314
)
1415

1516
const (

internal/core/selection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ func TestSelection_SelectKeyword(t *testing.T) {
12591259
var gotKbpos, gotKepos int
12601260
var gotMatch bool
12611261

1262-
for i := 0; i < test.args.cycles; i++ {
1262+
for range test.args.cycles {
12631263
gotKbpos, gotKepos, gotMatch = sel.SelectKeyword(test.args.bpos, test.args.epos, test.args.next)
12641264
}
12651265

internal/display/engine.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,6 @@ func Init(e *Engine, highlighter func([]rune) string) {
6666
e.highlighter = highlighter
6767
}
6868

69-
// Refresh recomputes and redisplays the entire readline interface, except
70-
// the first lines of the primary prompt when the latter is a multiline one.
71-
func (e *Engine) Refresh() {
72-
fmt.Print(term.HideCursor)
73-
74-
// Go back to the first column, and if the primary prompt
75-
// was not printed yet, back up to the line's beginning row.
76-
term.MoveCursorBackwards(term.GetWidth())
77-
78-
if !e.primaryPrinted {
79-
term.MoveCursorUp(e.cursorRow)
80-
}
81-
82-
// Print either all or the last line of the prompt.
83-
e.prompt.LastPrint()
84-
85-
// Get all positions required for the redisplay to come:
86-
// prompt end (thus indentation), cursor positions, etc.
87-
e.computeCoordinates(true)
88-
89-
// Print the line, and any of the secondary and right prompts.
90-
e.displayLine()
91-
e.displayMultilinePrompts()
92-
93-
// Display hints and completions, go back
94-
// to the start of the line, then to cursor.
95-
helpersMoved := e.displayHelpers()
96-
if helpersMoved {
97-
e.cursorHintToLineStart()
98-
e.lineStartToCursorPos()
99-
} else {
100-
e.lineEndToCursorPos()
101-
}
102-
fmt.Print(term.ShowCursor)
103-
}
104-
10569
// PrintPrimaryPrompt redraws the primary prompt.
10670
// There are relatively few cases where you want to use this.
10771
// It is currently only used when using clear-screen commands.
@@ -274,70 +238,6 @@ func (e *Engine) displayLine() {
274238
}
275239
}
276240

277-
func (e *Engine) displayMultilinePrompts() {
278-
// If we have more than one line, write the columns.
279-
if e.line.Lines() > 1 {
280-
term.MoveCursorUp(e.lineRows)
281-
term.MoveCursorBackwards(term.GetWidth())
282-
e.prompt.MultilineColumnPrint()
283-
}
284-
285-
// Then if we have a line at all, rewrite the last column
286-
// character with any secondary prompt available.
287-
if e.line.Lines() > 0 {
288-
term.MoveCursorBackwards(term.GetWidth())
289-
e.prompt.SecondaryPrint()
290-
term.MoveCursorBackwards(term.GetWidth())
291-
term.MoveCursorForwards(e.lineCol)
292-
}
293-
294-
// Then prompt the right-sided prompt if possible.
295-
e.prompt.RightPrint(e.lineCol, true)
296-
}
297-
298-
// displayHelpers renders the hint and completion sections.
299-
// It assumes that the cursor is on the last line of input,
300-
// and goes back to this same line after displaying this.
301-
func (e *Engine) displayHelpers() bool {
302-
// Recompute completions and hints if autocompletion is on.
303-
e.completer.Autocomplete()
304-
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-
318-
// Display hint and completions.
319-
ui.DisplayHint(e.hint)
320-
e.hintRows = ui.CoordinatesHint(e.hint)
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-
}
332-
333-
// Go back to the first line below the input line.
334-
term.MoveCursorBackwards(term.GetWidth())
335-
term.MoveCursorUp(e.compRows)
336-
term.MoveCursorUp(e.hintRows)
337-
338-
return true
339-
}
340-
341241
// lineEndToCursorPos moves the cursor from the end of the input line
342242
// to the current cursor position.
343243
func (e *Engine) lineEndToCursorPos() {

internal/display/highlight.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ func (e *Engine) highlightLine(line []rune, selection core.Selection) string {
2222
var highlighted string
2323

2424
// And apply highlighting before each rune.
25+
var highlightedSb25 strings.Builder
26+
2527
for i, r := range line {
2628
if highlight, found := colors[i]; found {
27-
highlighted += string(highlight)
29+
highlightedSb25.WriteString(string(highlight))
2830
}
2931

30-
highlighted += string(r)
32+
highlightedSb25.WriteRune(r)
3133
}
3234

35+
highlighted += highlightedSb25.String()
36+
3337
// Finally, highlight comments using a regex.
3438
comment := strings.Trim(e.opts.GetString("comment-begin"), "\"")
3539
commentPattern := fmt.Sprintf(`(^|\s)%s.*`, comment)

0 commit comments

Comments
 (0)