Skip to content

Commit

Permalink
Use AlecAivazis/survey instead of manifoldco/promptui (#99)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
goyalmunish authored Apr 14, 2022
1 parent 7ad4f75 commit 85ba273
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 122 deletions.
3 changes: 1 addition & 2 deletions cmd/reminder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main

import (
"fmt"
"time"

"reminder/internal/model"
"reminder/pkg/utils"
Expand Down Expand Up @@ -71,6 +70,6 @@ func flow() {
}

func main() {
go utils.Spinner(100 * time.Millisecond)
// go utils.Spinner(100 * time.Millisecond)
flow()
}
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
53 changes: 43 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
51 changes: 35 additions & 16 deletions internal/model/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -95,23 +108,31 @@ 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"])
err := errors.New("Tag's slug is empty")
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
Expand Down Expand Up @@ -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},
Expand Down
16 changes: 8 additions & 8 deletions internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
35 changes: 12 additions & 23 deletions internal/model/reminder_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 85ba273

Please sign in to comment.