Skip to content

Commit

Permalink
Improve editor package and add-task command
Browse files Browse the repository at this point in the history
  • Loading branch information
hatappi committed Jun 11, 2024
1 parent f86ef0b commit b3b321b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
39 changes: 20 additions & 19 deletions cmd/addTask.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,34 @@ And add a task using the editor.
return err
}

name := strings.Join(args, " ")
if name != "" {
err = task.AddTask(config.TaskFile, name)
var newTasks []string

if len(args) > 0 {
newTasks = append(newTasks, strings.Join(args, " "))
}

if len(newTasks) == 0 {
lines, err := editor.ContentsByLine(initialText)
if err != nil {
return err
}
fmt.Printf("add %s\n", name)
return nil
}

ts, err := editor.GetSliceText(initialText)
if err != nil {
return err
}
for _, l := range lines {
// ignore empty string and comment
if l == "" || strings.HasPrefix(l, "#") {
continue
}

for _, t := range ts {
if t == "" {
continue
}
if strings.HasPrefix(t, "#") {
continue
newTasks = append(newTasks, l)
}
err = task.AddTask(config.TaskFile, t)
if err != nil {
}

for _, newTask := range newTasks {
if err := task.AddTask(config.TaskFile, newTask); err != nil {
return err
}
fmt.Printf("add %s\n", t)

fmt.Printf("add '%s'\n", newTask)
}

return nil
Expand Down
29 changes: 8 additions & 21 deletions internal/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
package editor

import (
"bufio"
"os"
"os/exec"
"strings"
)

// GetSliceText get slice text edited with editor.
func GetSliceText(initialText string) ([]string, error) {
// ContentsByLine gets contents edited with editor by line.
func ContentsByLine(initialText string) ([]string, error) {
tmpfile, err := os.CreateTemp("", "gomodoro")
if err != nil {
return nil, err
Expand All @@ -20,36 +20,23 @@ func GetSliceText(initialText string) ([]string, error) {
_ = tmpfile.Close()
}()

if _, err = tmpfile.WriteString(initialText); err != nil {
if _, err := tmpfile.WriteString(initialText); err != nil {

Check failure on line 23 in internal/editor/editor.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] internal/editor/editor.go#L23

shadow: declaration of "err" shadows declaration at line 12 (govet)
Raw output
internal/editor/editor.go:23:8: shadow: declaration of "err" shadows declaration at line 12 (govet)
	if _, err := tmpfile.WriteString(initialText); err != nil {
	      ^
return nil, err
}

if err = openEditor(tmpfile.Name()); err != nil {
if err := edit(tmpfile.Name()); err != nil {

Check failure on line 27 in internal/editor/editor.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] internal/editor/editor.go#L27

shadow: declaration of "err" shadows declaration at line 12 (govet)
Raw output
internal/editor/editor.go:27:5: shadow: declaration of "err" shadows declaration at line 12 (govet)
	if err := edit(tmpfile.Name()); err != nil {
	   ^
return nil, err
}

f, err := os.Open(tmpfile.Name())
b, err := os.ReadFile(tmpfile.Name())
if err != nil {
return nil, err
}
defer func() {
_ = f.Close()
}()

ts := make([]string, 0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
ts = append(ts, scanner.Text())
}

if err := scanner.Err(); err != nil {
return nil, err
}

return ts, nil
return strings.Split(string(b), "\n"), nil
}

func openEditor(filepath string) error {
func edit(filepath string) error {
cmdName := "vi"
if e := os.Getenv("EDITOR"); e != "" {
cmdName = e
Expand Down

0 comments on commit b3b321b

Please sign in to comment.