@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
50 lines (48 loc) • 1.93 kB
text/typescript
import chalk from 'chalk';
export class Spinner {
spinnerTypes: string[][];
currentCharIndex: number;
interval: NodeJS.Timeout | null;
spinnerChars: string[];
currentTypeIndex: number;
constructor() {
this.spinnerTypes = [
['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷', '⣜', '⣄'],
['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'],
['◐', '◓', '◑', '◒'],
['▖', '▘', '▝', '▗'],
['◢', '◣', '◤', '◥'],
['◰', '◳', '◲', '◱'],
['◴', '◷', '◶', '◵'],
['▰', '▱'],
['▉', '▊', '▋', '▌', '▍', '▎', '▏', '▎', '▍', '▌', '▋', '▊', '▉'],
['▁', '▃', '▄', '▅', '▆', '▇', '▆', '▅', '▄', '▃'],
];
this.currentCharIndex = 0;
this.currentTypeIndex = 0;
this.spinnerChars = this.spinnerTypes[this.currentTypeIndex];
this.interval = null;
}
start(message = 'Loading...') {
this.currentTypeIndex = Math.floor(Math.random() * this.spinnerTypes.length);
this.spinnerChars = this.spinnerTypes[this.currentTypeIndex];
process.stdout.write(`\r\n${message}`);
this.interval = setInterval(() => {
process.stdout.write(`\r${message} ${this.spinnerChars[this.currentCharIndex]}`);
this.currentCharIndex = (this.currentCharIndex + 1) % this.spinnerChars.length;
}, 100);
}
stop() {
this.interval && clearInterval(this.interval);
process.stdout.write('\r\n');
}
succeed(message = 'Done!') {
console.log(chalk.green(message));
this.stop();
}
fail(message = 'Failed!') {
console.log(chalk.red(message));
this.stop();
}
}