-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
cmd_list.go
60 lines (53 loc) · 1.06 KB
/
cmd_list.go
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
const (
doneMark1 = "\u2610"
doneMark2 = "\u2611"
)
func makeCmdList(filename string) *commander.Command {
cmdList := func(cmd *commander.Command, args []string) error {
nflag := cmd.Flag.Lookup("n").Value.Get().(bool)
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
br := bufio.NewReader(f)
n := 1
for {
b, _, err := br.ReadLine()
if err != nil {
if err != io.EOF {
return err
}
break
}
line := string(b)
if strings.HasPrefix(line, "-") {
if !nflag {
fmt.Printf("%s %03d: %s\n", doneMark2, n, strings.TrimSpace(line[1:]))
}
} else {
fmt.Printf("%s %03d: %s\n", doneMark1, n, strings.TrimSpace(line))
}
n++
}
return nil
}
flg := *flag.NewFlagSet("list", flag.ExitOnError)
flg.Bool("n", false, "only not done")
return &commander.Command{
Run: cmdList,
UsageLine: "list [options]",
Short: "show list index",
Flag: flg,
}
}