Skip to content

Commit 84fbb2b

Browse files
committed
✨ TypeScript 2.3 RC
📚 example with Async generators & iterators
1 parent 69281ab commit 84fbb2b

File tree

9 files changed

+110
-143
lines changed

9 files changed

+110
-143
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ import {Banner, Gauge, Spinner, Sparkline, Progress, Line, LineBuffer} from 'no
3939
})();
4040
```
4141

42+
```ts
43+
async function* getProgressSlowly() {
44+
let progress = 0;
45+
46+
while (progress <= 100) {
47+
await sleep(50);
48+
progress += Math.round(Math.random() * 5);
49+
if (progress >= 100) {
50+
yield 100;
51+
} else {
52+
yield progress;
53+
}
54+
}
55+
}
56+
57+
async function startDownload() {
58+
let myProgress = new Progress(50, chalk.green('Finished download!'));
59+
60+
for await (let progress of getProgressSlowly()) {
61+
myProgress.update(progress, 100)
62+
}
63+
}
64+
65+
(async () => {
66+
console.log('Starting downloads...');
67+
for (let i = 0; i < 10; i ++) {
68+
startDownload()
69+
}
70+
})();
71+
```
72+
4273
## Developer Setup
4374

4475
### Setup
@@ -52,7 +83,8 @@ yarn run build
5283
```
5384
### Examples
5485
```bash
55-
ts-node examples.js
86+
# ts-node examples.js
87+
ts-node --disableWarnings examples.ts
5688
```
5789

5890
### Test
@@ -65,4 +97,4 @@ yarn run test
6597
yarn run prepublishOnly
6698
cd dist
6799
npm publish
68-
```
100+
```

examples.js renamed to examples.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
require('core-js/es7/symbol.js');
12
import * as chalk from 'chalk';
23
import * as os from 'os';
34
import {Banner, Gauge, Spinner, Sparkline, Progress, Line, LineBuffer} from './src/index'
45
require('draftlog').into(console);
56

7+
export function sleep(ms) {
8+
return new Promise(resolve => setTimeout(resolve, ms));
9+
}
610

711
/********************/
812

9-
var total = os.totalmem();
10-
var free = os.freemem();
11-
var used = total - free;
12-
var human = Math.ceil(used / 1000000) + ' MB';
13+
const total = os.totalmem();
14+
const free = os.freemem();
15+
const used = total - free;
16+
const human = Math.ceil(used / 1000000) + ' MB';
1317
console.log('' + new Gauge(used, total, 20, total * 0.8, chalk.bold.grey(human)));
1418

1519
/********************/
@@ -74,7 +78,7 @@ const header = new Line(outputBuffer)
7478
.store();
7579

