Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write default value feature for select #225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _examples/confirm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func main() {
prompt := promptui.Prompt{
Label: "Delete Resource",
IsConfirm: true,

}

result, err := prompt.Run()

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions _examples/custom_select/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
Items: peppers,
Templates: templates,
Size: 4,
Default: 2,
Searcher: searcher,
}

Expand Down
1 change: 0 additions & 1 deletion _examples/prompt_default/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os/user"

"github.com/manifoldco/promptui"
)

Expand Down
8 changes: 5 additions & 3 deletions _examples/select/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package main

import (
"fmt"

"github.com/manifoldco/promptui"
)

func main() {
items := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"}
// Default count from 1!!
prompt := promptui.Select{
Label: "Select Day",
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"},
Items: items,
Default: 3,
}

_, result, err := prompt.Run()
Expand Down
1 change: 1 addition & 0 deletions _examples/select_add/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
Label: "What's your text editor",
Items: items,
AddLabel: "Other",
Default: 3,
}

index, result, err = prompt.Run()
Expand Down
1 change: 1 addition & 0 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (l *List) CanPageUp() bool {
// Index returns the index of the item currently selected inside the searched list. If no item is selected,
// the NotFound (-1) index is returned.
func (l *List) Index() int {

selected := l.scope[l.cursor]

for i, item := range l.items {
Expand Down
1 change: 0 additions & 1 deletion prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"strings"
"text/template"

"github.com/chzyer/readline"
"github.com/manifoldco/promptui/screenbuf"
)
Expand Down
89 changes: 80 additions & 9 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package promptui

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"reflect"
"text/tabwriter"
"text/template"

Expand All @@ -28,6 +30,10 @@ type Select struct {
// inside the templates. For example, `{{ .Name }}` will display the name property of a struct.
Label interface{}

// the (index + 1) number of item

Default int

// Items are the items to display inside the list. It expect a slice of any kind of values, including strings.
//
// If using a slice of strings, promptui will use those strings directly into its base templates or the
Expand Down Expand Up @@ -116,24 +122,27 @@ type Key struct {
// text/template syntax. Custom state, colors and background color are available for use inside
// the templates and are documented inside the Variable section of the docs.
//
// Examples
// # Examples
//
// text/templates use a special notation to display programmable content. Using the double bracket notation,
// the value can be printed with specific helper functions. For example
//
// This displays the value given to the template as pure, unstylized text. Structs are transformed to string
// with this notation.
// '{{ . }}'
//
// '{{ . }}'
//
// This displays the name property of the value colored in cyan
// '{{ .Name | cyan }}'
//
// '{{ .Name | cyan }}'
//
// This displays the label property of value colored in red with a cyan background-color
// '{{ .Label | red | cyan }}'
//
// '{{ .Label | red | cyan }}'
//
// See the doc of text/template for more info: https://golang.org/pkg/text/template/
//
// Notes
// # Notes
//
// Setting any of these templates will remove the icons from the default templates. They must
// be added back in each of their specific templates. The styles.go constants contains the default icons.
Expand All @@ -145,6 +154,10 @@ type SelectTemplates struct {
// Active is a text/template for when an item is currently active within the list.
Active string

// the (index + 1) number of item

Default int

// Inactive is a text/template for when an item is not currently active inside the list. This
// template is used for all items unless they are active or selected.
Inactive string
Expand Down Expand Up @@ -183,11 +196,53 @@ type SelectTemplates struct {
// SearchPrompt is the prompt displayed in search mode.
var SearchPrompt = "Search: "

// computing the default value for only select

func defaultValue(s *Select) bool {
itemsValue := reflect.ValueOf(s.Items)
if itemsValue.Kind() == reflect.Slice {
if s.Default <= itemsValue.Len() {
var b []interface{}
b = append(b, itemsValue.Index(s.Default-1))
for i := 0; i < itemsValue.Len(); i++ {
if i != (s.Default - 1) {
b = append(b, itemsValue.Index(i))
}
}
s.Items = b
return true
}
}
return false
}

// computing the default value for only select

func defaultValueSelectWithAdd(s *SelectWithAdd) bool {
if s.Default <= len(s.Items) {
var b []string
b = append(b, s.Items[s.Default-1])
for i, v := range s.Items {
if i != (s.Default - 1) {
b = append(b, v)
}
}
s.Items = b
return true
}
return false
}

// Run executes the select list. It displays the label and the list of items, asking the user to chose any
// value within to list. Run will keep the prompt alive until it has been canceled from
// the command prompt or it has received a valid value. It will return the value and an error if any
// occurred during the select's execution.
func (s *Select) Run() (int, string, error) {

if !defaultValue(s) {
return 0 , "" , errors.New("Default index + 1 was not founded")
}

return s.RunCursorAt(s.CursorPos, 0)
}

Expand Down Expand Up @@ -219,15 +274,19 @@ func (s *Select) RunCursorAt(cursorPos, scroll int) (int, string, error) {
return s.innerRun(cursorPos, scroll, ' ')
}

func checkForDefaultValue(s *Select, g *int) bool {
return true
}

func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) {

// Checking for the default value

c := &readline.Config{
Stdin: s.Stdin,
Stdout: s.Stdout,
}
err := c.Init()
if err != nil {
return 0, "", err
}

c.Stdin = readline.NewCancelableStdin(c.Stdin)

Expand Down Expand Up @@ -341,7 +400,6 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
sb.WriteString("No results")
} else {
active := items[idx]

details := s.renderDetails(active)
for _, d := range details {
sb.Write(d)
Expand Down Expand Up @@ -490,10 +548,15 @@ func (s *Select) prepareTemplates() error {
// SelectWithAdd represents a list for selecting a single item inside a list of items with the possibility to
// add new items to the list.
type SelectWithAdd struct {

// Label is the text displayed on top of the list to direct input. The IconInitial value "?" will be
// appended automatically to the label so it does not need to be added.
Label string

// the (index + 1) number of item

Default int

// Items are the items to display inside the list. Each item will be listed individually with the
// AddLabel as the first item of the list.
Items []string
Expand Down Expand Up @@ -524,8 +587,15 @@ type SelectWithAdd struct {
// If the addLabel is selected in the list, this function will return a -1 index with the added label and no error.
// Otherwise, it will return the index and the value of the selected item. In any case, if an error is triggered, it
// will also return the error as its third return value.

func (sa *SelectWithAdd) Run() (int, string, error) {

if !defaultValueSelectWithAdd(sa) {
return 0 , "",errors.New("please check the default value because it not exists")
}

if len(sa.Items) > 0 {

newItems := append([]string{sa.AddLabel}, sa.Items...)

list, err := list.New(newItems, 5)
Expand All @@ -538,6 +608,7 @@ func (sa *SelectWithAdd) Run() (int, string, error) {
Items: newItems,
IsVimMode: sa.IsVimMode,
HideHelp: sa.HideHelp,
Default: sa.Default,
Size: 5,
list: list,
Pointer: sa.Pointer,
Expand Down