From 3651a08c9331569f6a4c0ed7a93c39ad009dccdb Mon Sep 17 00:00:00 2001 From: EPRparadox82 <36447316+EPRparadox82@users.noreply.github.com> Date: Sun, 4 Nov 2018 18:50:55 +0100 Subject: [PATCH] Custom Checklist and MultiChoice (#96) --- actions.go | 14 ++++++++++++++ example/main.go | 4 ++++ ishell.go | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/actions.go b/actions.go index aeea34c..3055e7f 100644 --- a/actions.go +++ b/actions.go @@ -51,6 +51,11 @@ type Actions interface { // SetMultiPrompt sets the prompt string used for multiple lines. The string to be displayed before // the cursor; starting from the second line of input. SetMultiPrompt(prompt string) + // SetMultiChoicePrompt sets the prompt strings used for MultiChoice(). + SetMultiChoicePrompt(prompt, spacer string) + // SetChecklistOptions sets the strings representing the options of Checklist(). + // The generated string depends on SetMultiChoicePrompt() also. + SetChecklistOptions(open, selected string) // ShowPrompt sets whether prompt should show when requesting input for ReadLine and ReadPassword. // Defaults to true. ShowPrompt(show bool) @@ -135,6 +140,15 @@ func (s *shellActionsImpl) SetMultiPrompt(prompt string) { s.reader.multiPrompt = prompt } +func (s *shellActionsImpl) SetMultiChoicePrompt(prompt, spacer string) { + strMultiChoice = prompt + strMultiChoiceSpacer = spacer +} +func (s *shellActionsImpl) SetChecklistOptions(open, selected string) { + strMultiChoiceOpen = open + strMultiChoiceSelect = selected +} + func (s *shellActionsImpl) ShowPrompt(show bool) { s.reader.showPrompt = show s.reader.scanner.SetPrompt(s.reader.rlPrompt()) diff --git a/example/main.go b/example/main.go index 4ab4b4f..37c7f62 100644 --- a/example/main.go +++ b/example/main.go @@ -17,6 +17,10 @@ func main() { // display info. shell.Println("Sample Interactive Shell") + //Consider the unicode characters supported by the users font + //shell.SetMultiChoicePrompt(" >>"," - ") + //shell.SetChecklistOptions("[ ] ","[X] ") + // handle login. shell.AddCmd(&ishell.Cmd{ Name: "login", diff --git a/ishell.go b/ishell.go index 09eea2e..9d03ab3 100644 --- a/ishell.go +++ b/ishell.go @@ -15,6 +15,7 @@ import ( "sync" "time" "unicode" + "unicode/utf8" "github.com/abiosoft/readline" "github.com/fatih/color" @@ -29,6 +30,11 @@ const ( var ( errNoHandler = errors.New("incorrect input, try 'help'") errNoInterruptHandler = errors.New("no interrupt handler") + strMultiChoice = " ❯" + strMultiChoiceWin = " >" + strMultiChoiceSpacer = " " + strMultiChoiceOpen = "⬡ " + strMultiChoiceSelect = "⬢ " ) // Shell is an interactive cli shell. @@ -599,25 +605,25 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu func buildOptionsStrings(options []string, selected []int, index int) []string { var strs []string - symbol := " ❯" + symbol := strMultiChoice if runtime.GOOS == "windows" { - symbol = " >" + symbol = strMultiChoiceWin } for i, opt := range options { - mark := "⬡ " + mark := strMultiChoiceOpen if selected == nil { - mark = " " + mark = strMultiChoiceSpacer } for _, s := range selected { if s == i { - mark = "⬢ " + mark = strMultiChoiceSelect } } if i == index { cyan := color.New(color.FgCyan).Add(color.Bold).SprintFunc() strs = append(strs, cyan(symbol+mark+opt)) } else { - strs = append(strs, " "+mark+opt) + strs = append(strs, strings.Repeat(" ", utf8.RuneCountInString(symbol))+mark+opt) } } return strs