From 85ba2730f423c3679950b7f23bee235d6539d010 Mon Sep 17 00:00:00 2001 From: Munish Goyal Date: Fri, 15 Apr 2022 09:28:53 +1000 Subject: [PATCH] Use `AlecAivazis/survey` instead of `manifoldco/promptui` (#99) * Install github.com/AlecAivazis/survey/v2 and update dependencies * Attempt to use search functionality from survey * Attempt to use survey.Input * Run 'go mod tidy' to remove promptui --- cmd/reminder/main.go | 3 +- go.mod | 11 ++- go.sum | 53 ++++++++++--- internal/model/functions.go | 51 +++++++++---- internal/model/model_test.go | 16 ++-- internal/model/reminder_data.go | 35 +++------ pkg/utils/functions.go | 127 +++++++++++++++++--------------- 7 files changed, 174 insertions(+), 122 deletions(-) diff --git a/cmd/reminder/main.go b/cmd/reminder/main.go index 35f0581..35cef27 100644 --- a/cmd/reminder/main.go +++ b/cmd/reminder/main.go @@ -7,7 +7,6 @@ package main import ( "fmt" - "time" "reminder/internal/model" "reminder/pkg/utils" @@ -71,6 +70,6 @@ func flow() { } func main() { - go utils.Spinner(100 * time.Millisecond) + // go utils.Spinner(100 * time.Millisecond) flow() } diff --git a/go.mod b/go.mod index fbbf632..5963400 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,14 @@ module reminder go 1.18 -require github.com/manifoldco/promptui v0.9.0 +require github.com/AlecAivazis/survey/v2 v2.3.4 require ( - github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect - golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect + golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect + golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect + golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 34fdb2a..1edc2b6 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,43 @@ -github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= -github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= -golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4= -golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +github.com/AlecAivazis/survey/v2 v2.3.4 h1:pchTU9rsLUSvWEl2Aq9Pv3k0IE2fkqtGxazskAMd9Ng= +github.com/AlecAivazis/survey/v2 v2.3.4/go.mod h1:hrV6Y/kQCLhIZXGcriDCUBtB3wnN7156gMXJ3+b23xM= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= +github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= +golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8= +golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/model/functions.go b/internal/model/functions.go index 193fdc1..12c727f 100644 --- a/internal/model/functions.go +++ b/internal/model/functions.go @@ -30,8 +30,10 @@ func printNoteField(fieldName string, fieldValue interface{}) string { return strings.Join(strs, "") } -// NewNote function provides prompt to registre a new Note. -func NewNote(tagIDs []int, promptNoteText Prompter) (*Note, error) { +// NewNote function provides prompt to register a new Note, and returns its answer. +func NewNote(tagIDs []int, useText string) (*Note, error) { + var noteText string + var err error note := &Note{ Comments: Comments{}, Status: "pending", @@ -42,7 +44,14 @@ func NewNote(tagIDs []int, promptNoteText Prompter) (*Note, error) { UpdatedAt: utils.CurrentUnixTimestamp()}, // Text: noteText, } - noteText, err := promptNoteText.Run() + if useText == "" { + noteText, err = utils.GeneratePrompt("note_text", "") + if err != nil { + return note, err + } + } else { + noteText = useText + } note.Text = utils.TrimString(noteText) if err != nil || strings.Contains(note.Text, "^C") { return note, err @@ -85,7 +94,11 @@ func BasicTags() Tags { } // NewTag funciton provides prompt for creating new Tag. -func NewTag(tagID int, promptTagSlug Prompter, promptTagGroup Prompter) (*Tag, error) { +// Pass useSlug and/or useGroup to use given values instead of prompting user. +func NewTag(tagID int, useSlug string, useGroup string) (*Tag, error) { + var err error + var tagSlug string + var tagGroup string tag := &Tag{ Id: tagID, BaseStruct: BaseStruct{ @@ -95,13 +108,17 @@ func NewTag(tagID int, promptTagSlug Prompter, promptTagGroup Prompter) (*Tag, e // Group: tagGroup, } // ask for tag slug - tagSlug, err := promptTagSlug.Run() + if useSlug == "" { + tagSlug, err = utils.GeneratePrompt("tag_slug", "") + // in case of error or Ctrl-c as input, don't create the tag + if err != nil || strings.Contains(tag.Slug, "^C") { + return tag, err + } + } else { + tagSlug = useSlug + } tag.Slug = utils.TrimString(tagSlug) tag.Slug = strings.ToLower(tag.Slug) - // in case of error or Ctrl-c as input, don't create the tag - if err != nil || strings.Contains(tag.Slug, "^C") { - return tag, err - } if len(utils.TrimString(tag.Slug)) == 0 { // this should never be encountered because of validation in earlier step fmt.Printf("%v Skipping adding tag with empty slug\n", utils.Symbols["warning"]) @@ -109,9 +126,13 @@ func NewTag(tagID int, promptTagSlug Prompter, promptTagGroup Prompter) (*Tag, e return tag, err } // ask for tag's group - tagGroup, err := promptTagGroup.Run() - if err != nil { - return tag, err + if useGroup == "" { + tagGroup, err = utils.GeneratePrompt("tag_group", "") + if err != nil { + return tag, err + } + } else { + tagGroup = useGroup } tag.Group = strings.ToLower(tagGroup) // return successful tag @@ -147,11 +168,9 @@ func MakeSureFileExists(dataFilePath string) error { // BlankReminder function creates blank ReminderData object. func BlankReminder() *ReminderData { fmt.Println("Initializing the data file. Please provide following data.") - promptUserName := utils.GeneratePrompt("user_name", "") - name, err := promptUserName.Run() + name, err := utils.GeneratePrompt("user_name", "") utils.PrintErrorIfPresent(err) - promptUserEmail := utils.GeneratePrompt("user_email", "") - emailID, err := promptUserEmail.Run() + emailID, err := utils.GeneratePrompt("user_email", "") utils.PrintErrorIfPresent(err) return &ReminderData{ User: &User{Name: name, EmailId: emailID}, diff --git a/internal/model/model_test.go b/internal/model/model_test.go index 82c4588..1f207de 100644 --- a/internal/model/model_test.go +++ b/internal/model/model_test.go @@ -849,22 +849,22 @@ func TestNewTagRegistration(t *testing.T) { } func TestNewTag(t *testing.T) { - mockPromptTagSlug := &MockPromptTagSlug{} - mockPromptTagGroup := &MockPromptTagGroup{} - tag, _ := model.NewTag(10, mockPromptTagSlug, mockPromptTagGroup) + dummySlug := "test_tag_slug" + dummyGroup := "test_tag_group" + tag, _ := model.NewTag(10, dummySlug, dummyGroup) want := &model.Tag{ Id: 10, - Slug: "test_tag_slug", - Group: "test_tag_group", + Slug: dummySlug, + Group: dummyGroup, } utils.AssertEqual(t, tag, want) } func TestNewNote(t *testing.T) { - mockPromptNoteText := &MockPromptNoteText{} tagIDs := []int{1, 3, 5} - note, _ := model.NewNote(tagIDs, mockPromptNoteText) + dummyText := "a random note text" + note, _ := model.NewNote(tagIDs, dummyText) want := &model.Note{ - Text: "a random note text", + Text: dummyText, TagIds: tagIDs, Status: note.Status, BaseStruct: model.BaseStruct{UpdatedAt: note.UpdatedAt, CreatedAt: note.CreatedAt}, diff --git a/internal/model/reminder_data.go b/internal/model/reminder_data.go index c547888..7258cf2 100644 --- a/internal/model/reminder_data.go +++ b/internal/model/reminder_data.go @@ -234,18 +234,17 @@ func (reminderData *ReminderData) SearchNotes() error { allTexts = append(allTexts, note.SearchableText()) } // function to search across notes - searchNotes := func(input string, idx int) bool { - input = strings.ToLower(input) - noteText := allTexts[idx] - if strings.Contains(strings.ToLower(noteText), input) { + searchNotes := func(filterValue string, optValue string, optIndex int) bool { + filterValue = strings.ToLower(filterValue) + noteText := allTexts[optIndex] + if strings.Contains(strings.ToLower(noteText), filterValue) { return true } return false } // display prompt - promptNoteSelection := utils.GenerateNoteSearchSelect(utils.ChopStrings(allTexts, utils.TerminalWidth()-10), searchNotes) fmt.Printf("Searching through a total of %v notes:\n", len(allTexts)) - index, _, err := promptNoteSelection.Run() + index, err := utils.GenerateNoteSearchSelect(utils.ChopStrings(allTexts, utils.TerminalWidth()-10), searchNotes) if err != nil { return err } @@ -324,10 +323,7 @@ func (reminderData *ReminderData) NewTagRegistration() (int, error) { // collect and ask info about the tag tagID := reminderData.nextPossibleTagId() - promptTagSlug := utils.GeneratePrompt("tag_slug", "") - promptTagGroup := utils.GeneratePrompt("tag_group", "") - - tag, err := NewTag(tagID, promptTagSlug, promptTagGroup) + tag, err := NewTag(tagID, "", "") // validate and save data if err != nil { @@ -372,8 +368,7 @@ func (reminderData *ReminderData) NewNoteRegistration(tagIDs []int) (*Note, erro // assuming each note with have on average 2 tags tagIDs = make([]int, 0, 2) } - promptNoteText := utils.GeneratePrompt("note_text", "") - note, err := NewNote(tagIDs, promptNoteText) + note, err := NewNote(tagIDs, "") // validate and save data if err != nil { utils.PrintErrorIfPresent(err) @@ -504,8 +499,7 @@ func (reminderData *ReminderData) AskTagIds(tagIDs []int) []int { tagIDs = append(tagIDs, tagID) } // check with user if another tag is to be added - promtTagAnother := utils.GeneratePrompt("tag_another", "") - promptText, err := promtTagAnother.Run() + promptText, err := utils.GeneratePrompt("tag_another", "") utils.PrintErrorIfPresent(err) promptText = strings.ToLower(promptText) nextTag := false @@ -539,8 +533,7 @@ func (reminderData *ReminderData) PrintNoteAndAskOptions(note *Note) string { "Select Action") switch noteOption { case fmt.Sprintf("%v %v", utils.Symbols["comment"], "Add comment"): - promptCommment := utils.GeneratePrompt("note_comment", "") - promptText, err := promptCommment.Run() + promptText, err := utils.GeneratePrompt("note_comment", "") utils.PrintErrorIfPresent(err) err = reminderData.AddNoteComment(note, promptText) utils.PrintErrorIfPresent(err) @@ -559,22 +552,19 @@ func (reminderData *ReminderData) PrintNoteAndAskOptions(note *Note) string { utils.PrintErrorIfPresent(err) fmt.Print(note.ExternalText(reminderData)) case fmt.Sprintf("%v %v", utils.Symbols["calendar"], "Update due date"): - promptCompleteBy := utils.GeneratePrompt("note_completed_by", "") - promptText, err := promptCompleteBy.Run() + promptText, err := utils.GeneratePrompt("note_completed_by", "") utils.PrintErrorIfPresent(err) err = reminderData.UpdateNoteCompleteBy(note, promptText) utils.PrintErrorIfPresent(err) fmt.Print(note.ExternalText(reminderData)) case fmt.Sprintf("%v %v", utils.Symbols["text"], "Update text"): - promptNoteTextWithDefault := utils.GeneratePrompt("note_text", note.Text) - promptText, err := promptNoteTextWithDefault.Run() + promptText, err := utils.GeneratePrompt("note_text", note.Text) utils.PrintErrorIfPresent(err) err = reminderData.UpdateNoteText(note, promptText) utils.PrintErrorIfPresent(err) fmt.Print(note.ExternalText(reminderData)) case fmt.Sprintf("%v %v", utils.Symbols["glossary"], "Update summary"): - promptNoteTextWithDefault := utils.GeneratePrompt("note_summary", note.Summary) - promptText, err := promptNoteTextWithDefault.Run() + promptText, err := utils.GeneratePrompt("note_summary", note.Summary) utils.PrintErrorIfPresent(err) err = reminderData.UpdateNoteSummary(note, promptText) utils.PrintErrorIfPresent(err) @@ -644,7 +634,6 @@ func (reminderData *ReminderData) PrintNotesAndAskOptions(notes Notes, tagID int } else { promptText = fmt.Sprintf("Select Note") } - // ask user to select a note noteIndex, _, err := utils.AskOption(append(texts, fmt.Sprintf("%v %v", utils.Symbols["add"], "Add Note")), promptText) if (err != nil) || (noteIndex == -1) { return err diff --git a/pkg/utils/functions.go b/pkg/utils/functions.go index 42baa5e..4995145 100644 --- a/pkg/utils/functions.go +++ b/pkg/utils/functions.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "github.com/manifoldco/promptui" + "github.com/AlecAivazis/survey/v2" ) // Location variable provides location info for `time`. @@ -245,20 +245,20 @@ func AskOption(options []string, label string) (int, string, error) { // note: any item in options should not have \n character // otherwise such item is observed to not getting appear // in the rendered list - prompt := promptui.Select{ - Label: label, - Items: options, - Size: 25, - Stdout: &bellSkipper{}, + var selectedIndex int + prompt := &survey.Select{ + Message: label, + Options: options, + PageSize: 25, } - index, result, err := prompt.Run() + err := survey.AskOne(prompt, &selectedIndex) if err != nil { // error can happen if user raises an interrupt (such as Ctrl-c, SIGINT) fmt.Printf("%v Prompt failed %v\n", Symbols["warning"], err) return -1, "", err } - fmt.Printf("You chose %d:%q\n", index, result) - return index, result, nil + fmt.Printf("You chose %d:%q\n", selectedIndex, options[selectedIndex]) + return selectedIndex, options[selectedIndex], nil } // PerformShellOperation function performs shell operation and return its output. @@ -321,76 +321,83 @@ func PerformCwdiff(oldFilePath string, newFilePath string) error { return err } -// GeneratePrompt function generates promptui.Prompt. -func GeneratePrompt(promptName string, defaultText string) *promptui.Prompt { - var prompt *promptui.Prompt +// GeneratePrompt function generates survey.Input. +func GeneratePrompt(promptName string, defaultText string) (string, error) { + var prompt *survey.Input + var validator survey.Validator + var answer string + switch promptName { case "user_name": - prompt = &promptui.Prompt{ - Label: "User Name", - Default: defaultText, - Validate: ValidateString, + prompt = &survey.Input{ + Message: "User Name: ", + Default: defaultText, } + validator = survey.Required case "user_email": - prompt = &promptui.Prompt{ - Label: "User Email", - Default: defaultText, - Validate: ValidateString, + prompt = &survey.Input{ + Message: "User Email: ", + Default: defaultText, } + validator = survey.MinLength(0) case "tag_slug": - prompt = &promptui.Prompt{ - Label: "Tag Slug", - Default: defaultText, - Validate: ValidateNonEmptyString, + prompt = &survey.Input{ + Message: "Tag Slug: ", + Default: defaultText, } + validator = survey.MinLength(1) case "tag_group": - prompt = &promptui.Prompt{ - Label: "Tag Group", - Default: defaultText, - Validate: ValidateString, + prompt = &survey.Input{ + Message: "Tag Group: ", + Default: defaultText, } + validator = survey.MinLength(1) case "tag_another": - prompt = &promptui.Prompt{ - Label: "Add another tag: yes/no (default: no):", - Default: defaultText, - Validate: ValidateString, + prompt = &survey.Input{ + Message: "Add another tag: yes/no (default: no): ", + Default: defaultText, } + validator = survey.MinLength(1) case "note_text": - prompt = &promptui.Prompt{ - Label: "Note Text", - Default: defaultText, - Validate: ValidateNonEmptyString, + prompt = &survey.Input{ + Message: "Note Text: ", + Default: defaultText, } + validator = survey.MinLength(1) case "note_summary": - prompt = &promptui.Prompt{ - Label: "Note Summary", - Default: defaultText, - Validate: ValidateNonEmptyString, + prompt = &survey.Input{ + Message: "Note Summary: ", + Default: defaultText, } + validator = survey.MinLength(1) case "note_comment": - prompt = &promptui.Prompt{ - Label: "New Comment", - Default: defaultText, - Validate: ValidateNonEmptyString, + prompt = &survey.Input{ + Message: "New Comment: ", + Default: defaultText, } + validator = survey.MinLength(1) case "note_completed_by": - prompt = &promptui.Prompt{ - Label: "Due Date (format: DD-MM-YYYY or DD-MM), or enter nil to clear existing value", - Default: defaultText, - Validate: ValidateDateString, + prompt = &survey.Input{ + Message: "Due Date (format: DD-MM-YYYY or DD-MM), or enter nil to clear existing value: ", + Default: defaultText, } + // TODO: This needs to be fixed + // Validate: ValidateDateString, + validator = survey.MinLength(1) } - return prompt -} - -// GenerateNoteSearchSelect function generates promptui.Select. -func GenerateNoteSearchSelect(items []string, searchFunc func(string, int) bool) *promptui.Select { - prompt := &promptui.Select{ - Label: "Notes", - Items: items, - Size: 25, - StartInSearchMode: true, - Searcher: searchFunc, + err := survey.AskOne(prompt, &answer, survey.WithValidator(validator)) + return answer, err +} + +// GenerateNoteSearchSelect function generates survey.Select and return index of selected option. +func GenerateNoteSearchSelect(items []string, searchFunc func(filter string, value string, index int) bool) (int, error) { + var selectedIndex int + prompt := &survey.Select{ + Message: "Search: ", + Options: items, + PageSize: 25, + Filter: searchFunc, } - return prompt + err := survey.AskOne(prompt, &selectedIndex) + return selectedIndex, err }