Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

feat: add tree #57

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,27 @@ Waits for 1 second or given milliseconds
await cli.wait()
await cli.wait(3000)
```

# cli.tree

Generate a tree and display it

```typescript
let tree = cli.tree()
tree.insert('foo')
tree.insert('bar')

let subtree = cli.tree()
subtree.insert('qux')
tree.nodes.bar.insert('baz', subtree)

tree.display()
```

Outputs:
```shell
├─ foo
└─ bar
└─ baz
└─ qux
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"semver": "^5.5.1",
"strip-ansi": "^4.0.0",
"supports-color": "^5.5.0",
"supports-hyperlinks": "^1.0.1"
"supports-hyperlinks": "^1.0.1",
"treeify": "^1.1.0"
},
"devDependencies": {
"@oclif/tslint": "^3.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const deps = {
get styledHeader(): typeof import ('./styled/header').default { return fetch('./styled/header').default },
get styledJSON(): typeof import ('./styled/json').default { return fetch('./styled/json').default },
get table(): typeof import ('./styled/table').default { return fetch('./styled/table').default },
get tree(): typeof import ('./styled/tree').default { return fetch('./styled/tree').default },
get wait(): typeof import ('./wait').default { return fetch('./wait').default },
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ux = {
get styledHeader() { return deps.styledHeader },
get styledJSON() { return deps.styledJSON },
get table() { return deps.table },
get tree() { return deps.tree },
get open() { return deps.open },
get wait() { return deps.wait },

Expand Down
40 changes: 40 additions & 0 deletions src/styled/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const treeify = require('treeify')

export class Tree {
nodes: { [key: string]: Tree } = {}
constructor() { }

insert(child: string, value: Tree = new Tree()): Tree {
this.nodes[child] = value
return this
}

search(key: string): Tree | undefined {
for (let child of Object.keys(this.nodes)) {
if (child === key) {
return this.nodes[child]
} else {
let c = this.nodes[child].search(key)
if (c) return c
}
}
}

// tslint:disable-next-line:no-console
display(logger: any= console.log) {
const addNodes = function (nodes: any) {
let tree: { [key: string]: any } = {}
for (let p of Object.keys(nodes)) {
tree[p] = addNodes(nodes[p].nodes)
}
return tree
}

let tree = addNodes(this.nodes)
logger(treeify.asTree(tree))
}
}

export default function tree() {
return new Tree()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong, but this looks like it makes a singleton tree. Can you use cli.tree() multiple times without it displaying the information from the previous tree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, cli.tree() just instantiates a new Tree. Also, Tree is exported if folks want the ol'fashion'd way.

}
25 changes: 25 additions & 0 deletions test/styled/tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {expect, fancy} from 'fancy-test'

import cli from '../../src'

describe('styled/tree', () => {
fancy
.stdout()
.end('shows the tree', output => {
let tree = cli.tree()
tree.insert('foo')
tree.insert('bar')

let subtree = cli.tree()
subtree.insert('qux')
tree.nodes.bar.insert('baz', subtree)

tree.display()
expect(output.stdout).to.equal(`├─ foo
└─ bar
└─ baz
└─ qux

`)
})
})
Loading