@figliolia/chalk-animation 1.0.0 → 1.0.2

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.
Files changed (44) hide show
  1. package/README.md +11 -56
  2. package/dist/cjs/Animation.d.ts +8 -12
  3. package/dist/cjs/Animation.js +25 -36
  4. package/dist/cjs/ChalkAnimation.d.ts +18 -7
  5. package/dist/cjs/ChalkAnimation.js +26 -21
  6. package/dist/cjs/Decor.d.ts +5 -0
  7. package/dist/cjs/Decor.js +18 -0
  8. package/dist/cjs/Effects.d.ts +2 -2
  9. package/dist/cjs/Effects.js +28 -3
  10. package/dist/cjs/FrameRate.d.ts +11 -0
  11. package/dist/cjs/FrameRate.js +26 -0
  12. package/dist/cjs/InstanceTracker.d.ts +1 -0
  13. package/dist/cjs/InstanceTracker.js +4 -2
  14. package/dist/cjs/index.d.ts +1 -0
  15. package/dist/cjs/types.d.ts +5 -0
  16. package/dist/mjs/Animation.d.ts +8 -12
  17. package/dist/mjs/Animation.js +20 -33
  18. package/dist/mjs/ChalkAnimation.d.ts +18 -7
  19. package/dist/mjs/ChalkAnimation.js +24 -21
  20. package/dist/mjs/Decor.d.ts +5 -0
  21. package/dist/mjs/Decor.js +14 -0
  22. package/dist/mjs/Effects.d.ts +2 -2
  23. package/dist/mjs/Effects.js +27 -2
  24. package/dist/mjs/FrameRate.d.ts +11 -0
  25. package/dist/mjs/FrameRate.js +24 -0
  26. package/dist/mjs/InstanceTracker.d.ts +1 -0
  27. package/dist/mjs/InstanceTracker.js +2 -2
  28. package/dist/mjs/index.d.ts +1 -0
  29. package/dist/mjs/types.d.ts +5 -0
  30. package/package.json +1 -1
  31. package/src/Animation.ts +20 -34
  32. package/src/ChalkAnimation.ts +25 -26
  33. package/src/Decor.ts +24 -0
  34. package/src/Effects.ts +14 -6
  35. package/src/FrameRate.ts +28 -0
  36. package/src/InstanceTracker.ts +5 -3
  37. package/src/__tests__/ChalkAnimation.test.ts +108 -0
  38. package/src/index.ts +1 -0
  39. package/src/types.ts +14 -0
  40. package/dist/cjs/animateString.d.ts +0 -2
  41. package/dist/cjs/animateString.js +0 -18
  42. package/dist/mjs/animateString.d.ts +0 -2
  43. package/dist/mjs/animateString.js +0 -14
  44. package/src/animateString.ts +0 -15
package/README.md CHANGED
@@ -1,12 +1,6 @@
1
1
  # chalk-animation
2
2
 
