From 844b726343a16e4cfbeac917eec7fe0987baa102 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 7 Mar 2024 12:01:49 -0500 Subject: [PATCH] Allow optionally adding notes to new password entries --- src/cli/add.go | 14 +++++++++----- src/cli/utilitiesMisc.go | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/cli/add.go b/src/cli/add.go index 476cddb..f9fe7cf 100644 --- a/src/cli/add.go +++ b/src/cli/add.go @@ -17,11 +17,15 @@ func AddEntry(targetLocation string, hidePassword bool, isNote bool) { var unencryptedEntry []string if !isNote { - username := input("Username: ") - password := inputHidden("Password: ") - url := input("URL: ") - // TODO prompt for optional note - unencryptedEntry = []string{password, username, url} + username := input("Username:") + password := inputHidden("Password:") + url := input("URL:") + if inputBinary("Add a note to this entry?") { + note := newNote() + unencryptedEntry = append([]string{password, username, url}, note...) + } else { + unencryptedEntry = []string{password, username, url} + } } else { note := newNote() unencryptedEntry = append([]string{"", "", ""}, note...) diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go index 2022bbf..ba137bb 100644 --- a/src/cli/utilitiesMisc.go +++ b/src/cli/utilitiesMisc.go @@ -11,7 +11,7 @@ import ( // input prompts the user for input and returns the input as a string func input(prompt string) string { - fmt.Print("\n" + prompt) + fmt.Print("\n" + prompt + " ") var input string fmt.Scanln(&input) return input @@ -19,13 +19,24 @@ func input(prompt string) string { // inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal func inputHidden(prompt string) string { - fmt.Print("\n" + prompt) + fmt.Print("\n" + prompt + " ") byteInput, _ := terminal.ReadPassword(int(os.Stdin.Fd())) password := string(byteInput) fmt.Println() return password } +// inputBinary prompts the user with a yes/no question and returns the response as a boolean +func inputBinary(prompt string) bool { + reader := bufio.NewReader(os.Stdin) + fmt.Print("\n" + prompt + " (y/N) ") + char, _, _ := reader.ReadRune() + if char == 'y' || char == 'Y' { + return true + } + return false +} + // newNote uses the user-specified text editor to create a new note and returns the note as a slice of strings func newNote() []string { tempFile := offline.CreateTempFile()