File tree Expand file tree Collapse file tree 2 files changed +27
-39
lines changed Expand file tree Collapse file tree 2 files changed +27
-39
lines changed Original file line number Diff line number Diff line change @@ -28,33 +28,34 @@ And add a task using the editor.
28
28
return err
29
29
}
30
30
31
- name := strings .Join (args , " " )
32
- if name != "" {
33
- err = task .AddTask (config .TaskFile , name )
31
+ var newTasks []string
32
+
33
+ if len (args ) > 0 {
34
+ newTasks = append (newTasks , strings .Join (args , " " ))
35
+ }
36
+
37
+ if len (newTasks ) == 0 {
38
+ lines , err := editor .ContentsByLine (initialText )
34
39
if err != nil {
35
40
return err
36
41
}
37
- fmt .Printf ("add %s\n " , name )
38
- return nil
39
- }
40
42
41
- ts , err := editor .GetSliceText (initialText )
42
- if err != nil {
43
- return err
44
- }
43
+ for _ , l := range lines {
44
+ // ignore empty string and comment
45
+ if l == "" || strings .HasPrefix (l , "#" ) {
46
+ continue
47
+ }
45
48
46
- for _ , t := range ts {
47
- if t == "" {
48
- continue
49
- }
50
- if strings .HasPrefix (t , "#" ) {
51
- continue
49
+ newTasks = append (newTasks , l )
52
50
}
53
- err = task .AddTask (config .TaskFile , t )
54
- if err != nil {
51
+ }
52
+
53
+ for _ , newTask := range newTasks {
54
+ if err := task .AddTask (config .TaskFile , newTask ); err != nil {
55
55
return err
56
56
}
57
- fmt .Printf ("add %s\n " , t )
57
+
58
+ fmt .Printf ("added '%s'\n " , newTask )
58
59
}
59
60
60
61
return nil
Original file line number Diff line number Diff line change 2
2
package editor
3
3
4
4
import (
5
- "bufio"
6
5
"os"
7
6
"os/exec"
7
+ "strings"
8
8
)
9
9
10
- // GetSliceText get slice text edited with editor.
11
- func GetSliceText (initialText string ) ([]string , error ) {
10
+ // ContentsByLine gets contents edited with editor by line .
11
+ func ContentsByLine (initialText string ) ([]string , error ) {
12
12
tmpfile , err := os .CreateTemp ("" , "gomodoro" )
13
13
if err != nil {
14
14
return nil , err
@@ -24,32 +24,19 @@ func GetSliceText(initialText string) ([]string, error) {
24
24
return nil , err
25
25
}
26
26
27
- if err = openEditor (tmpfile .Name ()); err != nil {
27
+ if err = edit (tmpfile .Name ()); err != nil {
28
28
return nil , err
29
29
}
30
30
31
- f , err := os .Open (tmpfile .Name ())
31
+ b , err := os .ReadFile (tmpfile .Name ())
32
32
if err != nil {
33
33
return nil , err
34
34
}
35
- defer func () {
36
- _ = f .Close ()
37
- }()
38
-
39
- ts := make ([]string , 0 )
40
- scanner := bufio .NewScanner (f )
41
- for scanner .Scan () {
42
- ts = append (ts , scanner .Text ())
43
- }
44
-
45
- if err := scanner .Err (); err != nil {
46
- return nil , err
47
- }
48
35
49
- return ts , nil
36
+ return strings . Split ( string ( b ), " \n " ) , nil
50
37
}
51
38
52
- func openEditor (filepath string ) error {
39
+ func edit (filepath string ) error {
53
40
cmdName := "vi"
54
41
if e := os .Getenv ("EDITOR" ); e != "" {
55
42
cmdName = e
You can’t perform that action at this time.
0 commit comments