-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
60 lines (36 loc) · 2.47 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { LiveContainer } from 'clui-live';
const delay = ( n : number ) => new Promise<void>( resolve => setTimeout( resolve, n ) );
const cyan = ( text : string ) => `\u001B[46;37m` + text + `\u001B[49;39m`;
const green = ( text : string ) => `\u001B[42;37m` + text + `\u001B[49;39m`;
(async function () {
// Create a container that intercepts any console.logs (this behavior can be disabled calling the `unhook()` method)
const container = new LiveContainer().hook();
// Create a live (updatable) area
const area1 = container.createLiveArea();
area1.write( cyan( ' live: ' ) + ' Hello, I\'m a pice of text that can be updated!' ); await delay( 1000 );
console.log( "Regular console.log..." ); await delay( 1000 );
area1.write( cyan( ' live: ' ) + ' See? I can change myself.' ); await delay( 1000 );
console.log( "Regular console.log 2..." ); await delay( 1000 );
console.log( "Regular console.log 3..." ); await delay( 500 );
area1.write( cyan( ' live: ' ) + ' Wait, what\'s hapening?' );
for ( let i = 4; i <= 30; i++ ) {
console.log( "Regular console.log " + i + "..." );
await delay( Math.max( 250 - ( i - 4 ) * 30, 50 ) );
}
await delay( 500 );
area1.write( cyan( ' live: ' ) + ' Sorry about that. It\'s me again. I scrolled too far away, so I repositioned myself back down.' ); await delay( 2000 );
console.log( "Regular console.log 31..." ); await delay( 2000 );
area1.append( cyan( ' live: ' ) + ' Also, have I told you I can span multiple lines? I automatically adjust the text that\'s beneath me when my line count changes. Pretty neat uh?' ); await delay( 4000 );
const area2 = container.createLiveArea().pin();
area2.append( green( ' pinned: ' ) + " I can also force myself to always stay pinned to the bottom." ); await delay( 1000 );
for ( let i = 30; i <= 50; i++ ) {
console.log( "Regular console.log " + i + "..." );
area2.write( green( ` pinned: ${i}/50 ` ) + ' I can also force myself to always stay pinned to the bottom. Cooooooool.' );
await delay( 200 );
}
await delay( 1000 );
area1.write( cyan( ' live: ' ) + ' When I\'m done I can call `area.close()` to free resources, keeping the last text I wrote on screen.' ); await delay( 2000 );
area1.append( cyan( ' live: ' ) + ' Or I could call `area.clear()` before closing so no one would know I was ever even here. Sneaky.' ); await delay( 2000 );
area1.close();
area2.clear().close();
})();