7680
let line;
77-
for(var l = 0; l < 20; l++)
81+
for(let l = 0; l < 20; l++)
7882
{
7983
line = new Line(outputBuffer)
8084
.column((Math.random()*100).toFixed(3), 20)
@@ -87,53 +91,60 @@ for(var l = 0; l < 20; l++)
8791

8892
outputBuffer.output();
8993

90-
91-
/********************/
92-
93-
const countdown = new Spinner('Exiting in 10 seconds... ');
94-
95-
countdown.start(chalk.bgYellow.blue);
96-
97-
let number = 10;
98-
setInterval(function () {
99-
number--;
100-
countdown.message = 'Exiting in ' + number + ' seconds... ';
101-
if (number === 0) {
102-
process.stdout.write('\n');
103-
process.exit(0);
104-
}
105-
}, 1000);
106-
107-
10894
/********************/
10995

11096
const banner = new Banner(' Node UI is Awesome! See what you can build with this module ');
11197

11298
/********************/
11399

100+
async function* getProgressSlowly() {
101+
let progress = 0;
114102

115-
function startDownload() {
116-
let progress = 0
103+
while (progress <= 100) {
104+
await sleep(50);
105+
progress += Math.round(Math.random() * 5);
106+
if (progress >= 100) {
107+
yield 100;
108+
} else {
109+
yield progress;
110+
}
111+
}
112+
}
113+
114+
async function startDownload() {
115+
// let progress = 0;
117116
// let myProgress = new Progress();
118117
// let myProgress = new Progress(50, chalk.green('Finished download!'), chalk.red('|'), '-');
119118
let myProgress = new Progress(50, chalk.green('Finished download!'));
120-
let interval = setInterval( () => {
121-
progress += Math.round(Math.random() * 5)
122-
if(progress > 100) {
123-
myProgress.update(100, 100)
124-
clearInterval(interval)
125-
} else {
126-
myProgress.update(progress, 100)
127-
}
128-
}, 50)
119+
120+
for await (let progress of getProgressSlowly()) {
121+
myProgress.update(progress, 100)
122+
}
129123
}
130124

131125
(async () => {
132-
console.log('Starting downloads...')
126+
console.log('Starting downloads...');
133127
for (let i = 0; i < 10; i ++) {
134128
startDownload()
135129
// await sleep(100)
136130
}
137131
})();
138132

133+
134+
/********************/
135+
136+
const countdown = new Spinner('Exiting in 10 seconds... ');
137+
138+
countdown.start(chalk.bgYellow.blue);
139+
140+
let number = 20;
141+
setInterval(function () {
142+
number--;
143+
countdown.message = 'Exiting in ' + number + ' seconds... ';
144+
if (number === 0) {
145+
process.stdout.write('\n');
146+
process.exit(0);
147+
}
148+
}, 1000);
149+
139150
/********************/

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodeui",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Command Line UI components for Node.js",
55
"keywords": [
66
"UI",
@@ -27,7 +27,7 @@
2727
"license": "MIT",
2828
"scripts": {
2929
"prebuild": "node tools/delete.js",
30-
"build": "tsc --p src/tsconfig.app.json && tsc --p src/tsconfig.esm.json",
30+
"build": "tsc --p src/tsconfig.app.json --lib esnext && tsc --p src/tsconfig.esm.json --lib esnext",
3131
"prepublishOnly": "yarn run build && node tools/copy.js",
3232
"test": "ts-node test.ts"
3333
},
@@ -39,8 +39,10 @@
3939
"@types/chalk": "^0.4.31",
4040
"@types/jasmine": "^2.5.47",
4141
"@types/node": "^7.0.12",
42+
"core-js": "^2.4.1",
4243
"fs-p": "^2.0.0",
4344
"jasmine": "^2.5.3",
44-
"jasmine-spec-reporter": "^3.2.0"
45+
"jasmine-spec-reporter": "^4.0.0",
46+
"typescript": "^2.3.0"
4547
}
4648
}

src/line.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import * as chalk from "chalk";
33
export class Line {
44
private lineContent = "";
55

6-
constructor(public defaultBuffer) {
6+
constructor(public defaultBuffer?) {
77
}
88

99

1010
// Put text in the line
11-
text(text, styles) {
11+
text(text, styles?) {
1212
if(styles) {
1313
styles.forEach(function (element) {
1414
text = element(text);
@@ -19,7 +19,7 @@ export class Line {
1919
};
2020

2121
// Put padding in the line.
22-
padding(width, styles) {
22+
padding(width, styles?) {
2323
let padding = Array(width + 1).join(" ");
2424
if(styles) {
2525
styles.forEach(function (element) {
@@ -31,7 +31,7 @@ export class Line {
3131
};
3232

3333
// Put padding in the line.
34-
column(text, columnWidth, textStyles) {
34+
column(text, columnWidth, textStyles?) {
3535
let textWidth = chalk.stripColor(text).length;
3636

3737
if (textWidth > columnWidth) {
@@ -48,15 +48,15 @@ export class Line {
4848
}
4949

5050
// Fill the rest of the width of the line with space.
51-
fill(styles) {
51+
fill(styles?) {
5252
let fillWidth = process.stdout.columns - chalk.stripColor(this.lineContent).length;
5353
if (fillWidth > 0)
5454
this.padding(fillWidth, styles);
5555
return this;
5656
}
5757

5858
// Store a line in a line buffer to be output later.
59-
store(buffer) {
59+
store(buffer?) {
6060
if (typeof buffer == 'undefined') {
6161
if (typeof this.defaultBuffer == 'undefined')
6262
process.stderr.write('Attempt to store a line in a line buffer, without providing a line buffer to store that line in.');

src/tsconfig.app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"allowJs": false
88
},
99
"exclude": [
10-
"**/*.spec.ts"
10+
"**/*.spec.ts",
11+
"../examples.ts"
1112
]
1213
}

src/tsconfig.esm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"allowJs": false
88
},
99
"exclude": [
10-
"**/*.spec.ts"
10+
"**/*.spec.ts",
11+
"../examples.ts"
1112
]
1213
}

test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import SuiteInfo = jasmine.SuiteInfo;
55

66

77
class CustomProcessor extends DisplayProcessor {
8-
public displayJasmineStarted(info: SuiteInfo, log: String): String {
8+
public displayJasmineStarted(info: SuiteInfo, log: string): string {
99
return `TypeScript ${log}`;
1010
}
1111
}

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "ESNext",
4+
"target": "es2015",
55
"noImplicitAny": false,
66
"sourceMap": true,
77
"allowJs": true,
@@ -11,10 +11,11 @@
1111
"outDir": "dist"
1212
},
1313
"lib" : [
14-
"ESNext"
14+
"esnext"
1515
],
1616
"include": [
17-
"src/**/*.ts"
17+
"src/**/*.ts",
18+
"examples.ts"
1819
],
1920
"exclude": [
2021
"dist",

0 commit comments

Comments
 (0)