@cocreate/cli 1.48.0 → 1.50.0

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/src/spinner.js ADDED
@@ -0,0 +1,85 @@
1
+ // Define an object holding various spinner patterns, each pattern is an array of strings representing different frames
2
+ const spinnerPatterns = {
3
+ // Dots pattern: sequential increase in dots
4
+ dots: [". ", ".. ", "..."],
5
+
6
+ // Bar filling up incrementally
7
+ bar: ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"],
8
+
9
+ // A bouncing ball effect using Braille characters
10
+ bouncingBall: ["⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"],
11
+
12
+ // Wave pattern: characters form a wave-like effect
13
+ wave: ["⎺", "⎻", "⎼", "⎽", "⎼", "⎻"],
14
+
15
+ // Rotating arrows pattern for direction indication
16
+ arrows: ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"],
17
+
18
+ // Rotating dots pattern with increasing and decreasing length
19
+ rotatingDots: [
20
+ ". ",
21
+ ".. ",
22
+ "... ",
23
+ ".... ",
24
+ ".....",
25
+ " ....",
26
+ " ...",
27
+ " ..",
28
+ " ."
29
+ ],
30
+
31
+ // Horizontal bounce pattern to simulate a bouncing ball effect
32
+ horizontalBounce: [
33
+ "[ ]",
34
+ "[= ]",
35
+ "[== ]",
36
+ "[=== ]",
37
+ "[ ===]",
38
+ "[ ==]",
39
+ "[ =]"
40
+ ]
41
+ };
42
+
43
+ // spinner.js
44
+
45
+ /**
46
+ * Creates a spinner with the specified configuration
47
+ * @param {Object} options - Configuration options for the spinner
48
+ * @param {string} options.type - The type of spinner pattern
49
+ * @param {number} options.milliseconds - Interval in milliseconds for frame change
50
+ * @param {string} options.prefix - String to prefix before the spinner frame
51
+ * @param {string} options.suffix - String to suffix after the spinner frame
52
+ * @param {Function} options.onComplete - Callback function to execute when the spinner ends
53
+ * @returns {Object} - An object with an `end` method to stop the spinner
54
+ */
55
+ function createSpinner({
56
+ type = "dots", // Default type is 'dots'
57
+ milliseconds = 200, // Default interval time is 200 ms
58
+ prefix = "", // Default prefix is an empty string
59
+ suffix = "", // Default suffix is an empty string
60
+ onComplete = () => process.stdout.write("\r") // Default onComplete action is to clear the line
61
+ }) {
62
+ // Get the frames for the specified spinner type, fall back to 'dots' pattern if type not found
63
+ const frames = spinnerPatterns[type] || spinnerPatterns.dots;
64
+ let spinnerIndex = 0; // Track current frame index
65
+
66
+ // Set an interval to update the spinner frame
67
+ const interval = setInterval(() => {
68
+ // Write spinner frame to the process standard output along with prefix and suffix
69
+ process.stdout.write(`\r${prefix}${frames[spinnerIndex]}${suffix}`);
70
+ // Update frame index, reset to 0 if it exceeds available frames
71
+ spinnerIndex = (spinnerIndex + 1) % frames.length;
72
+ }, milliseconds);
73
+
74
+ // Return an interface to interact with spinner
75
+ return {
76
+ // Method to stop the spinner and execute the onComplete callback
77
+ end: () => {
78
+ clearInterval(interval); // Clear the frame update interval
79
+ onComplete(); // Execute onComplete callback (default clears the current line)
80
+ }
81
+ };
82
+ }
83
+
84
+ // Export createSpinner function as a module
85
+ module.exports = createSpinner;