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

Commit

Permalink
feat: add tree (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Oct 10, 2018
1 parent 251fd17 commit 8d742a7
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 1 deletion.
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()
}
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

0 comments on commit 8d742a7

Please sign in to comment.