@figliolia/chalk-animation 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/Animation.js +25 -36
- package/dist/cjs/ChalkAnimation.js +26 -21
- package/dist/cjs/Decor.js +18 -0
- package/dist/cjs/Effects.js +28 -3
- package/dist/cjs/FrameRate.js +26 -0
- package/dist/cjs/InstanceTracker.js +4 -2
- package/dist/cjs/__tests__/ChalkAnimation.test.js +95 -0
- package/dist/cjs/package.json +2 -2
- package/dist/mjs/Animation.js +20 -33
- package/dist/mjs/ChalkAnimation.js +26 -23
- package/dist/mjs/Decor.js +14 -0
- package/dist/mjs/Effects.js +27 -2
- package/dist/mjs/FrameRate.js +24 -0
- package/dist/mjs/InstanceTracker.js +2 -2
- package/dist/mjs/__tests__/ChalkAnimation.test.js +93 -0
- package/dist/mjs/index.js +2 -2
- package/dist/mjs/package.json +3 -2
- package/dist/types/Animation.d.ts +16 -0
- package/dist/types/ChalkAnimation.d.ts +21 -0
- package/dist/types/Decor.d.ts +5 -0
- package/dist/{mjs → types}/Effects.d.ts +2 -2
- package/dist/types/FrameRate.d.ts +11 -0
- package/dist/{mjs → types}/InstanceTracker.d.ts +1 -0
- package/dist/types/__tests__/ChalkAnimation.test.d.ts +1 -0
- package/dist/{cjs → types}/index.d.ts +1 -0
- package/dist/types/types.d.ts +6 -0
- package/package.json +7 -8
- package/src/Animation.ts +20 -34
- package/src/ChalkAnimation.ts +25 -26
- package/src/Decor.ts +24 -0
- package/src/Effects.ts +14 -6
- package/src/FrameRate.ts +28 -0
- package/src/InstanceTracker.ts +5 -3
- package/src/__tests__/ChalkAnimation.test.ts +108 -0
- package/src/index.ts +1 -0
- package/src/types.ts +14 -0
- package/dist/cjs/Animation.d.ts +0 -20
- package/dist/cjs/ChalkAnimation.d.ts +0 -10
- package/dist/cjs/Effects.d.ts +0 -13
- package/dist/cjs/InstanceTracker.d.ts +0 -14
- package/dist/cjs/animateString.d.ts +0 -2
- package/dist/cjs/animateString.js +0 -18
- package/dist/cjs/types.d.ts +0 -1
- package/dist/mjs/Animation.d.ts +0 -20
- package/dist/mjs/ChalkAnimation.d.ts +0 -10
- package/dist/mjs/animateString.d.ts +0 -2
- package/dist/mjs/animateString.js +0 -14
- package/dist/mjs/index.d.ts +0 -4
- package/dist/mjs/types.d.ts +0 -1
- package/src/animateString.ts +0 -15
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ChalkAnimation } from "../ChalkAnimation.js";
|
|
2
|
+
import { Effects } from "../Effects.js";
|
|
3
|
+
import { InstanceTracker } from "../InstanceTracker.js";
|
|
4
|
+
const animations = [
|
|
5
|
+
"rainbow",
|
|
6
|
+
"pulse",
|
|
7
|
+
"glitch",
|
|
8
|
+
"radar",
|
|
9
|
+
"neon",
|
|
10
|
+
"karaoke",
|
|
11
|
+
];
|
|
12
|
+
describe("Chalk Animation", () => {
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
jest.restoreAllMocks();
|
|
15
|
+
});
|
|
16
|
+
describe("Spawn", () => {
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
jest.spyOn(InstanceTracker, "LOG").mockImplementation(() => { });
|
|
19
|
+
});
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
InstanceTracker.clearAll();
|
|
22
|
+
InstanceTracker.IDs.reset();
|
|
23
|
+
});
|
|
24
|
+
animations.forEach(name => {
|
|
25
|
+
it(`ChalkAnimation.${name} - returns an initialized animation instance`, () => {
|
|
26
|
+
const animation = ChalkAnimation[name]("hello");
|
|
27
|
+
expect(animation.effect).toEqual(Effects[name]);
|
|
28
|
+
expect(typeof animation.ID).toEqual("string");
|
|
29
|
+
expect(animation.text).toEqual(["hello"]);
|
|
30
|
+
expect(animation.lines).toEqual(1);
|
|
31
|
+
expect(animation.speed).toEqual(1);
|
|
32
|
+
expect(animation.current).toEqual(1);
|
|
33
|
+
expect(animation.initialized).toEqual(true);
|
|
34
|
+
expect(animation.stopped).toEqual(false);
|
|
35
|
+
expect(animation.controller).not.toEqual(null);
|
|
36
|
+
expect(InstanceTracker.bucket.size).toEqual(1);
|
|
37
|
+
animation.stop();
|
|
38
|
+
expect(animation.stopped).toEqual(true);
|
|
39
|
+
expect(animation.controller).toEqual(null);
|
|
40
|
+
});
|
|
41
|
+
InstanceTracker.loggers.forEach(method => {
|
|
42
|
+
it(`ChalkAnimation.${name} - subsequent console.${method} stop the animation`, () => {
|
|
43
|
+
const animation = ChalkAnimation[name]("hello");
|
|
44
|
+
expect(animation.stopped).toEqual(false);
|
|
45
|
+
console[method]("");
|
|
46
|
+
expect(animation.stopped).toEqual(true);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it(`ChalkAnimation.${name} - can be stopped and restarted`, () => {
|
|
50
|
+
const animation = ChalkAnimation[name]("hello");
|
|
51
|
+
animation.stop();
|
|
52
|
+
expect(animation.stopped).toEqual(true);
|
|
53
|
+
animation.start();
|
|
54
|
+
expect(animation.current).toEqual(2);
|
|
55
|
+
});
|
|
56
|
+
it(`ChalkAnimation.${name} - can be manually rendered at a desired frame rate`, () => {
|
|
57
|
+
const animation = ChalkAnimation[name]("hello").stop();
|
|
58
|
+
expect(animation.current).toEqual(1);
|
|
59
|
+
animation.render();
|
|
60
|
+
expect(animation.current).toEqual(2);
|
|
61
|
+
animation.render();
|
|
62
|
+
expect(animation.current).toEqual(3);
|
|
63
|
+
});
|
|
64
|
+
it(`ChalkAnimation.${name} - text can be replaced`, () => {
|
|
65
|
+
if (name === "radar") {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const animation = ChalkAnimation[name]("Y Y Y").stop();
|
|
69
|
+
const f1 = animation.nextFrame();
|
|
70
|
+
animation.replace("Z Z Z");
|
|
71
|
+
const f2 = animation.nextFrame();
|
|
72
|
+
expect(f1.includes("Y")).toEqual(true);
|
|
73
|
+
expect(f1.includes("Z")).toEqual(false);
|
|
74
|
+
expect(f2.includes("Y")).toEqual(false);
|
|
75
|
+
expect(f2.includes("Z")).toEqual(true);
|
|
76
|
+
});
|
|
77
|
+
it(`ChalkAnimation.${name} - supports multiline strings`, () => {
|
|
78
|
+
const animation = ChalkAnimation[name]("Lorem\nipsum\ndolor\nsit\namet").stop();
|
|
79
|
+
const frame = animation.nextFrame();
|
|
80
|
+
expect(animation.lines).toEqual(5);
|
|
81
|
+
expect(frame.split("\n")).toHaveLength(5);
|
|
82
|
+
});
|
|
83
|
+
it(`ChalkAnimation.${name} - supports 5K+ frames`, () => {
|
|
84
|
+
const animation = ChalkAnimation[name]("Lorem\nipsum\ndolor\nsit\namet").stop();
|
|
85
|
+
for (let i = 0; i < 5000; i++) {
|
|
86
|
+
expect(() => {
|
|
87
|
+
animation.nextFrame();
|
|
88
|
+
}).not.toThrow();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ChalkAnimation } from "./ChalkAnimation";
|
|
2
|
-
export * from "./types";
|
|
1
|
+
export { ChalkAnimation } from "./ChalkAnimation.js";
|
|
2
|
+
export * from "./types.js";
|
package/dist/mjs/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnimationFN } from "./types";
|
|
2
|
+
import { FrameRate } from "./FrameRate";
|
|
3
|
+
export declare class Animation extends FrameRate {
|
|
4
|
+
lines: number;
|
|
5
|
+
text: string[];
|
|
6
|
+
stopped: boolean;
|
|
7
|
+
initialized: boolean;
|
|
8
|
+
effect: AnimationFN;
|
|
9
|
+
static readonly LINE_BREAKS: RegExp;
|
|
10
|
+
constructor(effect: AnimationFN, str: string, delay?: number, speed?: number);
|
|
11
|
+
render(): void;
|
|
12
|
+
nextFrame(): string;
|
|
13
|
+
readonly replace: (str: string) => this;
|
|
14
|
+
readonly stop: () => this;
|
|
15
|
+
readonly start: () => this;
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Animation } from "./Animation";
|
|
2
|
+
/**
|
|
3
|
+
* Chalk Animation
|
|
4
|
+
*
|
|
5
|
+
* Console animations for your CLI tools
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const animation = ChalkAnimation.rainbow("hello!");
|
|
9
|
+
* animation.stop();
|
|
10
|
+
* animation.start();
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class ChalkAnimation {
|
|
14
|
+
static readonly neon: (str: string, speed?: number) => Animation;
|
|
15
|
+
static readonly radar: (str: string, speed?: number) => Animation;
|
|
16
|
+
static readonly pulse: (str: string, speed?: number) => Animation;
|
|
17
|
+
static readonly glitch: (str: string, speed?: number) => Animation;
|
|
18
|
+
static readonly rainbow: (str: string, speed?: number) => Animation;
|
|
19
|
+
static readonly karaoke: (str: string, speed?: number) => Animation;
|
|
20
|
+
private static configure;
|
|
21
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { MethodKeys, MethodLike } from "./types";
|
|
2
|
+
export declare class Decor {
|
|
3
|
+
static bound<T, K extends MethodKeys<T>>(target: T, _propertyKey: K, descriptor: TypedPropertyDescriptor<MethodLike>): void;
|
|
4
|
+
static chainable<I, F extends (...args: any[]) => any>(instance: I, func: F): (...params: Parameters<F>) => I;
|
|
5
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export declare class Effects {
|
|
2
|
-
static longHsv: {
|
|
2
|
+
static readonly longHsv: {
|
|
3
3
|
interpolation: string;
|
|
4
4
|
hsvSpin: string;
|
|
5
5
|
};
|
|
6
|
-
static glitchChars
|
|
6
|
+
static readonly glitchChars = "x*0987654321[]0-~@#(____!!!!\\|?????....0000\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
|
|
7
7
|
static rainbow(str: string, frame: number): string;
|
|
8
8
|
static pulse(str: string, frame: number): string;
|
|
9
9
|
static glitch(str: string, frame: number): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InstanceTracker } from "./InstanceTracker";
|
|
2
|
+
export declare class FrameRate extends InstanceTracker {
|
|
3
|
+
current: number;
|
|
4
|
+
speed: number;
|
|
5
|
+
delay: number;
|
|
6
|
+
controller: ReturnType<typeof setTimeout> | null;
|
|
7
|
+
constructor(delay: number, speed: number);
|
|
8
|
+
schedule<F extends () => any>(func: F): void;
|
|
9
|
+
increment(): void;
|
|
10
|
+
clearFrames(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type AnimationFN = (str: string, frame: number) => string;
|
|
2
|
+
export type AnimationName = "rainbow" | "pulse" | "glitch" | "radar" | "neon" | "karaoke";
|
|
3
|
+
export type MethodLike<T = any> = (...args: any[]) => T;
|
|
4
|
+
export type MethodKeys<T, R = any> = keyof {
|
|
5
|
+
[K in keyof T as Required<T>[K] extends MethodLike<R> ? K : never]: T[K];
|
|
6
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@figliolia/chalk-animation",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "An ESM/Common.js compatible typescript port of the popular 'chalk-animation' library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"animation",
|
|
@@ -30,23 +30,21 @@
|
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
32
|
"import": "./dist/mjs/index.js",
|
|
33
|
-
"require": "./dist/cjs/index.js"
|
|
33
|
+
"require": "./dist/cjs/index.js",
|
|
34
|
+
"types": "./dist/types/index.d.ts"
|
|
34
35
|
}
|
|
35
36
|
},
|
|
36
37
|
"main": "dist/cjs/index.js",
|
|
37
38
|
"module": "dist/mjs/index.js",
|
|
38
|
-
"types": "dist/
|
|
39
|
+
"types": "dist/types/index.d.ts",
|
|
39
40
|
"bin": "./cli.js",
|
|
40
41
|
"files": [
|
|
41
42
|
"dist",
|
|
42
43
|
"src/*"
|
|
43
44
|
],
|
|
44
45
|
"scripts": {
|
|
45
|
-
"build": "
|
|
46
|
-
"build-cjs": "tsc -p cd/tsconfig.cjs.json",
|
|
47
|
-
"build-esm": "tsc -p cd/tsconfig.mjs.json",
|
|
46
|
+
"build": "npx ts-packager -e src",
|
|
48
47
|
"lint": "tsc --noemit && eslint ./ --fix",
|
|
49
|
-
"package-targets": "cd/package.sh",
|
|
50
48
|
"test": "jest"
|
|
51
49
|
},
|
|
52
50
|
"dependencies": {
|
|
@@ -55,6 +53,7 @@
|
|
|
55
53
|
"gradient-string": "^2.0.2"
|
|
56
54
|
},
|
|
57
55
|
"devDependencies": {
|
|
56
|
+
"@figliolia/ts-packager": "^1.0.3",
|
|
58
57
|
"@swc/core": "^1.3.99",
|
|
59
58
|
"@types/gradient-string": "^1.1.5",
|
|
60
59
|
"@types/jest": "^29.5.10",
|
|
@@ -81,4 +80,4 @@
|
|
|
81
80
|
"publishConfig": {
|
|
82
81
|
"access": "public"
|
|
83
82
|
}
|
|
84
|
-
}
|
|
83
|
+
}
|
package/src/Animation.ts
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import type { AnimationFN } from "./types";
|
|
2
|
-
import {
|
|
2
|
+
import { FrameRate } from "./FrameRate";
|
|
3
|
+
import { Decor } from "./Decor";
|
|
3
4
|
|
|
4
|
-
export class Animation extends
|
|
5
|
-
effect: any;
|
|
5
|
+
export class Animation extends FrameRate {
|
|
6
6
|
lines: number;
|
|
7
|
-
speed: number;
|
|
8
|
-
delay: number;
|
|
9
7
|
text: string[];
|
|
10
8
|
stopped = false;
|
|
11
|
-
currentFrame = 0;
|
|
12
9
|
initialized = false;
|
|
13
|
-
|
|
10
|
+
effect: AnimationFN;
|
|
11
|
+
static readonly LINE_BREAKS = /\r\n|\r|\n/;
|
|
14
12
|
constructor(effect: AnimationFN, str: string, delay = 0, speed = 0) {
|
|
15
|
-
super();
|
|
13
|
+
super(delay, speed);
|
|
16
14
|
this.effect = effect;
|
|
17
|
-
this.
|
|
18
|
-
this.delay = delay;
|
|
19
|
-
this.text = str.split(/\r\n|\r|\n/);
|
|
15
|
+
this.text = str.split(Animation.LINE_BREAKS);
|
|
20
16
|
this.lines = this.text.length;
|
|
21
17
|
}
|
|
22
18
|
|
|
@@ -25,47 +21,37 @@ export class Animation extends InstanceTracker {
|
|
|
25
21
|
Animation.LOG("\n".repeat(this.lines - 1));
|
|
26
22
|
this.initialized = true;
|
|
27
23
|
}
|
|
28
|
-
Animation.LOG(this.
|
|
29
|
-
this.
|
|
24
|
+
Animation.LOG(this.nextFrame());
|
|
25
|
+
this.schedule(() => {
|
|
30
26
|
if (!this.stopped) {
|
|
31
27
|
this.render();
|
|
32
28
|
}
|
|
33
|
-
}
|
|
29
|
+
});
|
|
34
30
|
}
|
|
35
31
|
|
|
36
|
-
public
|
|
37
|
-
this.
|
|
32
|
+
public nextFrame() {
|
|
33
|
+
this.increment();
|
|
38
34
|
return (
|
|
39
35
|
"\u001B[" +
|
|
40
36
|
`${this.lines}` +
|
|
41
37
|
"F\u001B[G\u001B[2K" +
|
|
42
|
-
this.text.map(
|
|
38
|
+
this.text.map(str => this.effect(str, this.current)).join("\n")
|
|
43
39
|
);
|
|
44
40
|
}
|
|
45
41
|
|
|
46
|
-
public replace(str: string) {
|
|
47
|
-
this.text = str.split(
|
|
42
|
+
public readonly replace = Decor.chainable(this, (str: string) => {
|
|
43
|
+
this.text = str.split(Animation.LINE_BREAKS);
|
|
48
44
|
this.lines = this.text.length;
|
|
49
|
-
|
|
50
|
-
}
|
|
45
|
+
});
|
|
51
46
|
|
|
52
|
-
public stop() {
|
|
47
|
+
public readonly stop = Decor.chainable(this, () => {
|
|
53
48
|
this.clearFrames();
|
|
54
49
|
this.stopped = true;
|
|
55
|
-
|
|
56
|
-
}
|
|
50
|
+
});
|
|
57
51
|
|
|
58
|
-
public start() {
|
|
52
|
+
public readonly start = Decor.chainable(this, () => {
|
|
59
53
|
this.clearFrames();
|
|
60
54
|
this.stopped = false;
|
|
61
55
|
this.render();
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private clearFrames() {
|
|
66
|
-
if (this.controller) {
|
|
67
|
-
clearTimeout(this.controller);
|
|
68
|
-
this.controller = null;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
56
|
+
});
|
|
71
57
|
}
|
package/src/ChalkAnimation.ts
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
import { Effects } from "./Effects";
|
|
2
2
|
import { Animation } from "./Animation";
|
|
3
|
-
import {
|
|
3
|
+
import type { AnimationName } from "./types";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Chalk Animation
|
|
7
|
+
*
|
|
8
|
+
* Console animations for your CLI tools
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const animation = ChalkAnimation.rainbow("hello!");
|
|
12
|
+
* animation.stop();
|
|
13
|
+
* animation.start();
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
5
16
|
export class ChalkAnimation {
|
|
6
|
-
public static
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
public static
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public static glitch(str: string, speed = 1) {
|
|
15
|
-
return animateString(Effects.glitch, str, 55, speed);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public static radar(str: string, speed = 1) {
|
|
19
|
-
return animateString(Effects.radar, str, 50, speed);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public static neon(str: string, speed = 1) {
|
|
23
|
-
return animateString(Effects.neon, str, 500, speed);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public static karaoke(str: string, speed = 1) {
|
|
27
|
-
return animateString(Effects.karaoke, str, 50, speed);
|
|
28
|
-
}
|
|
17
|
+
public static readonly neon = this.configure("neon", 500);
|
|
18
|
+
public static readonly radar = this.configure("radar", 50);
|
|
19
|
+
public static readonly pulse = this.configure("pulse", 16);
|
|
20
|
+
public static readonly glitch = this.configure("glitch", 55);
|
|
21
|
+
public static readonly rainbow = this.configure("rainbow", 15);
|
|
22
|
+
public static readonly karaoke = this.configure("karaoke", 50);
|
|
29
23
|
|
|
30
|
-
|
|
31
|
-
return
|
|
24
|
+
private static configure(effect: AnimationName, delay: number) {
|
|
25
|
+
return (str: string, speed = 1) => {
|
|
26
|
+
if (!speed || speed <= 0) {
|
|
27
|
+
throw new Error("Expected `speed` to be an number greater than 0");
|
|
28
|
+
}
|
|
29
|
+
return new Animation(Effects[effect], str, delay, speed).start();
|
|
30
|
+
};
|
|
32
31
|
}
|
|
33
32
|
}
|
package/src/Decor.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { MethodKeys, MethodLike } from "./types";
|
|
2
|
+
|
|
3
|
+
export class Decor {
|
|
4
|
+
public static bound<T, K extends MethodKeys<T>>(
|
|
5
|
+
target: T,
|
|
6
|
+
_propertyKey: K,
|
|
7
|
+
descriptor: TypedPropertyDescriptor<MethodLike>,
|
|
8
|
+
) {
|
|
9
|
+
const operation = descriptor.value;
|
|
10
|
+
if (operation) {
|
|
11
|
+
descriptor.value = operation.bind(target);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static chainable<I, F extends (...args: any[]) => any>(
|
|
16
|
+
instance: I,
|
|
17
|
+
func: F,
|
|
18
|
+
) {
|
|
19
|
+
return (...params: Parameters<F>): I => {
|
|
20
|
+
func(...params);
|
|
21
|
+
return instance;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/Effects.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
2
|
import gradient from "gradient-string";
|
|
3
|
+
import { Decor } from "./Decor";
|
|
3
4
|
|
|
4
5
|
export class Effects {
|
|
5
|
-
public static longHsv = { interpolation: "hsv", hsvSpin: "long" };
|
|
6
|
-
public static glitchChars =
|
|
6
|
+
public static readonly longHsv = { interpolation: "hsv", hsvSpin: "long" };
|
|
7
|
+
public static readonly glitchChars =
|
|
7
8
|
"x*0987654321[]0-~@#(____!!!!\\|?????....0000\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
|
|
9
|
+
|
|
10
|
+
@Decor.bound
|
|
8
11
|
public static rainbow(str: string, frame: number) {
|
|
9
12
|
const hue = 5 * frame;
|
|
10
13
|
const leftColor = { h: hue % 360, s: 1, v: 1 };
|
|
11
14
|
const rightColor = { h: (hue + 1) % 360, s: 1, v: 1 };
|
|
12
|
-
return gradient(leftColor, rightColor)(str,
|
|
15
|
+
return gradient(leftColor, rightColor)(str, this.longHsv);
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
@Decor.bound
|
|
15
19
|
public static pulse(str: string, frame: number) {
|
|
16
20
|
frame = (frame % 120) + 1;
|
|
17
21
|
const transition = 6;
|
|
@@ -48,6 +52,7 @@ export class Effects {
|
|
|
48
52
|
return g(str);
|
|
49
53
|
}
|
|
50
54
|
|
|
55
|
+
@Decor.bound
|
|
51
56
|
public static glitch(str: string, frame: number) {
|
|
52
57
|
if (
|
|
53
58
|
(frame % 2) + (frame % 3) + (frame % 11) + (frame % 29) + (frame % 37) >
|
|
@@ -66,9 +71,9 @@ export class Effects {
|
|
|
66
71
|
if (str[i]) {
|
|
67
72
|
if (str[i] !== "\n" && str[i] !== "\r" && Math.random() > 0.995) {
|
|
68
73
|
chunks.push(
|
|
69
|
-
|
|
70
|
-
Math.floor(Math.random() *
|
|
71
|
-
]
|
|
74
|
+
this.glitchChars[
|
|
75
|
+
Math.floor(Math.random() * this.glitchChars.length)
|
|
76
|
+
],
|
|
72
77
|
);
|
|
73
78
|
} else if (Math.random() > 0.005) {
|
|
74
79
|
chunks.push(str[i]);
|
|
@@ -86,6 +91,7 @@ export class Effects {
|
|
|
86
91
|
return result;
|
|
87
92
|
}
|
|
88
93
|
|
|
94
|
+
@Decor.bound
|
|
89
95
|
public static radar(str: string, frame: number) {
|
|
90
96
|
const depth = Math.floor(Math.min(str.length, str.length * 0.2));
|
|
91
97
|
const step = Math.floor(255 / depth);
|
|
@@ -106,6 +112,7 @@ export class Effects {
|
|
|
106
112
|
return chars.join("");
|
|
107
113
|
}
|
|
108
114
|
|
|
115
|
+
@Decor.bound
|
|
109
116
|
public static neon(str: string, frame: number) {
|
|
110
117
|
const color =
|
|
111
118
|
frame % 2 === 0
|
|
@@ -114,6 +121,7 @@ export class Effects {
|
|
|
114
121
|
return color(str);
|
|
115
122
|
}
|
|
116
123
|
|
|
124
|
+
@Decor.bound
|
|
117
125
|
public static karaoke(str: string, frame: number) {
|
|
118
126
|
const chars = (frame % (str.length + 20)) - 10;
|
|
119
127
|
if (chars < 0) {
|
package/src/FrameRate.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { InstanceTracker } from "./InstanceTracker";
|
|
2
|
+
|
|
3
|
+
export class FrameRate extends InstanceTracker {
|
|
4
|
+
current = 0;
|
|
5
|
+
speed: number;
|
|
6
|
+
delay: number;
|
|
7
|
+
controller: ReturnType<typeof setTimeout> | null = null;
|
|
8
|
+
constructor(delay: number, speed: number) {
|
|
9
|
+
super();
|
|
10
|
+
this.delay = delay;
|
|
11
|
+
this.speed = speed;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public schedule<F extends () => any>(func: F) {
|
|
15
|
+
this.controller = setTimeout(func, this.delay / this.speed);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public increment() {
|
|
19
|
+
this.current++;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public clearFrames() {
|
|
23
|
+
if (this.controller) {
|
|
24
|
+
clearTimeout(this.controller);
|
|
25
|
+
this.controller = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/InstanceTracker.ts
CHANGED
|
@@ -6,6 +6,7 @@ export class InstanceTracker {
|
|
|
6
6
|
static readonly LOG = console.log;
|
|
7
7
|
static IDs = new AutoIncrementingID();
|
|
8
8
|
static bucket = new Map<string, Animation>();
|
|
9
|
+
static loggers = ["log", "warn", "info", "error"] as const;
|
|
9
10
|
constructor() {
|
|
10
11
|
InstanceTracker.clearAll();
|
|
11
12
|
this.ID = InstanceTracker.IDs.get();
|
|
@@ -13,10 +14,11 @@ export class InstanceTracker {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
static {
|
|
16
|
-
|
|
17
|
-
methods.forEach((method) => {
|
|
17
|
+
this.loggers.forEach(method => {
|
|
18
18
|
const original = console[method];
|
|
19
|
-
console[method] = function (
|
|
19
|
+
console[method] = function (
|
|
20
|
+
...args: Parameters<(typeof console)[typeof method]>
|
|
21
|
+
) {
|
|
20
22
|
InstanceTracker.clearAll();
|
|
21
23
|
return original.apply(console, args);
|
|
22
24
|
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ChalkAnimation } from "../ChalkAnimation";
|
|
2
|
+
import { Effects } from "../Effects";
|
|
3
|
+
import { InstanceTracker } from "../InstanceTracker";
|
|
4
|
+
|
|
5
|
+
const animations = [
|
|
6
|
+
"rainbow",
|
|
7
|
+
"pulse",
|
|
8
|
+
"glitch",
|
|
9
|
+
"radar",
|
|
10
|
+
"neon",
|
|
11
|
+
"karaoke",
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
describe("Chalk Animation", () => {
|
|
15
|
+
afterAll(() => {
|
|
16
|
+
jest.restoreAllMocks();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("Spawn", () => {
|
|
20
|
+
beforeAll(() => {
|
|
21
|
+
jest.spyOn(InstanceTracker, "LOG").mockImplementation(() => {});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
InstanceTracker.clearAll();
|
|
26
|
+
InstanceTracker.IDs.reset();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
animations.forEach(name => {
|
|
30
|
+
it(`ChalkAnimation.${name} - returns an initialized animation instance`, () => {
|
|
31
|
+
const animation = ChalkAnimation[name]("hello");
|
|
32
|
+
expect(animation.effect).toEqual(Effects[name]);
|
|
33
|
+
expect(typeof animation.ID).toEqual("string");
|
|
34
|
+
expect(animation.text).toEqual(["hello"]);
|
|
35
|
+
expect(animation.lines).toEqual(1);
|
|
36
|
+
expect(animation.speed).toEqual(1);
|
|
37
|
+
expect(animation.current).toEqual(1);
|
|
38
|
+
expect(animation.initialized).toEqual(true);
|
|
39
|
+
expect(animation.stopped).toEqual(false);
|
|
40
|
+
expect(animation.controller).not.toEqual(null);
|
|
41
|
+
expect(InstanceTracker.bucket.size).toEqual(1);
|
|
42
|
+
animation.stop();
|
|
43
|
+
expect(animation.stopped).toEqual(true);
|
|
44
|
+
expect(animation.controller).toEqual(null);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
InstanceTracker.loggers.forEach(method => {
|
|
48
|
+
it(`ChalkAnimation.${name} - subsequent console.${method} stop the animation`, () => {
|
|
49
|
+
const animation = ChalkAnimation[name]("hello");
|
|
50
|
+
expect(animation.stopped).toEqual(false);
|
|
51
|
+
console[method]("");
|
|
52
|
+
expect(animation.stopped).toEqual(true);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it(`ChalkAnimation.${name} - can be stopped and restarted`, () => {
|
|
57
|
+
const animation = ChalkAnimation[name]("hello");
|
|
58
|
+
animation.stop();
|
|
59
|
+
expect(animation.stopped).toEqual(true);
|
|
60
|
+
animation.start();
|
|
61
|
+
expect(animation.current).toEqual(2);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it(`ChalkAnimation.${name} - can be manually rendered at a desired frame rate`, () => {
|
|
65
|
+
const animation = ChalkAnimation[name]("hello").stop();
|
|
66
|
+
expect(animation.current).toEqual(1);
|
|
67
|
+
animation.render();
|
|
68
|
+
expect(animation.current).toEqual(2);
|
|
69
|
+
animation.render();
|
|
70
|
+
expect(animation.current).toEqual(3);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it(`ChalkAnimation.${name} - text can be replaced`, () => {
|
|
74
|
+
if (name === "radar") {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const animation = ChalkAnimation[name]("Y Y Y").stop();
|
|
78
|
+
const f1 = animation.nextFrame();
|
|
79
|
+
animation.replace("Z Z Z");
|
|
80
|
+
const f2 = animation.nextFrame();
|
|
81
|
+
expect(f1.includes("Y")).toEqual(true);
|
|
82
|
+
expect(f1.includes("Z")).toEqual(false);
|
|
83
|
+
expect(f2.includes("Y")).toEqual(false);
|
|
84
|
+
expect(f2.includes("Z")).toEqual(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it(`ChalkAnimation.${name} - supports multiline strings`, () => {
|
|
88
|
+
const animation = ChalkAnimation[name](
|
|
89
|
+
"Lorem\nipsum\ndolor\nsit\namet",
|
|
90
|
+
).stop();
|
|
91
|
+
const frame = animation.nextFrame();
|
|
92
|
+
expect(animation.lines).toEqual(5);
|
|
93
|
+
expect(frame.split("\n")).toHaveLength(5);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it(`ChalkAnimation.${name} - supports 5K+ frames`, () => {
|
|
97
|
+
const animation = ChalkAnimation[name](
|
|
98
|
+
"Lorem\nipsum\ndolor\nsit\namet",
|
|
99
|
+
).stop();
|
|
100
|
+
for (let i = 0; i < 5000; i++) {
|
|
101
|
+
expect(() => {
|
|
102
|
+
animation.nextFrame();
|
|
103
|
+
}).not.toThrow();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|