-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.go
More file actions
199 lines (173 loc) · 4.56 KB
/
notes.go
File metadata and controls
199 lines (173 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
type NoteList map[string]string
func show(w io.Writer, notes NoteList) {
if len(notes) == 0 {
fmt.Fprintln(w, "No notes yet")
return
}
for key, value := range notes {
fmt.Fprintf(w, "%s: %v\n", key, len(value))
}
}
func FileChanged(initialStat os.FileInfo, file *os.File, c chan<- bool) {
for {
stat, err := file.Stat()
if err != nil {
return
}
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
c <- true
}
time.Sleep(10 * time.Millisecond)
}
}
func ProgramClosed(c chan<- bool, command *exec.Cmd) {
command.Wait()
// Wait for program termination signal
c <- true
}
const EDITOR_FILENAME = "./RAGADSFILE"
func addNew(w io.Writer, r io.Reader, notes NoteList, editorCommand string, editorArgs ...string) error {
//print request for input for title
reader := bufio.NewReader(r)
fmt.Fprintln(w, "Enter note title: ")
// read input and store in variable for title
input, _ := reader.ReadString('\n')
noteTitle := strings.TrimSpace(input)
//make a temp file
file, err := os.Create(EDITOR_FILENAME)
if err != nil {
return err
}
if err := file.Sync(); err != nil {
return err
}
//print request for input for text
fmt.Fprintln(w, "Enter lines of text:")
contents, err := editFile(file, editorCommand, editorArgs)
if err != nil {
return err
}
notes[noteTitle] = contents
return nil
}
// edits a temp file with the given editor and returns the contents or an error
func editFile(file *os.File, editorCommand string, editorArgs []string) (string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// spawn a editor for the file
cmd := exec.CommandContext(ctx, editorCommand, append(editorArgs, file.Name())...)
// watch the file for edits
initialStat, err := file.Stat()
if err != nil {
return "", err
}
cmd.Start()
c := make(chan bool)
go FileChanged(initialStat, file, c)
go ProgramClosed(c, cmd)
<-c
// when saved copy the contents to the storage at the `title`
contents, err := os.ReadFile(file.Name())
if err != nil {
return "", err
}
// clear the contents of the temp file (or destroy it)
err = os.Remove(file.Name())
if err != nil {
return "", err
}
// TODO: close the spawned program
return string(contents), nil
}
func deleteFileName(w io.Writer, r io.Reader, notes NoteList) {
show(w, notes)
reader := bufio.NewReader(r)
fmt.Fprint(w, "Enter the file name of the note you want to delete: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
delete(notes, input)
show(w, notes)
}
func edit(w io.Writer, r io.Reader, notes NoteList, editorCommand string, editorArgs ...string) error {
show(w, notes)
reader := bufio.NewReader(r)
fmt.Fprint(w, "Enter the file name of the note you want to edit: ")
input, _ := reader.ReadString('\n')
noteTitle := strings.TrimSpace(input)
value, ok := notes[noteTitle]
if !ok {
return fmt.Errorf("no note found called %s", noteTitle)
}
//make a temp file
file, err := os.Create(EDITOR_FILENAME)
if err != nil {
return err
}
_, err = file.WriteString(value)
if err != nil {
return err
}
contents, err := editFile(file, editorCommand, editorArgs)
if err != nil {
return err
}
notes[noteTitle] = contents
return nil
}
const DEFAULT_EDITOR = "code"
const DEFAULT_EDITOR_ARGS = "--wait"
const EDITOR_ENV_VAR = "EDITOR"
const EDITOR_ARGS_ENV_VAR = "EDITOR_ARGS"
func getVarOrDefault(envVar, defaultValue string) string {
value, set := os.LookupEnv(envVar)
value = strings.TrimSpace(value)
if value == "" || !set {
value = defaultValue
}
return value
}
func getEditor() string {
return getVarOrDefault(EDITOR_ENV_VAR, DEFAULT_EDITOR)
}
func getEditorArgs() []string {
args := getVarOrDefault(EDITOR_ARGS_ENV_VAR, DEFAULT_EDITOR_ARGS)
splitArgs := strings.Split(args, " ")
return splitArgs
}
func main() {
notes := make(map[string]string)
editor := getEditor()
editorArgs := getEditorArgs()
for {
var i int
fmt.Println("select an option by the number: \n1. show all notes \n2. add a note\n3. delete a note \n4. edit a note")
fmt.Scan(&i)
switch i {
case 1:
fmt.Println("You want to see your notes")
show(os.Stdout, notes)
case 2:
fmt.Println("You want to add a note")
addNew(os.Stdin, os.Stdout, notes, editor, editorArgs...)
case 3:
fmt.Println("You want to delete a note")
deleteFileName(os.Stdin, os.Stdout, notes)
case 4:
fmt.Println("You want edit a note")
edit(os.Stdin, os.Stdout, notes, editor, editorArgs...)
default:
fmt.Println("Choose between 1-4")
}
}
}