Skip to content

Commit 23d8605

Browse files
committed
added support to show instructions to users + bugfix where <TAB> couldn't start selection at empty prompt
1 parent 8712f8e commit 23d8605

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

callbacks.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ type ClosestMatch struct {
2222
OnSelect func(string)
2323
MaxShown int
2424

25-
// default: FgWhite (aka nothing)
26-
MatchColor *color.Color
25+
// default: "Use <TAB> and <ENTER> to select from below. Otherwise press <ENTER> when ready"
26+
Instructions string
27+
ShowInstructions bool
28+
2729
// default: FgBlue
2830
SelectedColor *color.Color
31+
// default: FgHiBlack
32+
InstructionColor *color.Color
2933
}
3034

3135
// CB returns a configured callback to use with Prompt
@@ -34,12 +38,15 @@ func (c *ClosestMatch) CB() Callback {
3438
// later
3539
delim := "::CBDELIM::"
3640

37-
if c.MatchColor == nil {
38-
c.MatchColor = color.New(color.FgWhite)
39-
}
4041
if c.SelectedColor == nil {
4142
c.SelectedColor = color.New(color.FgBlue)
4243
}
44+
if c.InstructionColor == nil {
45+
c.InstructionColor = color.New(color.FgHiBlack)
46+
}
47+
if c.Instructions == "" {
48+
c.Instructions = "Use <TAB> and <ENTER> to select from below. Otherwise press <ENTER> when ready"
49+
}
4350

4451
var content []string
4552
for k, v := range c.Data {
@@ -85,26 +92,22 @@ func (c *ClosestMatch) CB() Callback {
8592
return c.joinLines(
8693
topN,
8794
preproc,
88-
inp != "", // rank
8995
lastSelected,
9096
)
9197
}
9298
}
9399

94-
func (c *ClosestMatch) joinLines(lines []string, preproc func(string) string, rank bool, selected int) string {
100+
func (c *ClosestMatch) joinLines(lines []string, preproc func(string) string, selected int) string {
95101
var output string
102+
if c.ShowInstructions {
103+
output += c.InstructionColor.Sprintln(c.Instructions)
104+
}
96105
for i, line := range lines {
97106
line = preproc(line)
98-
// If the input isn't ranked, text hasn't been typed. No need to
99-
// color / make note
100-
if !rank {
101-
output += fmt.Sprintf("%s\n", line)
102-
continue
103-
}
104107

105-
// Highlight closest match if nothing has been selected yet
108+
// Nothing has been selected yet
106109
if i == 0 && selected == -1 {
107-
output += c.MatchColor.Sprintf("%s\n", line)
110+
output += fmt.Sprintln(line)
108111
continue
109112
}
110113

@@ -115,7 +118,7 @@ func (c *ClosestMatch) joinLines(lines []string, preproc func(string) string, ra
115118
}
116119

117120
// Plain jane text
118-
output += fmt.Sprintf("%s\n", line)
121+
output += fmt.Sprintln(line)
119122
}
120123
return output
121124
}

examples/closestmatch.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ func main() {
2020
}
2121
var selected string
2222
cb := rtprompt.ClosestMatch{
23-
Data: issues,
24-
OnSelect: func(s string) { selected = s },
25-
MaxShown: 7,
23+
Data: issues,
24+
OnSelect: func(s string) { selected = s },
25+
MaxShown: 7,
26+
ShowInstructions: true,
2627
}
2728
prompt := rtprompt.New("Summary: ", cb.CB())
2829
prompt.Wait()

0 commit comments

Comments
 (0)