summaryrefslogtreecommitdiff
path: root/internal/ui/table.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ui/table.go')
-rw-r--r--internal/ui/table.go42
1 files changed, 31 insertions, 11 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go
index 1314eb1..ae32070 100644
--- a/internal/ui/table.go
+++ b/internal/ui/table.go
@@ -204,6 +204,12 @@ type descEditDoneMsg struct {
type blinkMsg struct{}
+type descriptionTempFile interface {
+ Name() string
+ WriteString(string) (int, error)
+ Close() error
+}
+
// blinkInterval controls how quickly the row flashes when a task changes.
// A shorter interval results in a faster blink.
const blinkInterval = 150 * time.Millisecond
@@ -212,6 +218,28 @@ const blinkInterval = 150 * time.Millisecond
// The total blink duration is blinkInterval * blinkCycles.
const blinkCycles = 8
+func prepareDescriptionTempFile(description string, newTempFile func() (descriptionTempFile, error)) (string, error) {
+ tmpFile, err := newTempFile()
+ if err != nil {
+ return "", err
+ }
+
+ tmpPath := tmpFile.Name()
+
+ _, writeErr := tmpFile.WriteString(description)
+ closeErr := tmpFile.Close()
+ if writeErr != nil {
+ _ = os.Remove(tmpPath)
+ return "", writeErr
+ }
+ if closeErr != nil {
+ _ = os.Remove(tmpPath)
+ return "", closeErr
+ }
+
+ return tmpPath, nil
+}
+
// editCmd returns a command that edits the task and sends an
// editDoneMsg once the process is complete.
func editCmd(id int) tea.Cmd {
@@ -222,18 +250,10 @@ func editCmd(id int) tea.Cmd {
// editDescriptionCmd returns a command that opens the description in external editor
func editDescriptionCmd(description string) tea.Cmd {
return func() tea.Msg {
- // Create temp file
- tmpFile, err := os.CreateTemp("", "tasksamurai-desc-*.txt")
- if err != nil {
- return descEditDoneMsg{err: err, tempFile: ""}
- }
- tmpPath := tmpFile.Name()
-
- // Write current description to temp file
- _, err = tmpFile.WriteString(description)
- _ = tmpFile.Close()
+ tmpPath, err := prepareDescriptionTempFile(description, func() (descriptionTempFile, error) {
+ return os.CreateTemp("", "tasksamurai-desc-*.txt")
+ })
if err != nil {
- _ = os.Remove(tmpPath)
return descEditDoneMsg{err: err, tempFile: ""}
}