Skip to content

Commit 459696c

Browse files
committed
some minor UX updates (spinner), and update how comment funcs return results (via cb)
1 parent 3602da0 commit 459696c

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

cmd/commands/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
func validateDir(dir string) {
1010
if dir == "" {
1111
cwd, err := os.Getwd()
12-
handleError(err)
12+
handleError(err, nil)
1313
dir = cwd
1414
}
1515

1616
abs, err := filepath.Abs(filepath.Join(dir, ".git"))
17-
handleError(err)
17+
handleError(err, nil)
1818

1919
if _, err := os.Stat(abs); os.IsNotExist(err) {
20-
handleError(fmt.Errorf("%s is not a git repository", abs))
20+
handleError(fmt.Errorf("%s is not a git repository", abs), nil)
2121
}
2222
}

cmd/commands/root.go

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

7+
"github.com/briandowns/spinner"
78
"github.com/spf13/cobra"
89
)
910

@@ -14,9 +15,15 @@ var rootCmd = &cobra.Command{
1415
}
1516

1617
// TODO clean this up
17-
func handleError(err error) {
18+
func handleError(err error, spinner *spinner.Spinner) {
1819
if err != nil {
19-
fmt.Println(err)
20+
if spinner != nil {
21+
// spinner.Suffix = ""
22+
spinner.FinalMSG = err.Error()
23+
spinner.Stop()
24+
} else {
25+
fmt.Println(err)
26+
}
2027
os.Exit(1)
2128
}
2229
}

cmd/commands/status.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ var statusCmd = &cobra.Command{
2020
Args: cobra.MaximumNArgs(1),
2121
Run: func(cmd *cobra.Command, args []string) {
2222
cwd, err := os.Getwd()
23-
handleError(err)
23+
handleError(err, nil)
2424

2525
dir := cwd
2626
if len(args) == 1 {
2727
dir, err = filepath.Rel(cwd, args[0])
28-
handleError(err)
28+
handleError(err, nil)
2929
}
3030

3131
validateDir(dir)
3232

3333
r, err := git.PlainOpen(dir)
34-
handleError(err)
34+
handleError(err, nil)
3535

3636
ref, err := r.Head()
37-
handleError(err)
37+
handleError(err, nil)
3838

3939
commit, err := r.CommitObject(ref.Hash())
40-
handleError(err)
40+
handleError(err, nil)
4141

4242
err = tickgit.WriteStatus(commit, os.Stdout)
43-
handleError(err)
43+
handleError(err, nil)
4444
},
4545
}

cmd/commands/todos.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"sort"
@@ -24,36 +25,44 @@ var todosCmd = &cobra.Command{
2425
Args: cobra.MaximumNArgs(1),
2526
Run: func(cmd *cobra.Command, args []string) {
2627
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
28+
s.HideCursor = true
2729
s.Suffix = " finding TODOs"
2830
s.Writer = os.Stderr
2931
s.Start()
3032

3133
cwd, err := os.Getwd()
32-
handleError(err)
34+
handleError(err, s)
3335

3436
dir := cwd
3537
if len(args) == 1 {
3638
dir, err = filepath.Rel(cwd, args[0])
37-
handleError(err)
39+
handleError(err, s)
3840
}
3941

4042
validateDir(dir)
4143

42-
comments, err := comments.SearchDir(dir)
43-
handleError(err)
44-
45-
t := todos.NewToDos(comments)
44+
foundToDos := make(todos.ToDos, 0)
45+
err = comments.SearchDir(dir, func(comment *comments.Comment) {
46+
todo := todos.NewToDo(*comment)
47+
if todo != nil {
48+
foundToDos = append(foundToDos, todo)
49+
s.Suffix = fmt.Sprintf(" %d TODOs found", len(foundToDos))
50+
}
51+
})
52+
handleError(err, s)
4653

54+
s.Suffix = fmt.Sprintf(" blaming %d TODOs", len(foundToDos))
4755
ctx := context.Background()
4856
// timeout after 30 seconds
4957
// ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
5058
// defer cancel()
51-
err = t.FindBlame(ctx, dir)
52-
sort.Sort(&t)
59+
err = foundToDos.FindBlame(ctx, dir)
60+
sort.Sort(&foundToDos)
5361

54-
handleError(err)
62+
handleError(err, s)
5563

5664
s.Stop()
57-
todos.WriteTodos(t, os.Stdout)
65+
66+
todos.WriteTodos(foundToDos, os.Stdout)
5867
},
5968
}

pkg/comments/comments.go

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package comments
22

33
import (
4-
"bytes"
54
"io"
6-
"io/ioutil"
75
"os"
86
"path/filepath"
97
"strings"
@@ -71,41 +69,39 @@ type Comment struct {
7169
}
7270

7371
// SearchFile searches a file for comments. It infers the language
74-
func SearchFile(filePath string, reader io.Reader) (Comments, error) {
75-
src, err := ioutil.ReadAll(reader)
76-
if err != nil {
77-
return nil, err
78-
}
79-
lang := Language(enry.GetLanguage(filepath.Base(filePath), src))
72+
func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
73+
// src, err := ioutil.ReadAll(reader)
74+
// if err != nil {
75+
// return nil, err
76+
// }
77+
lang := Language(enry.GetLanguage(filepath.Base(filePath), nil))
8078
if enry.IsVendor(filePath) {
81-
return nil, nil
79+
return nil
8280
}
8381
options, ok := LanguageParseOptions[lang]
8482
if !ok { // TODO provide a default parse option?
85-
return nil, nil
83+
return nil
8684
}
8785
commentParser, err := lege.NewParser(options)
8886
if err != nil {
89-
return nil, err
87+
return err
9088
}
9189

92-
collections, err := commentParser.Parse(bytes.NewReader(src))
90+
collections, err := commentParser.Parse(reader)
9391
if err != nil {
94-
return nil, err
92+
return err
9593
}
9694

97-
comments := make(Comments, 0)
9895
for _, c := range collections {
9996
comment := Comment{*c, filePath}
100-
comments = append(comments, &comment)
97+
cb(&comment)
10198
}
10299

103-
return comments, nil
100+
return nil
104101
}
105102

106103
// SearchDir searches a directory for comments
107-
func SearchDir(dirPath string) (Comments, error) {
108-
found := make(Comments, 0)
104+
func SearchDir(dirPath string, cb func(comment *Comment)) error {
109105
err := godirwalk.Walk(dirPath, &godirwalk.Options{
110106
Callback: func(path string, de *godirwalk.Dirent) error {
111107
localPath, err := filepath.Rel(dirPath, path)
@@ -130,36 +126,32 @@ func SearchDir(dirPath string) (Comments, error) {
130126
if err != nil {
131127
return err
132128
}
133-
defer f.Close()
134-
t, err := SearchFile(localPath, f)
129+
err = SearchFile(localPath, f, cb)
135130
if err != nil {
136131
return err
137132
}
138-
c := Comments(t)
139-
found = append(found, c...)
133+
f.Close()
140134
}
141135
return nil
142136
},
143137
Unsorted: true,
144138
})
145139
if err != nil {
146-
return nil, err
140+
return err
147141
}
148-
return found, nil
142+
return nil
149143
}
150144

151145
// SearchCommit searches all files in the tree of a given commit
152-
func SearchCommit(commit *object.Commit) (Comments, error) {
153-
found := make(Comments, 0)
154-
t, err := commit.Tree()
155-
if err != nil {
156-
return nil, err
157-
}
158-
146+
func SearchCommit(commit *object.Commit, cb func(*Comment)) error {
159147
var wg sync.WaitGroup
160148
errs := make(chan error)
161149

162-
fileIter := t.Files()
150+
fileIter, err := commit.Files()
151+
if err != nil {
152+
return err
153+
}
154+
defer fileIter.Close()
163155
fileIter.ForEach(func(file *object.File) error {
164156
if file.Mode.IsFile() {
165157
wg.Add(1)
@@ -171,20 +163,17 @@ func SearchCommit(commit *object.Commit) (Comments, error) {
171163
errs <- err
172164
return
173165
}
174-
c, err := SearchFile(file.Name, r)
166+
err = SearchFile(file.Name, r, cb)
175167
if err != nil {
176168
errs <- err
177169
return
178170
}
179171

180-
for _, comment := range c {
181-
found = append(found, comment)
182-
}
183172
}()
184173
}
185174
return nil
186175
})
187176

188177
wg.Wait()
189-
return found, nil
178+
return nil
190179
}

0 commit comments

Comments
 (0)