Skip to content

Commit 1e7e6f7

Browse files
authored
Merge pull request #82 from reeflective/dev
dev
2 parents b9747bd + 4d14d00 commit 1e7e6f7

7 files changed

Lines changed: 51 additions & 47 deletions

File tree

completions.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ type Completions struct {
4040
func CompleteValues(values ...string) Completions {
4141
vals := make([]Completion, 0, len(values))
4242
for _, val := range values {
43-
vals = append(vals, Completion{Value: val, Display: val, Description: ""})
43+
vals = append(vals, Completion{
44+
Value: val,
45+
Display: val,
46+
Description: "",
47+
})
4448
}
4549

4650
return Completions{values: vals}
@@ -54,7 +58,12 @@ func CompleteStyledValues(values ...string) Completions {
5458

5559
vals := make([]Completion, 0, len(values)/2)
5660
for i := 0; i < len(values); i += 2 {
57-
vals = append(vals, Completion{Value: values[i], Display: values[i], Description: "", Style: values[i+1]})
61+
vals = append(vals, Completion{
62+
Value: values[i],
63+
Display: values[i],
64+
Description: "",
65+
Style: values[i+1],
66+
})
5867
}
5968

6069
return Completions{values: vals}
@@ -68,7 +77,11 @@ func CompleteValuesDescribed(values ...string) Completions {
6877

6978
vals := make([]Completion, 0, len(values)/2)
7079
for i := 0; i < len(values); i += 2 {
71-
vals = append(vals, Completion{Value: values[i], Display: values[i], Description: values[i+1]})
80+
vals = append(vals, Completion{
81+
Value: values[i],
82+
Display: values[i],
83+
Description: values[i+1],
84+
})
7285
}
7386

7487
return Completions{values: vals}
@@ -82,7 +95,12 @@ func CompleteStyledValuesDescribed(values ...string) Completions {
8295

8396
vals := make([]Completion, 0, len(values)/3)
8497
for i := 0; i < len(values); i += 3 {
85-
vals = append(vals, Completion{Value: values[i], Display: values[i], Description: values[i+1], Style: values[i+2]})
98+
vals = append(vals, Completion{
99+
Value: values[i],
100+
Display: values[i],
101+
Description: values[i+1],
102+
Style: values[i+2],
103+
})
86104
}
87105

88106
return Completions{values: vals}

internal/completion/display.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func (e *Engine) highlightDisplay(grp *group, val Candidate, pad, col int, selec
108108
return padSpace(pad)
109109
}
110110

111-
reset := color.Fmt(val.Style)
111+
style := color.Fmt(val.Style)
112112
candidate, padded := grp.trimDisplay(val, pad, col)
113113

114114
if e.IsearchRegex != nil && e.isearchBuf.Len() > 0 && !selected {
115115
match := e.IsearchRegex.FindString(candidate)
116-
match = color.Fmt(color.Bg+"244") + match + color.Reset + reset
116+
match = color.Fmt(color.Bg+"244") + match + color.Reset + style
117117
candidate = e.IsearchRegex.ReplaceAllLiteralString(candidate, match)
118118
}
119119

@@ -130,12 +130,12 @@ func (e *Engine) highlightDisplay(grp *group, val Candidate, pad, col int, selec
130130
// Highlight the prefix if any and configured for it.
131131
if e.config.GetBool("colored-completion-prefix") && e.prefix != "" {
132132
if prefixMatch, err := regexp.Compile("^" + e.prefix); err == nil {
133-
prefixColored := color.Bold + color.FgBlue + e.prefix + color.BoldReset + color.FgDefault + reset
133+
prefixColored := color.Bold + color.FgBlue + e.prefix + color.BoldReset + color.FgDefault + style
134134
candidate = prefixMatch.ReplaceAllString(candidate, prefixColored)
135135
}
136136
}
137137

138-
candidate = reset + candidate + color.Reset
138+
candidate = style + candidate + color.Reset
139139
}
140140

141141
return candidate + padded

internal/completion/group.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ func (g *group) initCompletionsGrid(comps RawValues) {
124124
}
125125

126126
pairLength := g.longestValueDescribed(comps)
127-
if pairLength > g.termWidth {
128-
pairLength = g.termWidth
129-
}
127+
pairLength = min(g.termWidth, pairLength)
130128

131129
maxColumns := g.termWidth / pairLength
132130
if g.list || maxColumns < 0 {
@@ -144,9 +142,9 @@ func (g *group) initCompletionAliased(domains []Candidate) {
144142
g.aliased = true
145143

146144
// Filter out all duplicates: group aliased completions together.
147-
grid, descriptions := g.createDescribedRows(domains)
145+
grid, _ := g.createDescribedRows(domains)
148146
g.calculateMaxColumnWidths(grid)
149-
g.wrapExcessAliases(grid, descriptions)
147+
g.wrapExcessAliases(grid)
150148

151149
g.maxY = len(g.rows)
152150
g.maxX = len(g.columnsWidth)
@@ -172,16 +170,14 @@ func (g *group) createDescribedRows(values []Candidate) ([][]Candidate, []string
172170
// Sorting helps with easier grids.
173171
for _, description := range uniqueDescriptions {
174172
row := descriptionMap[description]
175-
// slices.Sort(row)
176-
// slices.Reverse(row)
177173
rows = append(rows, row)
178174
}
179175

180176
return rows, uniqueDescriptions
181177
}
182178

183179
// Wraps all rows for which there are too many aliases to be displayed on a single one.
184-
func (g *group) wrapExcessAliases(grid [][]Candidate, descriptions []string) {
180+
func (g *group) wrapExcessAliases(grid [][]Candidate) {
185181
breakeven := 0
186182
maxColumns := len(g.columnsWidth)
187183

@@ -356,10 +352,7 @@ func (g *group) trimDisplay(comp Candidate, pad, col int) (candidate, padded str
356352
// Get the allowed length for this column.
357353
// The display can never be longer than terminal width.
358354
maxDisplayWidth := g.columnsWidth[col] + 1
359-
360-
if maxDisplayWidth > g.termWidth {
361-
maxDisplayWidth = g.termWidth
362-
}
355+
maxDisplayWidth = min(g.termWidth, maxDisplayWidth)
363356

364357
val = sanitizer.Replace(val)
365358

@@ -417,10 +410,7 @@ func (g *group) getPad(value Candidate, columnIndex int, desc bool) int {
417410
// to the terminal size: we are not really sure
418411
// of where offsets might be set in the code...
419412
column := columns[columnIndex]
420-
if column > g.termWidth-1 {
421-
column = g.termWidth - 1
422-
}
423-
413+
column = min(g.termWidth-1, column)
424414
padding := column - valLen
425415

426416
if padding < 0 {
@@ -641,7 +631,7 @@ func createGrid(values []Candidate, rowCount, maxColumns int) [][]Candidate {
641631

642632
grid := make([][]Candidate, rowCount)
643633

644-
for i := 0; i < rowCount; i++ {
634+
for i := range rowCount {
645635
grid[i] = createRow(values, maxColumns, i)
646636
}
647637

@@ -651,10 +641,7 @@ func createGrid(values []Candidate, rowCount, maxColumns int) [][]Candidate {
651641
func createRow(domains []Candidate, maxColumns, rowIndex int) []Candidate {
652642
rowStart := rowIndex * maxColumns
653643
rowEnd := (rowIndex + 1) * maxColumns
654-
655-
if rowEnd > len(domains) {
656-
rowEnd = len(domains)
657-
}
644+
rowEnd = min(len(domains), rowEnd)
658645

659646
return domains[rowStart:rowEnd]
660647
}

internal/keymap/completion.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package keymap
22

3-
import "github.com/reeflective/readline/inputrc"
3+
import (
4+
"slices"
5+
6+
"github.com/reeflective/readline/inputrc"
7+
)
48

59
// menuselectKeys are the default keymaps in menuselect mode.
610
var menuselectKeys = map[string]inputrc.Bind{
@@ -115,11 +119,5 @@ func (m *Engine) restrictCommands(mode Mode, commands []string) map[string]input
115119
}
116120

117121
func isValidCommand(widget string, commands []string) bool {
118-
for _, isw := range commands {
119-
if isw == widget {
120-
return true
121-
}
122-
}
123-
124-
return false
122+
return slices.Contains(commands, widget)
125123
}

internal/keymap/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func printBindsReadable(commands []string, all map[string][]string) {
145145
case len(commandBinds) > 5:
146146
var firstBinds []string
147147

148-
for i := 0; i < 5; i++ {
148+
for i := range 5 {
149149
firstBinds = append(firstBinds, "\""+commandBinds[i]+"\"")
150150
}
151151

internal/keymap/dispatch.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ func MatchMain(eng *Engine) (bind inputrc.Bind, command func(), prefix bool) {
9696
return bind, command, prefix
9797
}
9898

99-
func (m *Engine) makeMatch(active, prefixed inputrc.Bind) (prefix bool) {
100-
m.active = active
101-
m.prefixed = prefixed
102-
return m.prefixed.Action != ""
103-
}
104-
10599
func (m *Engine) dispatchKeys(binds map[string]inputrc.Bind) (bind inputrc.Bind, prefix bool, read, matched []byte) {
106100
// Support for Unicode: if the character is multi-byte (UTF-8), consume all its bytes
107101
// and treat it as a single self-insert action. Note that we just peek the characters
@@ -270,6 +264,13 @@ func (m *Engine) handleEscape(main bool) (bind inputrc.Bind, cmd func(), pref bo
270264
return bind, cmd, pref
271265
}
272266

267+
// makeMatch populates currently used binds.
268+
func (m *Engine) makeMatch(active, prefixed inputrc.Bind) (prefix bool) {
269+
m.active = active
270+
m.prefixed = prefixed
271+
return m.prefixed.Action != ""
272+
}
273+
273274
func (m *Engine) isEscapeKey() bool {
274275
keys := m.keys.Caller()
275276
if len(keys) == 0 {

internal/ui/prompt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,23 @@ func (p *Prompt) MultilineColumnPrint() {
205205
switch {
206206
case numbered:
207207
column := ""
208-
for pos := 0; pos < p.line.Lines(); pos++ {
208+
for pos := range p.line.Lines() {
209209
column += fmt.Sprintf("\n\x1b[1;30m%d\x1b[0m", pos+2)
210210
}
211211

212212
fmt.Print(column)
213213

214214
case len(custom) > 0:
215215
column := ""
216-
for pos := 0; pos < p.line.Lines(); pos++ {
216+
for range p.line.Lines() {
217217
column += fmt.Sprintf("\n%s\x1b[0m", custom)
218218
}
219219

220220
fmt.Print(column)
221221

222222
case defaultCol:
223223
column := ""
224-
for pos := 0; pos < p.line.Lines(); pos++ {
224+
for range p.line.Lines() {
225225
column += "\n" + multilineColumnDefault
226226
}
227227

0 commit comments

Comments
 (0)