diff --git a/.changeset/strong-gifts-impress.md b/.changeset/strong-gifts-impress.md new file mode 100644 index 0000000..9a9e866 --- /dev/null +++ b/.changeset/strong-gifts-impress.md @@ -0,0 +1,5 @@ +--- +'archons': patch +--- + +Support set custumized tick strings and progress chars diff --git a/Cargo.toml b/Cargo.toml index cbd8543..afea5f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,6 @@ thiserror = "2.0.10" napi-build = "2" [profile.release] -lto = true +lto = true +opt-level = 3 +panic = "abort" diff --git a/examples/progressbar.cts b/examples/progressbar.cts index 82d1449..ac34a4a 100644 --- a/examples/progressbar.cts +++ b/examples/progressbar.cts @@ -1,40 +1,71 @@ import { Context, defineCommand, run } from '..' const spinner = defineCommand({ - meta: { - name: 'spinner', + meta: { + name: 'spinner', + }, + options: { + 'enable-steady-tick': { + type: 'option', + action: 'store', }, - options: { - 'enable-steady-tick': { - type: 'option', - action: 'store', + }, + callback: async (ctx: Context) => { + const spinner = ctx.createSpinner() + spinner.setMessage('loading') + spinner.tick() + let i = 100 + const interval = ctx.args.interval as number + if (ctx.args['enable-steady-tick']) { + spinner.println('Enabled steady tick') + spinner.enableSteadyTick(interval) + while (i--) { + if (i < 30) { + spinner.setMessage('Disabled steady tick for now') + spinner.disableSteadyTick() } + await new Promise((resolve) => setTimeout(resolve, interval)) + } + } else { + spinner.println('Disabled steady tick') + while (i--) { + spinner.tick() + await new Promise((resolve) => setTimeout(resolve, interval)) + } + } + spinner.finishWithMessage('✨ finished') + }, +}) + +const bar = defineCommand({ + meta: { + name: 'bar', + }, + options: { + clear: { + type: 'option', + action: 'store', + help: 'Clear the progress bar', + parser: 'boolean', }, - callback: async (ctx: Context) => { - const spinner = ctx.createSpinner(); - spinner.setMessage('loading'); - spinner.tick(); - let i = 100; - const interval = ctx.args.interval as number; - if (ctx.args['enable-steady-tick']) { - spinner.println('Enabled steady tick'); - spinner.enableSteadyTick(interval); - while (i--) { - if (i < 30) { - spinner.setMessage('Disabled steady tick for now'); - spinner.disableSteadyTick(); - } - await new Promise((resolve) => setTimeout(resolve, interval)); - } - } else { - spinner.println('Disabled steady tick'); - while (i--) { - spinner.tick(); - await new Promise((resolve) => setTimeout(resolve, interval)); - } - } - spinner.finishWithMessage('✨ finished'); + }, + callback: async (ctx: Context) => { + const bar = ctx.createProgressBar(ctx.args.total as number) + bar.setTemplate('{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos:>5}/{len:5} {msg}') + bar.setProgressChars('=>-') + let i = 100 + const interval = ctx.args.interval as number + while (i--) { + bar.inc(1) + await new Promise((resolve) => setTimeout(resolve, interval)) } + if (ctx.args.clear) { + bar.finishAndClear() + } else { + bar.finish() + } + console.log('✨ finished') + }, }) const main = defineCommand({ @@ -50,11 +81,20 @@ const main = defineCommand({ default: '100', global: true, help: 'Interval of spinner', - parser: 'number' - } + parser: 'number', + }, + total: { + type: 'option', + numArgs: '1', + default: '100', + global: true, + help: 'Total of progress bar', + parser: 'number', + }, }, subcommands: { spinner, + bar, }, }) diff --git a/examples/tsconfig.json b/examples/tsconfig.json index c484ad4..3430932 100644 --- a/examples/tsconfig.json +++ b/examples/tsconfig.json @@ -1,11 +1,10 @@ { - "extends": "../tsconfig.json", - "compilerOptions": { - "moduleResolution": "NodeNext", - "module": "NodeNext", - "outDir": "." - }, - "include": ["."], - "exclude": ["index.cjs"] - } - \ No newline at end of file + "extends": "../tsconfig.json", + "compilerOptions": { + "moduleResolution": "NodeNext", + "module": "NodeNext", + "outDir": "." + }, + "include": ["."], + "exclude": ["index.cjs"] +} diff --git a/index.d.ts b/index.d.ts index f56c737..868cb23 100644 --- a/index.d.ts +++ b/index.d.ts @@ -223,6 +223,7 @@ export interface Command { export declare class ProgressBar { finish(): void finishAndClear(): void + finishUsingStyle(): void finishWithMessage(msg: string): void setPosition(pos: number): void setLength(len: number): void @@ -230,6 +231,8 @@ export declare class ProgressBar { setPrefix(prefix: string): void setTabWidth(width: number): void setTemplate(template: string): void + setTickStrings(s: Array): void + setProgressChars(s: string): void tick(): void abandon(): void abandonWithMessage(msg: string): void diff --git a/src/progressbar.rs b/src/progressbar.rs index a862fbd..e598970 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -7,7 +7,6 @@ use crate::types::{Context, Error}; #[napi] pub struct ProgressBar { bar: indicatif::ProgressBar, - style: indicatif::ProgressStyle, } #[napi] @@ -22,6 +21,11 @@ impl ProgressBar { self.bar.finish_and_clear(); } + #[napi] + pub fn finish_using_style(&self) { + self.bar.finish_using_style(); + } + #[napi] pub fn finish_with_message(&self, msg: String) { self.bar.finish_with_message(msg); @@ -57,14 +61,31 @@ impl ProgressBar { Ok( self.bar.set_style( self - .style - .clone() + .bar + .style() .template(&template) .map_err(Error::IndicatifTemplateError)?, ), ) } + #[napi] + pub fn set_tick_strings(&self, s: Vec) { + self.bar.set_style( + self + .bar + .style() + .tick_strings(&s.iter().map(|s| s.as_str()).collect::>()), + ); + } + + #[napi] + pub fn set_progress_chars(&self, s: String) { + self + .bar + .set_style(self.bar.style().progress_chars(s.as_str())); + } + #[napi] pub fn tick(&self) { self.bar.tick(); @@ -123,14 +144,12 @@ impl Context { #[napi] pub fn create_progress_bar(&self, total: u32) -> ProgressBar { let bar = indicatif::ProgressBar::new(total as u64); - let style = indicatif::ProgressStyle::default_bar(); - ProgressBar { bar, style } + ProgressBar { bar } } #[napi] pub fn create_spinner(&self) -> ProgressBar { let bar = indicatif::ProgressBar::new_spinner(); - let style = indicatif::ProgressStyle::default_spinner(); - ProgressBar { bar, style } + ProgressBar { bar } } }