3
- [![Build Status](https://flat.badgen.net/github/checks/bokub/chalk-animation?label=tests)](https://github.com/bokub/chalk-animation/actions/workflows/run.yml)
4
- [![Version](https://runkit.io/bokub/npm-version/branches/master/chalk-animation?style=flat)](https://www.npmjs.com/package/chalk-animation)
5
- [![Codecov](https://flat.badgen.net/codecov/c/github/bokub/chalk-animation)](https://codecov.io/gh/bokub/chalk-animation)
6
- [![Downloads](https://flat.badgen.net/npm/dm/chalk-animation?color=FF9800)](https://www.npmjs.com/package/chalk-animation)
7
- [![XO code style](https://flat.badgen.net/badge/code%20style/XO/5ed9c7)](https://github.com/sindresorhus/xo)
8
-
9
- > Colorful animations in terminal output
3
+ Colorful animations in terminal output
10
4
 
11
5
 
12
6
  ## Available animations
@@ -24,26 +18,26 @@
24
18
  ## Install
25
19
 
26
20
  ```bash
27
- $ npm i chalk-animation
21
+ $ npm i @figliolia/chalk-animation
28
22
  ```
29
23
 
30
24
 
31
25
  ## Usage
32
26
 
33
27
  ```javascript
34
- import chalkAnimation from 'chalk-animation';
28
+ import { ChalkAnimation } from '@figliolia/chalk-animation';
35
29
 
36
- chalkAnimation.rainbow('Lorem ipsum dolor sit amet');
30
+ ChalkAnimation.rainbow('Lorem ipsum dolor sit amet');
37
31
  ```
38
32
 
39
33
  #### Start and stop
40
34
 
41
35
  You can stop and resume an animation with `stop()` and `start()`.
42
36
 
43
- When created, the instance of chalkAnimation **starts automatically**.
37
+ When created, the instance of ChalkAnimation **starts automatically**.
44
38
 
45
39
  ```javascript
46
- const rainbow = chalkAnimation.rainbow('Lorem ipsum'); // Animation starts
40
+ const rainbow = ChalkAnimation.rainbow('Lorem ipsum'); // Animation starts
47
41
 
48
42
  setTimeout(() => {
49
43
  rainbow.stop(); // Animation stops
@@ -60,7 +54,7 @@ setTimeout(() => {
60
54
  Anything printed to the console will stop the previous animation automatically
61
55
 
62
56
  ```javascript
63
- chalkAnimation.rainbow('Lorem ipsum');
57
+ ChalkAnimation.rainbow('Lorem ipsum');
64
58
  setTimeout(() => {
65
59
  // Stop the 'Lorem ipsum' animation, then write on a new line.
66
60
  console.log('dolor sit amet');
@@ -72,7 +66,7 @@ setTimeout(() => {
72
66
  Change the animation speed using a second parameter. Should be greater than 0, default is 1.
73
67
 
74
68
  ```javascript
75
- chalkAnimation.rainbow('Lorem ipsum', 2); // Two times faster than default
69
+ ChalkAnimation.rainbow('Lorem ipsum', 2); // Two times faster than default
76
70
  ```
77
71
 
78
72
  #### Changing text
@@ -81,7 +75,7 @@ Change the animated text seamlessly with `replace()`
81
75
 
82
76
  ```javascript
83
77
  let str = 'Loading...';
84
- const rainbow = chalkAnimation.rainbow(str);
78
+ const rainbow = ChalkAnimation.rainbow(str);
85
79
 
86
80
  // Add a new dot every second
87
81
  setInterval(() => {
@@ -94,7 +88,7 @@ setInterval(() => {
94
88
  Manually render frames with `render()`, or get the content of the next frame with `frame()`
95
89
 
96
90
  ```javascript
97
- const rainbow = chalkAnimation.rainbow('Lorem ipsum').stop(); // Don't start the animation
91
+ const rainbow = ChalkAnimation.rainbow('Lorem ipsum').stop(); // Don't start the animation
98
92
 
99
93
  rainbow.render(); // Display the first frame
100
94
 
@@ -102,45 +96,6 @@ const frame = rainbow.frame(); // Get the second frame
102
96
  console.log(frame);
103
97
  ```
104
98
 
105
-
106
- ## CLI mode
107
-
108
- ```bash
109
- # Install package globally
110
- $ npm install --global chalk-animation
111
- ```
112
-
113
- ```
114
- $ chalk-animation --help
115
-
116
- Colorful animations in terminal output
117
-
118
- Usage
119
- $ chalk-animation <name> [options] [text...]
120
-
121
- Options
122
- --duration Duration of the animation in ms, defaults to Infinity
123
- --speed Animation speed as number > 0, defaults to 1
124
-
125
- Available animations
126
- rainbow
127
- pulse
128
- glitch
129
- radar
130
- neon
131
- karaoke
132
-
133
- Example
134
- $ chalk-animation rainbow Hello world!
135
- ```
136
-
137
-
138
- ## Related
139
-
140
- - [gradient-string](https://github.com/bokub/gradient-string) - Output gradients to terminal
141
- - [chalk](https://github.com/chalk/chalk) - Output colored text to terminal
142
-
143
-
144
99
  ## License
145
100
 
146
- MIT © [Boris K](https://github.com/bokub)
101
+ MIT © [Alex Figliolia](https://github.com/alexfigliolia)
@@ -1,20 +1,16 @@
1
1
  import type { AnimationFN } from "./types";
2
- import { InstanceTracker } from "./InstanceTracker";
3
- export declare class Animation extends InstanceTracker {
4
- effect: any;
2
+ import { FrameRate } from "./FrameRate";
3
+ export declare class Animation extends FrameRate {
5
4
  lines: number;
6
- speed: number;
7
- delay: number;
8
5
  text: string[];
9
6
  stopped: boolean;
10
- currentFrame: number;
11
7
  initialized: boolean;
12
- controller: ReturnType<typeof setTimeout> | null;
8
+ effect: AnimationFN;
9
+ static readonly LINE_BREAKS: RegExp;
13
10
  constructor(effect: AnimationFN, str: string, delay?: number, speed?: number);
14
11
  render(): void;
15
- frame(): string;
16
- replace(str: string): this;
17
- stop(): this;
18
- start(): this;
19
- private clearFrames;
12
+ nextFrame(): string;
13
+ readonly replace: (str: string) => this;
14
+ readonly stop: () => this;
15
+ readonly start: () => this;
20
16
  }
@@ -1,18 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Animation = void 0;
4
- const InstanceTracker_1 = require("./InstanceTracker");
5
- class Animation extends InstanceTracker_1.InstanceTracker {
4
+ const FrameRate_1 = require("./FrameRate");
5
+ const Decor_1 = require("./Decor");
6
+ class Animation extends FrameRate_1.FrameRate {
6
7
  constructor(effect, str, delay = 0, speed = 0) {
7
- super();
8
+ super(delay, speed);
8
9
  this.stopped = false;
9
- this.currentFrame = 0;
10
10
  this.initialized = false;
11
- this.controller = null;
11
+ this.replace = Decor_1.Decor.chainable(this, (str) => {
12
+ this.text = str.split(Animation.LINE_BREAKS);
13
+ this.lines = this.text.length;
14
+ });
15
+ this.stop = Decor_1.Decor.chainable(this, () => {
16
+ this.clearFrames();
17
+ this.stopped = true;
18
+ });
19
+ this.start = Decor_1.Decor.chainable(this, () => {
20
+ this.clearFrames();
21
+ this.stopped = false;
22
+ this.render();
23
+ });
12
24
  this.effect = effect;
13
- this.speed = speed;
14
- this.delay = delay;
15
- this.text = str.split(/\r\n|\r|\n/);
25
+ this.text = str.split(Animation.LINE_BREAKS);
16
26
  this.lines = this.text.length;
17
27
  }
18
28
  render() {
@@ -20,41 +30,20 @@ class Animation extends InstanceTracker_1.InstanceTracker {
20
30
  Animation.LOG("\n".repeat(this.lines - 1));
21
31
  this.initialized = true;
22
32
  }
23
- Animation.LOG(this.frame());
24
- this.controller = setTimeout(() => {
33
+ Animation.LOG(this.nextFrame());
34
+ this.schedule(() => {
25
35
  if (!this.stopped) {
26
36
  this.render();
27
37
  }
28
- }, this.delay / this.speed);
38
+ });
29
39
  }
30
- frame() {
31
- this.currentFrame++;
40
+ nextFrame() {
41
+ this.increment();
32
42
  return ("\u001B[" +
33
43
  `${this.lines}` +
34
44
  "F\u001B[G\u001B[2K" +
35
- this.text.map((str) => this.effect(str, this.currentFrame)).join("\n"));
36
- }
37
- replace(str) {
38
- this.text = str.split(/\r\n|\r|\n/);
39
- this.lines = this.text.length;
40
- return this;
41
- }
42
- stop() {
43
- this.clearFrames();
44
- this.stopped = true;
45
- return this;
46
- }
47
- start() {
48
- this.clearFrames();
49
- this.stopped = false;
50
- this.render();
51
- return this;
52
- }
53
- clearFrames() {
54
- if (this.controller) {
55
- clearTimeout(this.controller);
56
- this.controller = null;
57
- }
45
+ this.text.map(str => this.effect(str, this.current)).join("\n"));
58
46
  }
59
47
  }
60
48
  exports.Animation = Animation;
49
+ Animation.LINE_BREAKS = /\r\n|\r|\n/;
@@ -1,10 +1,21 @@
1
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
+ */
2
13
  export declare class ChalkAnimation {
3
- static rainbow(str: string, speed?: number): Animation;
4
- static pulse(str: string, speed?: number): Animation;
5
- static glitch(str: string, speed?: number): Animation;
6
- static radar(str: string, speed?: number): Animation;
7
- static neon(str: string, speed?: number): Animation;
8
- static karaoke(str: string, speed?: number): Animation;
9
- static clearAllAnimations(): void;
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;
10
21
  }
@@ -1,30 +1,35 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.ChalkAnimation = void 0;
4
5
  const Effects_1 = require("./Effects");
5
6
  const Animation_1 = require("./Animation");
6
- const animateString_1 = require("./animateString");
7
+ /**
8
+ * Chalk Animation
9
+ *
10
+ * Console animations for your CLI tools
11
+ *
12
+ * ```typescript
13
+ * const animation = ChalkAnimation.rainbow("hello!");
14
+ * animation.stop();
15
+ * animation.start();
16
+ * ```
17
+ */
7
18
  class ChalkAnimation {
8
- static rainbow(str, speed = 1) {
9
- return (0, animateString_1.animateString)(Effects_1.Effects.rainbow, str, 15, speed);
10
- }
11
- static pulse(str, speed = 1) {
12
- return (0, animateString_1.animateString)(Effects_1.Effects.pulse, str, 16, speed);
13
- }
14
- static glitch(str, speed = 1) {
15
- return (0, animateString_1.animateString)(Effects_1.Effects.glitch, str, 55, speed);
16
- }
17
- static radar(str, speed = 1) {
18
- return (0, animateString_1.animateString)(Effects_1.Effects.radar, str, 50, speed);
19
- }
20
- static neon(str, speed = 1) {
21
- return (0, animateString_1.animateString)(Effects_1.Effects.neon, str, 500, speed);
22
- }
23
- static karaoke(str, speed = 1) {
24
- return (0, animateString_1.animateString)(Effects_1.Effects.karaoke, str, 50, speed);
25
- }
26
- static clearAllAnimations() {
27
- return Animation_1.Animation.clearAll();
19
+ static configure(effect, delay) {
20
+ return (str, speed = 1) => {
21
+ if (!speed || speed <= 0) {
22
+ throw new Error("Expected `speed` to be an number greater than 0");
23
+ }
24
+ return new Animation_1.Animation(Effects_1.Effects[effect], str, delay, speed).start();
25
+ };
28
26
  }
29
27
  }
30
28
  exports.ChalkAnimation = ChalkAnimation;
29
+ _a = ChalkAnimation;
30
+ ChalkAnimation.neon = _a.configure("neon", 500);
31
+ ChalkAnimation.radar = _a.configure("radar", 50);
32
+ ChalkAnimation.pulse = _a.configure("pulse", 16);
33
+ ChalkAnimation.glitch = _a.configure("glitch", 55);
34
+ ChalkAnimation.rainbow = _a.configure("rainbow", 15);
35
+ ChalkAnimation.karaoke = _a.configure("karaoke", 50);
@@ -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
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Decor = void 0;
4
+ class Decor {
5
+ static bound(target, _propertyKey, descriptor) {
6
+ const operation = descriptor.value;
7
+ if (operation) {
8
+ descriptor.value = operation.bind(target);
9
+ }
10
+ }
11
+ static chainable(instance, func) {
12
+ return (...params) => {
13
+ func(...params);
14
+ return instance;
15
+ };
16
+ }
17
+ }
18
+ exports.Decor = Decor;
@@ -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: string;
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;
@@ -1,4 +1,10 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
2
8
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
10
  };
@@ -6,12 +12,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
12
  exports.Effects = void 0;
7
13
  const chalk_1 = __importDefault(require("chalk"));
8
14
  const gradient_string_1 = __importDefault(require("gradient-string"));
15
+ const Decor_1 = require("./Decor");
9
16
  class Effects {
10
17
  static rainbow(str, frame) {
11
18
  const hue = 5 * frame;
12
19
  const leftColor = { h: hue % 360, s: 1, v: 1 };
13
20
  const rightColor = { h: (hue + 1) % 360, s: 1, v: 1 };
14
- return (0, gradient_string_1.default)(leftColor, rightColor)(str, Effects.longHsv);
21
+ return (0, gradient_string_1.default)(leftColor, rightColor)(str, this.longHsv);
15
22
  }
16
23
  static pulse(str, frame) {
17
24
  frame = (frame % 120) + 1;
@@ -56,7 +63,7 @@ class Effects {
56
63
  i += skip;
57
64
  if (str[i]) {
58
65
  if (str[i] !== "\n" && str[i] !== "\r" && Math.random() > 0.995) {
59
- chunks.push(Effects.glitchChars[Math.floor(Math.random() * Effects.glitchChars.length)]);
66
+ chunks.push(this.glitchChars[Math.floor(Math.random() * this.glitchChars.length)]);
60
67
  }
61
68
  else if (Math.random() > 0.005) {
62
69
  chunks.push(str[i]);
@@ -104,6 +111,24 @@ class Effects {
104
111
  chalk_1.default.white(str.substr(chars)));
105
112
  }
106
113
  }
107
- exports.Effects = Effects;
108
114
  Effects.longHsv = { interpolation: "hsv", hsvSpin: "long" };
109
115
  Effects.glitchChars = "x*0987654321[]0-~@#(____!!!!\\|?????....0000\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
116
+ __decorate([
117
+ Decor_1.Decor.bound
118
+ ], Effects, "rainbow", null);
119
+ __decorate([
120
+ Decor_1.Decor.bound
121
+ ], Effects, "pulse", null);
122
+ __decorate([
123
+ Decor_1.Decor.bound
124
+ ], Effects, "glitch", null);
125
+ __decorate([
126
+ Decor_1.Decor.bound
127
+ ], Effects, "radar", null);
128
+ __decorate([
129
+ Decor_1.Decor.bound
130
+ ], Effects, "neon", null);
131
+ __decorate([
132
+ Decor_1.Decor.bound
133
+ ], Effects, "karaoke", null);
134
+ exports.Effects = Effects;
@@ -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,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FrameRate = void 0;
4
+ const InstanceTracker_1 = require("./InstanceTracker");
5
+ class FrameRate extends InstanceTracker_1.InstanceTracker {
6
+ constructor(delay, speed) {
7
+ super();
8
+ this.current = 0;
9
+ this.controller = null;
10
+ this.delay = delay;
11
+ this.speed = speed;
12
+ }
13
+ schedule(func) {
14
+ this.controller = setTimeout(func, this.delay / this.speed);
15
+ }
16
+ increment() {
17
+ this.current++;
18
+ }
19
+ clearFrames() {
20
+ if (this.controller) {
21
+ clearTimeout(this.controller);
22
+ this.controller = null;
23
+ }
24
+ }
25
+ }
26
+ exports.FrameRate = FrameRate;
@@ -8,6 +8,7 @@ export declare class InstanceTracker {
8
8
  };
9
9
  static IDs: AutoIncrementingID;
10
10
  static bucket: Map<string, Animation>;
11
+ static loggers: readonly ["log", "warn", "info", "error"];
11
12
  constructor();
12
13
  get instance(): Animation;
13
14
  static clearAll(): void;
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.InstanceTracker = void 0;
4
5
  const event_emitter_1 = require("@figliolia/event-emitter");
@@ -19,12 +20,13 @@ class InstanceTracker {
19
20
  }
20
21
  }
21
22
  exports.InstanceTracker = InstanceTracker;
23
+ _a = InstanceTracker;
22
24
  InstanceTracker.LOG = console.log;
23
25
  InstanceTracker.IDs = new event_emitter_1.AutoIncrementingID();
24
26
  InstanceTracker.bucket = new Map();
27
+ InstanceTracker.loggers = ["log", "warn", "info", "error"];
25
28
  (() => {
26
- const methods = ["log", "warn", "info", "error"];
27
- methods.forEach((method) => {
29
+ _a.loggers.forEach(method => {
28
30
  const original = console[method];
29
31
  console[method] = function (...args) {
30
32
  InstanceTracker.clearAll();
@@ -1,4 +1,5 @@
1
1
  export { ChalkAnimation } from "./ChalkAnimation";
2
+ export type { Effects } from "./Effects";
2
3
  export type { Animation } from "./Animation";
3
4
  export type { InstanceTracker } from "./InstanceTracker";
4
5
  export * from "./types";
@@ -1 +1,6 @@
1
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
+ };
@@ -1,20 +1,16 @@
1
1
  import type { AnimationFN } from "./types";
2
- import { InstanceTracker } from "./InstanceTracker";
3
- export declare class Animation extends InstanceTracker {
4
- effect: any;
2
+ import { FrameRate } from "./FrameRate";
3
+ export declare class Animation extends FrameRate {
5
4
  lines: number;
6
- speed: number;
7
- delay: number;
8
5
  text: string[];
9
6
  stopped: boolean;
10
- currentFrame: number;
11
7
  initialized: boolean;
12
- controller: ReturnType<typeof setTimeout> | null;
8
+ effect: AnimationFN;
9
+ static readonly LINE_BREAKS: RegExp;
13
10
  constructor(effect: AnimationFN, str: string, delay?: number, speed?: number);
14
11
  render(): void;
15
- frame(): string;
16
- replace(str: string): this;
17
- stop(): this;
18
- start(): this;
19
- private clearFrames;
12
+ nextFrame(): string;
13
+ readonly replace: (str: string) => this;
14
+ readonly stop: () => this;
15
+ readonly start: () => this;
20
16
  }
@@ -1,20 +1,16 @@
1
- import { InstanceTracker } from "./InstanceTracker";
2
- export class Animation extends InstanceTracker {
3
- effect;
1
+ import { FrameRate } from "./FrameRate";
2
+ import { Decor } from "./Decor";
3
+ export class Animation extends FrameRate {
4
4
  lines;
5
- speed;
6
- delay;
7
5
  text;
8
6
  stopped = false;
9
- currentFrame = 0;
10
7
  initialized = false;
11
- controller = null;
8
+ effect;
9
+ static LINE_BREAKS = /\r\n|\r|\n/;
12
10
  constructor(effect, str, delay = 0, speed = 0) {
13
- super();
11
+ super(delay, speed);
14
12
  this.effect = effect;
15
- this.speed = speed;
16
- this.delay = delay;
17
- this.text = str.split(/\r\n|\r|\n/);
13
+ this.text = str.split(Animation.LINE_BREAKS);
18
14
  this.lines = this.text.length;
19
15
  }
20
16
  render() {
@@ -22,40 +18,31 @@ export class Animation extends InstanceTracker {
22
18
  Animation.LOG("\n".repeat(this.lines - 1));
23
19
  this.initialized = true;
24
20
  }
25
- Animation.LOG(this.frame());
26
- this.controller = setTimeout(() => {
21
+ Animation.LOG(this.nextFrame());
22
+ this.schedule(() => {
27
23
  if (!this.stopped) {
28
24
  this.render();
29
25
  }
30
- }, this.delay / this.speed);
26
+ });
31
27
  }
32
- frame() {
33
- this.currentFrame++;
28
+ nextFrame() {
29
+ this.increment();
34
30
  return ("\u001B[" +
35
31
  `${this.lines}` +
36
32
  "F\u001B[G\u001B[2K" +
37
- this.text.map((str) => this.effect(str, this.currentFrame)).join("\n"));
33
+ this.text.map(str => this.effect(str, this.current)).join("\n"));
38
34
  }
39
- replace(str) {
40
- this.text = str.split(/\r\n|\r|\n/);
35
+ replace = Decor.chainable(this, (str) => {
36
+ this.text = str.split(Animation.LINE_BREAKS);
41
37
  this.lines = this.text.length;
42
- return this;
43
- }
44
- stop() {
38
+ });
39
+ stop = Decor.chainable(this, () => {
45
40
  this.clearFrames();
46
41
  this.stopped = true;
47
- return this;
48
- }
49
- start() {
42
+ });
43
+ start = Decor.chainable(this, () => {
50
44
  this.clearFrames();
51
45
  this.stopped = false;
52
46
  this.render();
53
- return this;
54
- }
55
- clearFrames() {
56
- if (this.controller) {
57
- clearTimeout(this.controller);
58
- this.controller = null;
59
- }
60
- }
47
+ });
61
48
  }
@@ -1,10 +1,21 @@
1
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
+ */
2
13
  export declare class ChalkAnimation {
3
- static rainbow(str: string, speed?: number): Animation;
4
- static pulse(str: string, speed?: number): Animation;
5
- static glitch(str: string, speed?: number): Animation;
6
- static radar(str: string, speed?: number): Animation;
7
- static neon(str: string, speed?: number): Animation;
8
- static karaoke(str: string, speed?: number): Animation;
9
- static clearAllAnimations(): void;
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;
10
21
  }