From e2dce7682f652b8fc62d2691c48f398db7de346a Mon Sep 17 00:00:00 2001 From: Alireza17224 Date: Sat, 5 Aug 2023 20:59:27 +0330 Subject: [PATCH 1/2] Write default value feature for select --- _examples/confirm/main.go | 2 +- _examples/custom_select/main.go | 1 + _examples/prompt_default/main.go | 1 - _examples/select/main.go | 8 +-- _examples/select_add/main.go | 1 + list/list.go | 1 + prompt.go | 1 - select.go | 87 ++++++++++++++++++++++++++++---- 8 files changed, 87 insertions(+), 15 deletions(-) diff --git a/_examples/confirm/main.go b/_examples/confirm/main.go index fe48cc9..84458ed 100644 --- a/_examples/confirm/main.go +++ b/_examples/confirm/main.go @@ -10,8 +10,8 @@ func main() { prompt := promptui.Prompt{ Label: "Delete Resource", IsConfirm: true, + } - result, err := prompt.Run() if err != nil { diff --git a/_examples/custom_select/main.go b/_examples/custom_select/main.go index c2078bc..7ba52bd 100644 --- a/_examples/custom_select/main.go +++ b/_examples/custom_select/main.go @@ -52,6 +52,7 @@ func main() { Items: peppers, Templates: templates, Size: 4, + Default: 2, Searcher: searcher, } diff --git a/_examples/prompt_default/main.go b/_examples/prompt_default/main.go index 9ee3877..4915d73 100644 --- a/_examples/prompt_default/main.go +++ b/_examples/prompt_default/main.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "os/user" - "github.com/manifoldco/promptui" ) diff --git a/_examples/select/main.go b/_examples/select/main.go index c25aff3..1a23a08 100644 --- a/_examples/select/main.go +++ b/_examples/select/main.go @@ -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() diff --git a/_examples/select_add/main.go b/_examples/select_add/main.go index 87ecce1..a41fc06 100644 --- a/_examples/select_add/main.go +++ b/_examples/select_add/main.go @@ -17,6 +17,7 @@ func main() { Label: "What's your text editor", Items: items, AddLabel: "Other", + Default: 3, } index, result, err = prompt.Run() diff --git a/list/list.go b/list/list.go index c98a39c..2afc8e1 100644 --- a/list/list.go +++ b/list/list.go @@ -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 { diff --git a/prompt.go b/prompt.go index 8e35123..450858c 100644 --- a/prompt.go +++ b/prompt.go @@ -5,7 +5,6 @@ import ( "io" "strings" "text/template" - "github.com/chzyer/readline" "github.com/manifoldco/promptui/screenbuf" ) diff --git a/select.go b/select.go index b58ed97..60cabb3 100644 --- a/select.go +++ b/select.go @@ -2,9 +2,11 @@ package promptui import ( "bytes" + "errors" "fmt" "io" "os" + "reflect" "text/tabwriter" "text/template" @@ -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 @@ -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. @@ -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 @@ -183,11 +196,51 @@ 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) { + + defaultValue(s) + return s.RunCursorAt(s.CursorPos, 0) } @@ -219,15 +272,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) @@ -341,7 +398,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) @@ -490,10 +546,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 @@ -524,8 +585,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) @@ -538,6 +606,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, From 5317b060956bbcf715df41d2262b0f00e5ea6e4e Mon Sep 17 00:00:00 2001 From: Alireza17224 Date: Sat, 5 Aug 2023 21:11:36 +0330 Subject: [PATCH 2/2] validation --- select.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/select.go b/select.go index 60cabb3..b2dda96 100644 --- a/select.go +++ b/select.go @@ -239,7 +239,9 @@ func defaultValueSelectWithAdd(s *SelectWithAdd) bool { // occurred during the select's execution. func (s *Select) Run() (int, string, error) { - defaultValue(s) + if !defaultValue(s) { + return 0 , "" , errors.New("Default index + 1 was not founded") + } return s.RunCursorAt(s.CursorPos, 0) }