Skip to content

Commit 480af4f

Browse files
authored
Merge pull request #29 from tryzniak/feature/highlight-default
Highlight the default target
2 parents 049f7b7 + b15229a commit 480af4f

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

help/help.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
// OutputAllShort outputs all short help representations to the given writer.
1717
func OutputAllShort(r io.Reader, w io.Writer, targets []string) error {
1818
comments, err := getComments(r, targets)
19+
1920
if err != nil {
2021
return err
2122
}
@@ -27,7 +28,7 @@ func OutputAllShort(r io.Reader, w io.Writer, targets []string) error {
2728
continue
2829
}
2930

30-
fmt.Fprintf(w, " %-*s %-s\n", width+2, c.Target, firstLine(c.Value))
31+
printShort(w, c, width)
3132
}
3233

3334
fmt.Fprintf(w, "\n")
@@ -47,7 +48,7 @@ func OutputAllLong(r io.Reader, w io.Writer, targets []string) error {
4748
continue
4849
}
4950

50-
fmt.Fprintf(w, " %-s:\n%-s\n\n", c.Target, indent(indent(c.Value)))
51+
printVerbose(w, c)
5152
}
5253

5354
fmt.Fprintf(w, "\n")
@@ -57,6 +58,7 @@ func OutputAllLong(r io.Reader, w io.Writer, targets []string) error {
5758
// getComments parses, filters, and sorts all comment nodes.
5859
func getComments(r io.Reader, targets []string) ([]parser.Comment, error) {
5960
nodes, err := parser.ParseRecursive(r, "/usr/local/include")
61+
6062
if err != nil {
6163
return nil, errors.Wrap(err, "parsing")
6264
}
@@ -117,3 +119,20 @@ func firstLine(s string) string {
117119
func indent(s string) string {
118120
return strings.Replace(" "+s, "\n", "\n ", -1)
119121
}
122+
123+
func printVerbose(w io.Writer, c parser.Comment) (int, error) {
124+
if c.Default {
125+
c.Value = c.Value + " (default)"
126+
}
127+
128+
return fmt.Fprintf(w, " %-s:\n%-s\n\n", c.Target, indent(indent(c.Value)))
129+
}
130+
131+
func printShort(w io.Writer, c parser.Comment, width int) (int, error) {
132+
comment := firstLine(c.Value)
133+
if c.Default {
134+
comment = comment + " (default)"
135+
}
136+
137+
return fmt.Fprintf(w, " %-*s %-s\n", width+2, c.Target, comment)
138+
}

parser/comment.go

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

33
// Comment node.
44
type Comment struct {
5-
Target string
6-
Value string
5+
Target string
6+
Value string
7+
Default bool
78
}

parser/funcs.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,37 @@ func Parse(r io.Reader) ([]Node, error) {
1616
// ParseRecursive parses the given input recursively
1717
// relative to the given dir such as /usr/local/include.
1818
func ParseRecursive(r io.Reader, dir string) ([]Node, error) {
19+
nodes, err := parseRecursiveHelper(r, dir)
20+
21+
for i, _ := range nodes {
22+
defaultComment, ok := nodes[i].(Comment)
23+
if !ok {
24+
continue
25+
}
26+
27+
defaultComment.Default = true
28+
nodes[i] = Comment{
29+
Target: defaultComment.Target,
30+
Value: defaultComment.Value,
31+
Default: true,
32+
}
33+
break
34+
}
35+
36+
return nodes, err
37+
}
38+
39+
func parseRecursiveHelper(r io.Reader, dir string) ([]Node, error) {
1940
nodes, err := Parse(r)
41+
2042
if err != nil {
2143
return nil, errors.Wrap(err, "parsing")
2244
}
2345

46+
otherNodes := []Node{}
2447
for _, n := range nodes {
48+
otherNodes = append(otherNodes, n)
49+
2550
inc, ok := n.(Include)
2651

2752
if !ok {
@@ -34,15 +59,16 @@ func ParseRecursive(r io.Reader, dir string) ([]Node, error) {
3459
return nil, errors.Wrapf(err, "opening %q", path)
3560
}
3661

37-
more, err := ParseRecursive(f, dir)
62+
more, err := parseRecursiveHelper(f, dir)
63+
3864
if err != nil {
3965
return nil, errors.Wrapf(err, "parsing %q", path)
4066
}
4167

42-
nodes = append(nodes, more...)
68+
otherNodes = append(otherNodes, more...)
4369

4470
f.Close()
4571
}
4672

47-
return nodes, nil
73+
return otherNodes, nil
4874
}

parser/parser_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ dummy:
6464

6565
// Output:
6666
// parser.Include{Value:"github.com/tj/foo"}
67-
// parser.Comment{Target:"", Value:"Stuff here:\n\n :)"}
68-
// parser.Comment{Target:"start", Value:"Start the dev server."}
69-
// parser.Comment{Target:"api", Value:"Start the API server."}
70-
// parser.Comment{Target:"deps", Value:"Display dependency graph."}
71-
// parser.Comment{Target:"size", Value:"Display size of dependencies.\n\n- foo\n- bar\n- baz"}
72-
// parser.Comment{Target:"dummy", Value:"Just a comment.\nJust another comment."}
67+
// parser.Comment{Target:"", Value:"Stuff here:\n\n :)", Default:false}
68+
// parser.Comment{Target:"start", Value:"Start the dev server.", Default:false}
69+
// parser.Comment{Target:"api", Value:"Start the API server.", Default:false}
70+
// parser.Comment{Target:"deps", Value:"Display dependency graph.", Default:false}
71+
// parser.Comment{Target:"size", Value:"Display size of dependencies.\n\n- foo\n- bar\n- baz", Default:false}
72+
// parser.Comment{Target:"dummy", Value:"Just a comment.\nJust another comment.", Default:false}
7373
}
7474

7575
func ExampleParser_Parse_withoutComments() {

0 commit comments

Comments
 (0)