summaryrefslogtreecommitdiff
path: root/internal/prompt
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-31 22:07:56 +0200
committerPaul Buetow <paul@buetow.org>2024-10-31 22:07:56 +0200
commit368d58e61ab3f9978c7cf51ee4c0d45263b9e5c5 (patch)
tree86c39f05f4d9d071ff3d9266599f96e37aff8c8e /internal/prompt
parente45b591404ebb36fa7d71510e99fdecf668a85e7 (diff)
refactor
Diffstat (limited to 'internal/prompt')
-rw-r--r--internal/prompt/file.go54
-rw-r--r--internal/prompt/prompt.go62
2 files changed, 59 insertions, 57 deletions
diff --git a/internal/prompt/file.go b/internal/prompt/file.go
new file mode 100644
index 0000000..d01f066
--- /dev/null
+++ b/internal/prompt/file.go
@@ -0,0 +1,54 @@
+package prompt
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "strings"
+)
+
+func FileAction(question, content, filePath string) error {
+ info2(filePath)
+ fmt.Print(":\n")
+ info1(content)
+ fmt.Print("\n")
+ reader := bufio.NewReader(os.Stdin)
+
+ for {
+ ack("%s (y=yes/n=no/e=edit/d=delete):", question)
+ input, err := reader.ReadString('\n')
+ if err != nil {
+ fmt.Println("Error reading input:", err)
+ continue
+ }
+
+ switch strings.ToLower(strings.TrimSpace(input)) {
+ case "y", "yes":
+ return nil
+ case "n", "no":
+ return ErrAborted
+ case "e", "edit":
+ return EditFile(filePath)
+ case "d", "delete":
+ return os.Remove(filePath)
+ default:
+ fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.")
+ }
+ }
+}
+
+func EditFile(filePath string) error {
+ editor, ok := os.LookupEnv("EDITOR")
+ if !ok {
+ return errors.New("EDITOR environment variable is not set")
+ }
+
+ cmd := exec.Command(editor, filePath)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ cmd.Stdin = os.Stdin
+
+ return cmd.Run()
+}
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
index e9c5e72..d8684f3 100644
--- a/internal/prompt/prompt.go
+++ b/internal/prompt/prompt.go
@@ -5,30 +5,19 @@ import (
"errors"
"fmt"
"os"
- "os/exec"
- "strings"
"github.com/fatih/color"
)
var (
- ErrAborted = errors.New("aborted")
- ErrEditContent = errors.New("edit content")
- ErrDeleteFile = errors.New("delete file")
- info = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc()
- ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc()
+ ErrAborted = errors.New("aborted")
+ info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc()
+ info2 = color.New(color.FgHiYellow, color.BgBlue, color.Bold).PrintfFunc()
+ ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc()
)
-// TODO: Refactor this prompt, including all operations done on the file, like abort, edit, remove, etc.
-// TODO: And also don't use error for control flow.
-func DoYouWantThis(question, content string) error {
- info(content)
- fmt.Print("\n")
- return whatNow(question)
-}
-
func Acknowledge(message, content string) error {
- info(content)
+ info1(content)
fmt.Print("\n")
ack(message + " (press enter)")
reader := bufio.NewReader(os.Stdin)
@@ -37,44 +26,3 @@ func Acknowledge(message, content string) error {
}
return nil
}
-
-func whatNow(question string) error {
- reader := bufio.NewReader(os.Stdin)
-
- for {
- ack("%s (y=yes/n=no/e=edit/d=delete):", question)
- input, err := reader.ReadString('\n')
- if err != nil {
- fmt.Println("Error reading input:", err)
- continue
- }
-
- switch strings.ToLower(strings.TrimSpace(input)) {
- case "y", "yes":
- return nil
- case "n", "no":
- return ErrAborted
- case "e", "edit":
- return ErrEditContent
- case "d", "delete":
- // TODO: Implement
- return ErrDeleteFile
- default:
- fmt.Println("Please enter 'y' or 'n' or 'e' or 'd'.")
- }
- }
-}
-
-func EditFile(filePath string) error {
- editor, ok := os.LookupEnv("EDITOR")
- if !ok {
- return errors.New("EDITOR environment variable is not set")
- }
-
- cmd := exec.Command(editor, filePath)
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- cmd.Stdin = os.Stdin
-
- return cmd.Run()
-}