Skip to content

Commit

Permalink
feat(progressbar): support for customize progressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jan 11, 2025
1 parent fa31f1f commit 036c0b5
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-gifts-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'archons': patch
---

Support set custumized tick strings and progress chars
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ thiserror = "2.0.10"
napi-build = "2"

[profile.release]
lto = true
lto = true
opt-level = 3
panic = "abort"
104 changes: 72 additions & 32 deletions examples/progressbar.cts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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,
},
})

Expand Down
19 changes: 9 additions & 10 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "."
},
"include": ["."],
"exclude": ["index.cjs"]
}

"extends": "../tsconfig.json",
"compilerOptions": {
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "."
},
"include": ["."],
"exclude": ["index.cjs"]
}
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,16 @@ 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
setMessage(msg: string): void
setPrefix(prefix: string): void
setTabWidth(width: number): void
setTemplate(template: string): void
setTickStrings(s: Array<string>): void
setProgressChars(s: string): void
tick(): void
abandon(): void
abandonWithMessage(msg: string): void
Expand Down
33 changes: 26 additions & 7 deletions src/progressbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::types::{Context, Error};
#[napi]
pub struct ProgressBar {
bar: indicatif::ProgressBar,
style: indicatif::ProgressStyle,
}

#[napi]
Expand All @@ -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);
Expand Down Expand Up @@ -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<String>) {
self.bar.set_style(
self
.bar
.style()
.tick_strings(&s.iter().map(|s| s.as_str()).collect::<Vec<&str>>()),
);
}

#[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();
Expand Down Expand Up @@ -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 }
}
}

0 comments on commit 036c0b5

Please sign in to comment.