diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-18 22:30:10 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-18 22:30:10 +0300 |
| commit | 49750f5fb56b94ce0ee7a86b833aa7abf6db66ae (patch) | |
| tree | 521951312b2c2b00a76c8908405176a2f5d36c23 | |
| parent | 0ad3bbbfc1f2c126d0c9c7282958b5839192b49e (diff) | |
can edit files when they are too large
| -rw-r--r-- | internal/entry/entry.go | 16 | ||||
| -rw-r--r-- | internal/prompt/prompt.go | 22 |
2 files changed, 28 insertions, 10 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 9f1eadf..475c315 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -78,18 +78,26 @@ func New(filePath string) (Entry, error) { func (e Entry) Content() (string, error) { bytes, err := os.ReadFile(e.Path) - return string(bytes), err + if err != err { + return "", err + } + return strings.TrimSpace(string(bytes)), nil } -// TODO: Optionally open editor when a content is too large. func (e Entry) ContentWithLimit(sizeLimit int) (string, error) { content, err := e.Content() if err != nil { return "", err } if len(content) > sizeLimit { - return "", fmt.Errorf("entry content exceeds size limit: %d > %d: %v", - len(content), sizeLimit, e) + err := fmt.Errorf("entry content exceeds size limit: %d > %d: %v", len(content), sizeLimit, e) + if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil { + return "", errors.Join(err, err2) + } + if err2 := e.Edit(); err2 != nil { + return "", errors.Join(err, err2) + } + return e.ContentWithLimit(sizeLimit) } return content, nil } diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index e9d35c1..bf5997d 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -12,24 +12,34 @@ import ( ) var ( - ErrAborted = errors.New("aborted") - // TODO: Add edit functionality. 1. configure EDITOR, 2. fork EDITOR process on the given file. + ErrAborted = errors.New("aborted") ErrEditContent = errors.New("edit content") - contentColor = color.New(color.FgCyan, color.BgBlue, color.Bold).SprintFunc() - dangerColor = color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc() + blue = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc() + red = color.New(color.FgWhite, color.BgRed, color.Bold).PrintfFunc() ) func DoYouWantThis(question, content string) error { - fmt.Print(contentColor(content)) + blue(content) fmt.Print("\n") return whatNow(question) } +func Acknowledge(message, content string) error { + blue(content) + fmt.Print("\n") + red(message + " (press enter)") + reader := bufio.NewReader(os.Stdin) + if _, err := reader.ReadString('\n'); err != nil { + return err + } + return nil +} + func whatNow(question string) error { reader := bufio.NewReader(os.Stdin) for { - fmt.Printf("%s ", dangerColor(fmt.Sprintf("%s (y=yes/n=no/e=edit):", question))) + red("%s (y=yes/n=no/e=edit):", question) input, err := reader.ReadString('\n') if err != nil { fmt.Println("Error reading input:", err) |
