-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.go
More file actions
81 lines (62 loc) · 1.28 KB
/
ls.go
File metadata and controls
81 lines (62 loc) · 1.28 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
package main
import (
"flag"
"fmt"
"strings"
"unicode/utf8"
)
var LsCmd = &Command{
Usage: "ls [-n] [-wc] <file>",
Short: "list chapters",
Long: `List the chapters in the given manuscript file.
The -n flag will display chapter numbers.
The -wc flag will print the word count of each chapter.`,
Run: lsCmd,
}
func lsCmd(cmd *Command, args []string) error {
var (
number bool
wc bool
)
fs := flag.NewFlagSet(cmd.Argv0, flag.ExitOnError)
fs.BoolVar(&number, "n", false, "display chapter number")
fs.BoolVar(&wc, "wc", false, "display word count of the chapters")
fs.Parse(args)
args = fs.Args()
if len(args) == 0 {
return ErrUsage
}
ms, err := ParseManuscript(args[0])
if err != nil {
return err
}
chapters := ms.Chapters()
pad := 0
for _, ch := range chapters {
title := ch.Title()
if title == "" {
title = ch.Number()
}
if l := utf8.RuneCountInString(title); l > pad {
pad = l
}
}
for i, ch := range chapters {
if number {
fmt.Printf("%3d ", i+1)
}
title := ch.Title()
if title == "" {
title = ch.Number()
}
fmt.Print(title)
if wc {
if n := pad - utf8.RuneCountInString(title); n > 0 {
fmt.Print(strings.Repeat(" ", n))
}
fmt.Printf(" %6s", formatNumber(ch.WordCount()))
}
fmt.Println()
}
return nil
}