Skip to content

Commit b6df7a0

Browse files
committed
🎉 Initial commit
0 parents  commit b6df7a0

23 files changed

+1071
-0
lines changed

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
7+
# dependencies
8+
/node_modules
9+
10+
# IDEs and editors
11+
/.idea
12+
.project
13+
.classpath
14+
.c9/
15+
*.launch
16+
.settings/
17+
*.sublime-workspace
18+
19+
# IDE - VSCode
20+
.vscode/*
21+
!.vscode/settings.json
22+
!.vscode/tasks.json
23+
!.vscode/launch.json
24+
!.vscode/extensions.json
25+
26+
# misc
27+
/.sass-cache
28+
/connect.lock
29+
/coverage/*
30+
/libpeerconnection.log
31+
npm-debug.log
32+
testem.log
33+
/typings
34+
35+
# e2e
36+
/e2e/*.js
37+
/e2e/*.map
38+
39+
#System Files
40+
.DS_Store
41+
Thumbs.db
42+
43+
44+
NOTES.md
45+
config/
46+
TODO.md

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2017 Sumanth Chinthagunta
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Node UI
2+
=======
3+
Command Line UI components for Node.js
4+
5+
> This module is based on [clui](https://github.com/nathanpeck/clui) and [draftlog](https://github.com/ivanseidel/node-draftlog)
6+
7+
:: [Setup](#setup)
8+
:: [Usage](#usage)
9+
:: [Test](#test)
10+
:: [Build](#build)
11+
12+
### Dependencies
13+
14+
1. chalk
15+
2. draftlog
16+
17+
### Setup
18+
> Installing this module in your project
19+
```bash
20+
yarn add nodeui
21+
# or via npm
22+
npm install nodeui
23+
```
24+
25+
### Usage
26+
```ts
27+
import * as chalk from 'chalk';
28+
import {Spinner, Gauge, Sparkline, Progress, Line, LineBuffer} from 'nodeui'
29+
(async () => {
30+
try {
31+
let spinner = new Spinner('initial message');
32+
spinner.start();
33+
await sleep(800);
34+
spinner.message = 'still working...';
35+
await sleep(800);
36+
spinner.message = 'almost done...';
37+
await sleep(800);
38+
spinner.stop()
39+
} catch (err) {
40+
console.log('Error:::', err)
41+
}
42+
})();
43+
```
44+
45+
## Developer Setup
46+
47+
### Setup
48+
```bash
49+
yarn
50+
```
51+
52+
### Build
53+
```bash
54+
yarn run build
55+
```
56+
### Examples
57+
```bash
58+
ts-node examples.js
59+
```
60+
61+
### Test
62+
```bash
63+
yarn run test
64+
```
65+
66+
### Publish
67+
```bash
68+
yarn run prepublishOnly
69+
cd dist
70+
npm publish
71+
```

examples.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import * as chalk from 'chalk';
2+
import {Banner, Spinner, Sparkline, Progress, Line, LineBuffer} from './src/index'
3+
require('draftlog').into(console);
4+
5+
6+
7+
const reqsPerSec = [10,12,3,7,12,9,23,10,9,19,16,18,12,12];
8+
9+
console.log(new Sparkline(reqsPerSec, 'reqs/sec').toString());
10+
11+
/********************/
12+
13+
const thisProgressBar = new Progress(20);
14+
console.log(thisProgressBar.update(40, 100));
15+
16+
/********************/
17+
18+
19+
const headers = new Line()
20+
.padding(2)
21+
.column('Column One', 20, [chalk.cyan])
22+
.column('Column Two', 20, [chalk.cyan])
23+
.column('Column Three', 20, [chalk.cyan])
24+
.column('Column Four', 20, [chalk.cyan])
25+
.fill()
26+
.output();
27+
28+
const body = new Line()
29+
.padding(2)
30+
.column((Math.random()*100).toFixed(3), 20)
31+
.column((Math.random()*100).toFixed(3), 20)
32+
.column((Math.random()*100).toFixed(3), 20)
33+
.column((Math.random()*100).toFixed(3), 20)
34+
.fill()
35+
.output();
36+
37+
38+
39+
/********************/
40+
41+
42+
const outputBuffer = new LineBuffer({
43+
x: 0,
44+
y: 0,
45+
width: 'console',
46+
height: 'console'
47+
});
48+
49+
const message = new Line(outputBuffer)
50+
.column('Title Placehole', 20, [chalk.green])
51+
.fill()
52+
.store();
53+
54+
const blankLine = new Line(outputBuffer)
55+
.fill()
56+
.store();
57+
58+
const header = new Line(outputBuffer)
59+
.column('Suscipit', 20, [chalk.cyan])
60+
.column('Voluptatem', 20, [chalk.cyan])
61+
.column('Nesciunt', 20, [chalk.cyan])
62+
.column('Laudantium', 11, [chalk.cyan])
63+
.fill()
64+
.store();
65+
66+
let line;
67+
for(var l = 0; l < 20; l++)
68+
{
69+
line = new Line(outputBuffer)
70+
.column((Math.random()*100).toFixed(3), 20)
71+
.column((Math.random()*100).toFixed(3), 20)
72+
.column((Math.random()*100).toFixed(3), 20)
73+
.column((Math.random()*100).toFixed(3), 11)
74+
.fill()
75+
.store();
76+
}
77+
78+
outputBuffer.output();
79+
80+
81+
/********************/
82+
83+
const countdown = new Spinner('Exiting in 10 seconds... ');
84+
85+
countdown.start(chalk.bgYellow.blue);
86+
87+
let number = 10;
88+
setInterval(function () {
89+
number--;
90+
countdown.message = 'Exiting in ' + number + ' seconds... ';
91+
if (number === 0) {
92+
process.stdout.write('\n');
93+
process.exit(0);
94+
}
95+
}, 1000);
96+
97+
98+
/********************/
99+
100+
const banner = new Banner(' Node UI is Awesome! See what you can build with this module ');
101+
102+
/********************/
103+
104+
105+
function startDownload() {
106+
let progress = 0
107+
// let myProgress = new Progress();
108+
// let myProgress = new Progress(50, chalk.green('Finished download!'), chalk.red('|'), '-');
109+
let myProgress = new Progress(50, chalk.green('Finished download!'));
110+
let interval = setInterval( () => {
111+
progress += Math.round(Math.random() * 5)
112+
if(progress > 100) {
113+
myProgress.update(100, 100)
114+
clearInterval(interval)
115+
} else {
116+
myProgress.update(progress, 100)
117+
}
118+
}, 50)
119+
}
120+
121+
(async () => {
122+
console.log('Starting downloads...')
123+
for (let i = 0; i < 10; i ++) {
124+
startDownload()
125+
// await sleep(100)
126+
}
127+
})();
128+
129+
/********************/

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "nodeui",
3+
"version": "0.1.0",
4+
"description": "Command Line UI components for Node.js",
5+
"main": "index.js",
6+
"author": "Sumanth Chinthagunta",
7+
"license": "MIT",
8+
"scripts": {
9+
"prebuild": "node tools/delete.js",
10+
"build": "tsc --p src/tsconfig.app.json",
11+
"prepublishOnly": "yarn run build && node tools/copy.js",
12+
"test": "ts-node test.ts"
13+
},
14+
"dependencies": {
15+
"chalk": "^1.1.3",
16+
"draftlog": "^1.0.12"
17+
},
18+
"devDependencies": {
19+
"@types/chalk": "^0.4.31",
20+
"@types/jasmine": "^2.5.47",
21+
"@types/node": "^7.0.12",
22+
"fs-p": "^2.0.0",
23+
"jasmine": "^2.5.3",
24+
"jasmine-spec-reporter": "^3.2.0"
25+
}
26+
}

