Skip to content

Commit 4c25abf

Browse files
authored
Level flg of xtree (#358)
* Add level flg for xtree * fix test
1 parent ae1b29c commit 4c25abf

3 files changed

Lines changed: 98 additions & 10 deletions

File tree

README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ $ go install github.com/ddddddO/gtree/cmd/xtree@latest
731731
```console
732732
$ xtree --help
733733
NAME:
734-
xtree - This CLI uses {JSON|YAML|TOML} to generate directory tree
734+
xtree - This CLI uses {JSON|YAML|TOML} to generate ASCII tree
735735

736736
USAGE:
737737
xtree [global options] [command [command options]]
@@ -758,9 +758,10 @@ USAGE:
758758
xtree output [options]
759759

760760
OPTIONS:
761-
--omit-index, --omit, -o set this option when you do not want to display array indices.
762-
--allow-duplicate, -a set this option when you want to allow duplicate node names at the same level.
763-
--help, -h show help
761+
--omit-index, --omit, -o set this option when you do not want to display array indices.
762+
--allow-duplicate, -a set this option when you want to allow duplicate node names at the same level.
763+
--level int, -l int set this option when you want to specify the depth of the tree.
764+
--help, -h show help
764765
```
765766

766767
#### JSON to tree
@@ -927,6 +928,73 @@ $ cat a.yaml | xtree output
927928
└── dark
928929
```
929930

931+
### Omit index flg
932+
```console
933+
$ cat a.json | xtree output --omit-index
934+
.
935+
├── age
936+
│ └── 30
937+
├── devices
938+
│ ├── os
939+
│ │ ├── ios
940+
│ │ └── windows
941+
│ └── type
942+
│ ├── mobile
943+
│ └── desktop
944+
├── height
945+
│ └── 175.5
946+
├── is_active
947+
│ └── true
948+
├── metadata
949+
│ └── <nil>
950+
├── name
951+
│ └── Alice
952+
├── roles
953+
│ ├── admin
954+
│ └── editor
955+
└── settings
956+
├── notifications
957+
│ └── true
958+
└── theme
959+
└── dark
960+
```
961+
962+
### Level flg
963+
```console
964+
$ cat a.json | xtree output --level 1
965+
.
966+
├── age
967+
├── devices
968+
├── height
969+
├── is_active
970+
├── metadata
971+
├── name
972+
├── roles
973+
└── settings
974+
975+
$ cat a.json | xtree output --level 2 --omit-index
976+
.
977+
├── age
978+
│ └── 30
979+
├── devices
980+
│ ├── os
981+
│ └── type
982+
├── height
983+
│ └── 175.5
984+
├── is_active
985+
│ └── true
986+
├── metadata
987+
│ └── <nil>
988+
├── name
989+
│ └── Alice
990+
├── roles
991+
│ ├── admin
992+
│ └── editor
993+
└── settings
994+
├── notifications
995+
└── theme
996+
```
997+
930998
# Process
931999

9321000
> [!NOTE]

cmd/xtree/main.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
func main() {
2626
app := &cli.Command{
2727
Name: "xtree",
28-
Usage: "This CLI uses {JSON|YAML|TOML} to generate directory tree",
28+
Usage: "This CLI uses {JSON|YAML|TOML} to generate ASCII tree",
2929
Version: fmt.Sprintf("%s / revision %s", Version, Revision),
3030
Commands: []*cli.Command{
3131
{
@@ -44,6 +44,12 @@ func main() {
4444
Aliases: []string{"a"},
4545
Usage: "set this option when you want to allow duplicate node names at the same level.",
4646
},
47+
&cli.IntFlag{
48+
Name: "level",
49+
Aliases: []string{"l"},
50+
Usage: "set this option when you want to specify the depth of the tree.",
51+
HideDefault: true,
52+
},
4753
},
4854
Before: notExistArgs,
4955
Action: actionOutput,
@@ -72,11 +78,15 @@ func actionOutput(ctx context.Context, c *cli.Command) error {
7278
root = gtree.NewRoot(".", gtree.WithDuplicationAllowed())
7379
}
7480
omitIndex := c.Bool("omit-index")
81+
level := c.Int("level")
82+
if c.IsSet("level") && level <= 0 {
83+
return errors.New("invalid level, must be greater than 0.")
84+
}
7585

76-
return output(os.Stdout, os.Stdin, root, omitIndex)
86+
return output(os.Stdout, os.Stdin, root, omitIndex, level)
7787
}
7888

79-
func output(w io.Writer, r io.Reader, root *gtree.Node, omitIndex bool) error {
89+
func output(w io.Writer, r io.Reader, root *gtree.Node, omitIndex bool, level int) error {
8090
input, err := io.ReadAll(r)
8191
if err != nil {
8292
return err
@@ -87,12 +97,20 @@ func output(w io.Writer, r io.Reader, root *gtree.Node, omitIndex bool) error {
8797
}
8898
walk(root, "", data, omitIndex)
8999

100+
limit := level + 1
90101
for wn, err := range gtree.WalkIterFromRoot(root) {
91102
if err != nil {
92103
return err
93104
}
94105

95-
fmt.Fprintln(w, wn.Row())
106+
if level == 0 {
107+
fmt.Fprintln(w, wn.Row())
108+
continue
109+
}
110+
if wn.Level() <= uint(limit) {
111+
fmt.Fprintln(w, wn.Row())
112+
continue
113+
}
96114
}
97115

98116
return nil

cmd/xtree/main_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestOutput(t *testing.T) {
7373
inputData io.Reader
7474
inputRoot *gtree.Node
7575
inputOmitIndex bool
76+
inputLevel int
7677
want string
7778
wantErr error
7879
}{
@@ -537,7 +538,7 @@ func TestOutput(t *testing.T) {
537538
t.Parallel()
538539

539540
ret := &bytes.Buffer{}
540-
gotErr := output(ret, tt.inputData, tt.inputRoot, tt.inputOmitIndex)
541+
gotErr := output(ret, tt.inputData, tt.inputRoot, tt.inputOmitIndex, tt.inputLevel)
541542
gotOutput := ret.String()
542543

543544
if gotErr != nil {
@@ -559,6 +560,7 @@ func TestOutput_multiRow(t *testing.T) {
559560
inputData io.Reader
560561
inputRoot *gtree.Node
561562
inputOmitIndex bool
563+
inputLevel int
562564
want string
563565
wantErr error
564566
}{
@@ -633,7 +635,7 @@ config:
633635
t.Parallel()
634636

635637
ret := &bytes.Buffer{}
636-
gotErr := output(ret, tt.inputData, tt.inputRoot, tt.inputOmitIndex)
638+
gotErr := output(ret, tt.inputData, tt.inputRoot, tt.inputOmitIndex, tt.inputLevel)
637639
gotOutput := ret.String()
638640

639641
if gotErr != nil {

0 commit comments

Comments
 (0)