Skip to content

Commit

Permalink
Custom Checklist and MultiChoice (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
EPRparadox82 authored and abiosoft committed Nov 4, 2018
1 parent 2ef49f1 commit 3651a08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
14 changes: 14 additions & 0 deletions actions.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions example/main.go
Expand Up @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions ishell.go
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"time"
"unicode"
"unicode/utf8"

"github.com/abiosoft/readline"
"github.com/fatih/color"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3651a08

Please sign in to comment.