src/banner.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as chalk from "chalk";
2+
import {ChalkChain} from "chalk";
3+
4+
export class Banner {
5+
6+
constructor(public text, color: ChalkChain = chalk.yellow) {
7+
const words = text.split('');
8+
console.log(chalk.dim('*'.repeat(words.length)));
9+
const update = console.draft();
10+
console.log(chalk.dim('*'.repeat(words.length)));
11+
12+
setInterval( () => {
13+
words.push(words.shift());
14+
update(color(words.join('')));
15+
}, 100)
16+
}
17+
}

src/gauge.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as chalk from 'chalk';
2+
import {Gauge} from "./gauge";
3+
4+
describe('Gauge', () => {
5+
6+
let gauge: Gauge;
7+
8+
beforeEach(function(done) {
9+
const total = 2000;
10+
const free = 500;
11+
const used = total - free;
12+
const human = used + ' MB';
13+
gauge = new Gauge(used, total, 20, total * 0.8, chalk.bold.grey(human));
14+
done();
15+
});
16+
17+
it('when value is less then dangerZone, it should be green', () => {
18+
let expected = `[${chalk.green('|'.repeat(15))}${'-'.repeat(6)}] ${chalk.bold.grey('1500 MB')}`;
19+
expect(gauge.toString()).toEqual(expected);
20+
});
21+
22+
it('when value is more then dangerZone, it should be red', () => {
23+
gauge.value = 1800;
24+
let expected = `[${chalk.red('|'.repeat(18))}${'-'.repeat(3)}] ${chalk.bold.grey('1500 MB')}`;
25+
expect(gauge.toString()).toEqual(expected);
26+
});
27+
});

src/gauge.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as chalk from "chalk";
2+
3+
export class Gauge {
4+
5+
constructor(public value, public maxValue, public width, public dangerZone, public suffix) {
6+
7+
}
8+
9+
toString() {
10+
if (this.maxValue === 0) {
11+
return '[]';
12+
}
13+
else {
14+
let barLength = Math.ceil(this.value / this.maxValue * this.width);
15+
if (barLength > this.width)
16+
barLength = this.width;
17+
18+
let barColor = chalk.green;
19+
if (this.value > this.dangerZone)
20+
barColor = chalk.red;
21+
22+
return `[${barColor('|'.repeat(barLength))}${'-'.repeat(this.width + 1 - barLength)}] ${this.suffix}`;
23+
}
24+
}
25+
26+
}

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from './gauge'
2+
export * from './spinner'
3+
export * from './progress'
4+
export * from './sparkline'
5+
export * from './line'
6+
export * from './lineBuffer'
7+
export * from './table'
8+
export * from './banner'

0 commit comments

Comments
 (0)