Skip to content

Commit

Permalink
feat: add option to pass colours to be used (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Rumney committed Mar 29, 2023
1 parent b3f8797 commit 4552d45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion prompt/list.go
Expand Up @@ -14,9 +14,12 @@ var ErrUserCancelled = fmt.Errorf("User cancelled")
var ErrUserChoiceInvalid = fmt.Errorf("User choice invalid")

// ChooseFromList prompts the user to select an item from a list. It return the chosen list index, the chosen list item value, or an error
func ChooseFromList(message string, options []string) (int, string, error) {
func ChooseFromList(message string, options []string, colourOverrides ...string) (int, string, error) {
fmt.Printf("\n %s\n\n", message)
colours := []string{"lightblue", "lightgreen", "lightyellow", "white"}
if len(colourOverrides) > 0 {
colours = colourOverrides
}

for i, option := range options {
col := colours[i%len(colours)]
Expand Down
8 changes: 6 additions & 2 deletions prompt/multiSelectList.go
Expand Up @@ -2,10 +2,11 @@ package prompt

import (
"fmt"
"sort"

"github.com/liamg/clinch/terminal"
"github.com/liamg/tml"
"github.com/pkg/term"
"sort"
)

const (
Expand Down Expand Up @@ -36,13 +37,16 @@ func (item *listItem) toString() string {
return fmt.Sprintf(" <darkgrey>[</darkgrey>%v<darkgrey>]</darkgrey> <%s>%v\n\r", check, item.colour, item.value)
}

func ChooseFromMultiList(message string, options []string) ([]int, []string, error) {
func ChooseFromMultiList(message string, options []string, colourOverrides ...string) ([]int, []string, error) {
if len(options) == 0 {
return nil, nil, ErrNoOptionsProvided
}
sort.Strings(options)
var items []*listItem
colours := []string{"lightblue", "lightgreen", "lightyellow", "white"}
if len(colourOverrides) > 0 {
colours = colourOverrides
}
for index, option := range options {
col := colours[index%len(colours)]
items = append(items, &listItem{index: index, value: option, colour: col})
Expand Down

0 comments on commit 4552d45

Please sign in to comment.