Skip to content

Commit 5a044ed

Browse files
authored
Change show-index to omit-index flg (#357)
1 parent 5cbd689 commit 5a044ed

4 files changed

Lines changed: 304 additions & 107 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
# args: --issues-exit-code=0
2626

2727
- name: test # testだけ各OSで実行
28-
run: go test . ./markdown/... -race -coverprofile=coverage.out -covermode=atomic -v
28+
run: go test . ./markdown/... ./cmd/xtree/... -race -coverprofile=coverage.out -covermode=atomic -v
2929

3030
- name: upload coverage to Codecov
3131
if: runner.os == 'Linux'

README.md

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@ USAGE:
757757
xtree output [options]
758758

759759
OPTIONS:
760-
--show-index, --index, -i set this option when you want to display array element numbers (indices).
761-
--allow-duplicate, -a set this option when you want to allow duplicate node names at the same level
760+
--omit-index, --omit, -o set this option when you do not want to display array indices.
761+
--allow-duplicate, -a set this option when you want to allow duplicate node names at the same level.
762762
--help, -h show help
763763
```
764764

@@ -787,12 +787,16 @@ $ cat a.json | xtree output
787787
├── age
788788
│ └── 30
789789
├── devices
790-
│ ├── os
791-
│ │ ├── ios
792-
│ │ └── windows
793-
│ └── type
794-
│ ├── mobile
795-
│ └── desktop
790+
│ ├── [0]
791+
│ │ ├── os
792+
│ │ │ └── ios
793+
│ │ └── type
794+
│ │ └── mobile
795+
│ └── [1]
796+
│ ├── os
797+
│ │ └── windows
798+
│ └── type
799+
│ └── desktop
796800
├── height
797801
│ └── 175.5
798802
├── is_active
@@ -802,8 +806,10 @@ $ cat a.json | xtree output
802806
├── name
803807
│ └── Alice
804808
├── roles
805-
│ ├── admin
806-
│ └── editor
809+
│ ├── [0]
810+
│ │ └── admin
811+
│ └── [1]
812+
│ └── editor
807813
└── settings
808814
├── notifications
809815
│ └── true
@@ -837,21 +843,27 @@ $ cat a.toml | xtree output
837843
├── age
838844
│ └── 30
839845
├── devices
840-
│ ├── os
841-
│ │ ├── ios
842-
│ │ └── windows
843-
│ └── type
844-
│ ├── mobile
845-
│ └── desktop
846+
│ ├── [0]
847+
│ │ ├── os
848+
│ │ │ └── ios
849+
│ │ └── type
850+
│ │ └── mobile
851+
│ └── [1]
852+
│ ├── os
853+
│ │ └── windows
854+
│ └── type
855+
│ └── desktop
846856
├── height
847857
│ └── 175.5
848858
├── is_active
849859
│ └── true
850860
├── name
851861
│ └── Alice
852862
├── roles
853-
│ ├── admin
854-
│ └── editor
863+
│ ├── [0]
864+
│ │ └── admin
865+
│ └── [1]
866+
│ └── editor
855867
└── settings
856868
├── notifications
857869
│ └── true
@@ -884,12 +896,16 @@ $ cat a.yaml | xtree output
884896
├── age
885897
│ └── 30
886898
├── devices
887-
│ ├── os
888-
│ │ ├── ios
889-
│ │ └── windows
890-
│ └── type
891-
│ ├── mobile
892-
│ └── desktop
899+
│ ├── [0]
900+
│ │ ├── os
901+
│ │ │ └── ios
902+
│ │ └── type
903+
│ │ └── mobile
904+
│ └── [1]
905+
│ ├── os
906+
│ │ └── windows
907+
│ └── type
908+
│ └── desktop
893909
├── height
894910
│ └── 175.5
895911
├── is_active
@@ -899,8 +915,10 @@ $ cat a.yaml | xtree output
899915
├── name
900916
│ └── Alice
901917
├── roles
902-
│ ├── admin
903-
│ └── editor
918+
│ ├── [0]
919+
│ │ └── admin
920+
│ └── [1]
921+
│ └── editor
904922
└── settings
905923
├── notifications
906924
│ └── true

cmd/xtree/main.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func main() {
3535
"Let's try 'cat {.json|.yaml|.toml} | xtree output'.",
3636
Flags: []cli.Flag{
3737
&cli.BoolFlag{
38-
Name: "show-index",
39-
Aliases: []string{"index", "i"},
40-
Usage: "set this option when you want to display array element numbers (indices).",
38+
Name: "omit-index",
39+
Aliases: []string{"omit", "o"},
40+
Usage: "set this option when you do not want to display array indices.",
4141
},
4242
&cli.BoolFlag{
4343
Name: "allow-duplicate",
4444
Aliases: []string{"a"},
45-
Usage: "set this option when you want to allow duplicate node names at the same level",
45+
Usage: "set this option when you want to allow duplicate node names at the same level.",
4646
},
4747
},
4848
Before: notExistArgs,
@@ -71,12 +71,12 @@ func actionOutput(ctx context.Context, c *cli.Command) error {
7171
if c.Bool("allow-duplicate") {
7272
root = gtree.NewRoot(".", gtree.WithDuplicationAllowed())
7373
}
74-
showIndex := c.Bool("show-index")
74+
omitIndex := c.Bool("omit-index")
7575

76-
return output(os.Stdout, os.Stdin, root, showIndex)
76+
return output(os.Stdout, os.Stdin, root, omitIndex)
7777
}
7878

79-
func output(w io.Writer, r io.Reader, root *gtree.Node, showIndex bool) error {
79+
func output(w io.Writer, r io.Reader, root *gtree.Node, omitIndex bool) error {
8080
input, err := io.ReadAll(r)
8181
if err != nil {
8282
return err
@@ -85,7 +85,7 @@ func output(w io.Writer, r io.Reader, root *gtree.Node, showIndex bool) error {
8585
if err != nil {
8686
return err
8787
}
88-
walk(root, "", data, showIndex)
88+
walk(root, "", data, omitIndex)
8989

9090
return gtree.OutputFromRoot(w, root)
9191
}
@@ -106,7 +106,7 @@ func parseData(data []byte) (any, error) {
106106
return nil, errors.New("data is in an unsupported format or is invalid")
107107
}
108108

109-
func walk(parent *gtree.Node, key string, value any, showIndex bool) {
109+
func walk(parent *gtree.Node, key string, value any, omitIndex bool) {
110110
switch v := value.(type) {
111111
case map[string]any:
112112
node := parent
@@ -121,7 +121,7 @@ func walk(parent *gtree.Node, key string, value any, showIndex bool) {
121121
sort.Strings(keys)
122122

123123
for _, k := range keys {
124-
walk(node, k, v[k], showIndex)
124+
walk(node, k, v[k], omitIndex)
125125
}
126126

127127
case map[any]any:
@@ -130,7 +130,7 @@ func walk(parent *gtree.Node, key string, value any, showIndex bool) {
130130
node = parent.Add(key)
131131
}
132132
for k, val := range v {
133-
walk(node, fmt.Sprintf("%v", k), val, showIndex)
133+
walk(node, fmt.Sprintf("%v", k), val, omitIndex)
134134
}
135135

136136
case []any:
@@ -140,11 +140,11 @@ func walk(parent *gtree.Node, key string, value any, showIndex bool) {
140140
}
141141

142142
for i, item := range v {
143-
if showIndex {
144-
indexNode := node.Add(fmt.Sprintf("[%d]", i))
145-
walk(indexNode, "", item, showIndex)
143+
if omitIndex {
144+
walk(node, "", item, omitIndex)
146145
} else {
147-
walk(node, "", item, showIndex)
146+
indexNode := node.Add(fmt.Sprintf("[%d]", i))
147+
walk(indexNode, "", item, omitIndex)
148148
}
149149
}
150150

0 commit comments

Comments
 (0)