Skip to content

Commit 3651a08

Browse files
EPRparadox82abiosoft
authored andcommitted
Custom Checklist and MultiChoice (#96)
1 parent 2ef49f1 commit 3651a08

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

actions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ type Actions interface {
5151
// SetMultiPrompt sets the prompt string used for multiple lines. The string to be displayed before
5252
// the cursor; starting from the second line of input.
5353
SetMultiPrompt(prompt string)
54+
// SetMultiChoicePrompt sets the prompt strings used for MultiChoice().
55+
SetMultiChoicePrompt(prompt, spacer string)
56+
// SetChecklistOptions sets the strings representing the options of Checklist().
57+
// The generated string depends on SetMultiChoicePrompt() also.
58+
SetChecklistOptions(open, selected string)
5459
// ShowPrompt sets whether prompt should show when requesting input for ReadLine and ReadPassword.
5560
// Defaults to true.
5661
ShowPrompt(show bool)
@@ -135,6 +140,15 @@ func (s *shellActionsImpl) SetMultiPrompt(prompt string) {
135140
s.reader.multiPrompt = prompt
136141
}
137142

143+
func (s *shellActionsImpl) SetMultiChoicePrompt(prompt, spacer string) {
144+
strMultiChoice = prompt
145+
strMultiChoiceSpacer = spacer
146+
}
147+
func (s *shellActionsImpl) SetChecklistOptions(open, selected string) {
148+
strMultiChoiceOpen = open
149+
strMultiChoiceSelect = selected
150+
}
151+
138152
func (s *shellActionsImpl) ShowPrompt(show bool) {
139153
s.reader.showPrompt = show
140154
s.reader.scanner.SetPrompt(s.reader.rlPrompt())

example/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func main() {
1717
// display info.
1818
shell.Println("Sample Interactive Shell")
1919

20+
//Consider the unicode characters supported by the users font
21+
//shell.SetMultiChoicePrompt(" >>"," - ")
22+
//shell.SetChecklistOptions("[ ] ","[X] ")
23+
2024
// handle login.
2125
shell.AddCmd(&ishell.Cmd{
2226
Name: "login",

ishell.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sync"
1616
"time"
1717
"unicode"
18+
"unicode/utf8"
1819

1920
"github.com/abiosoft/readline"
2021
"github.com/fatih/color"
@@ -29,6 +30,11 @@ const (
2930
var (
3031
errNoHandler = errors.New("incorrect input, try 'help'")
3132
errNoInterruptHandler = errors.New("no interrupt handler")
33+
strMultiChoice = " ❯"
34+
strMultiChoiceWin = " >"
35+
strMultiChoiceSpacer = " "
36+
strMultiChoiceOpen = "⬡ "
37+
strMultiChoiceSelect = "⬢ "
3238
)
3339

3440
// Shell is an interactive cli shell.
@@ -599,25 +605,25 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
599605

600606
func buildOptionsStrings(options []string, selected []int, index int) []string {
601607
var strs []string
602-
symbol := " ❯"
608+
symbol := strMultiChoice
603609
if runtime.GOOS == "windows" {
604-
symbol = " >"
610+
symbol = strMultiChoiceWin
605611
}
606612
for i, opt := range options {
607-
mark := "⬡ "
613+
mark := strMultiChoiceOpen
608614
if selected == nil {
609-
mark = " "
615+
mark = strMultiChoiceSpacer
610616
}
611617
for _, s := range selected {
612618
if s == i {
613-
mark = "⬢ "
619+
mark = strMultiChoiceSelect
614620
}
615621
}
616622
if i == index {
617623
cyan := color.New(color.FgCyan).Add(color.Bold).SprintFunc()
618624
strs = append(strs, cyan(symbol+mark+opt))
619625
} else {
620-
strs = append(strs, " "+mark+opt)
626+
strs = append(strs, strings.Repeat(" ", utf8.RuneCountInString(symbol))+mark+opt)
621627
}
622628
}
623629
return strs

0 commit comments

Comments
 (0)