Skip to content

Commit 3b77fb9

Browse files
committed
Done edit feature
1 parent e0f9a3e commit 3b77fb9

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

internal/app/edit/edit.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"slices"
7+
"strings"
78

89
r_error "github.com/DanWlker/remind/internal/error"
910
"github.com/DanWlker/remind/internal/pkg/data"
@@ -32,7 +33,7 @@ func editTodoAssociatedWith(directory string) error {
3233

3334
dataFileFullPath := dataFolder + string(os.PathSeparator) + currentDirectoryRecord.DataFileName
3435

35-
prettyPrintedString, errSPrettyPrintDataFile := data.SPrettyPrintDataFile(dataFileFullPath, "")
36+
prettyPrintedString, errSPrettyPrintDataFile := data.SPrettyPrintDataFile(dataFileFullPath, nil)
3637
if errSPrettyPrintDataFile != nil {
3738
return fmt.Errorf("data.SPrettyPrintDataFile: %w", errSPrettyPrintDataFile)
3839
}
@@ -42,7 +43,17 @@ func editTodoAssociatedWith(directory string) error {
4243
return fmt.Errorf("shared.OpenDefaultEditor: %w", errOpenDefaultEditor)
4344
}
4445

45-
fmt.Println(result)
46+
var todoList []data.TodoEntity
47+
for _, item := range strings.Split(string(result), "\n") {
48+
if item != "" {
49+
todoList = append(todoList, data.TodoEntity{Text: item})
50+
}
51+
}
52+
53+
errWriteTodoToFile := data.WriteTodoToFile(dataFileFullPath, todoList)
54+
if errWriteTodoToFile != nil {
55+
return fmt.Errorf("helper.WriteTodoToFile: %w", errWriteTodoToFile)
56+
}
4657

4758
return nil
4859
}

internal/app/list/list.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ func listOne(pathToFind string) error {
3131
return fmt.Errorf("helper.GetDataFolder: %w", errGetDataFolder)
3232
}
3333

34-
if errPrettyPrintFile := data.PrettyPrintDataFile(dataFolder+string(os.PathSeparator)+projectRecordEntity.DataFileName, " "); errPrettyPrintFile != nil {
34+
if errPrettyPrintFile := data.PrettyPrintDataFile(
35+
dataFolder+string(os.PathSeparator)+projectRecordEntity.DataFileName,
36+
func(todo string, index int) string {
37+
return fmt.Sprintf("\t%v. %v", index, todo)
38+
}); errPrettyPrintFile != nil {
3539
return fmt.Errorf("helper.PrettyPrintDataFile: %w", errPrettyPrintFile)
3640
}
3741
return nil
@@ -48,7 +52,12 @@ func listConcurrently(item record.RecordEntity, dataFolder string) (chan string,
4852
}
4953

5054
go func() {
51-
result, errPrettyPrintDataFile := data.SPrettyPrintDataFile(dataFolder+string(os.PathSeparator)+item.DataFileName, " ")
55+
result, errPrettyPrintDataFile := data.SPrettyPrintDataFile(
56+
dataFolder+string(os.PathSeparator)+item.DataFileName,
57+
func(todo string, index int) string {
58+
return fmt.Sprintf("\t%v. %v", index, todo)
59+
},
60+
)
5261
if errPrettyPrintDataFile != nil {
5362
c <- fmt.Sprintf("Error: Something went wrong: data.SPrettyPrintDataFile: %v", errPrettyPrintDataFile)
5463
}

internal/pkg/data/data.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,27 @@ func GetDataFolder() (string, error) {
5656
return dataFolder, nil
5757
}
5858

59-
// TODO: Change prefix to be a func
60-
func SPrettyPrintDataFile(dataFileFullPath string, prefix string) (string, error) {
59+
func SPrettyPrintDataFile(dataFileFullPath string, editText func(todo string, index int) string) (string, error) {
6160
var b bytes.Buffer
6261
todoList, errGetTodoFromDataFile := GetTodoFromDataFile(dataFileFullPath)
6362
if errGetTodoFromDataFile != nil {
6463
return "", fmt.Errorf("GetTodoFromDataFile: %w", errGetTodoFromDataFile)
6564
}
6665

6766
for i, todo := range todoList {
68-
b.WriteString(fmt.Sprintf("%v%v. %v\n", prefix, i, todo.Text))
67+
if editText == nil {
68+
b.WriteString(todo.Text)
69+
} else {
70+
b.WriteString(editText(todo.Text, i))
71+
}
72+
b.WriteString("\n")
6973
}
7074

7175
return b.String(), nil
7276
}
7377

74-
func PrettyPrintDataFile(dataFileFullPath string, prefix string) error {
75-
result, err := SPrettyPrintDataFile(dataFileFullPath, prefix)
78+
func PrettyPrintDataFile(dataFileFullPath string, editText func(todo string, index int) string) error {
79+
result, err := SPrettyPrintDataFile(dataFileFullPath, editText)
7680
if err != nil {
7781
return fmt.Errorf("SPrettyPrintDataFile: %w", err)
7882
}

0 commit comments

Comments
 (0)