Skip to content

Commit

Permalink
Move chars to theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 14, 2024
1 parent ebf6216 commit d6634a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
25 changes: 16 additions & 9 deletions crates/console/src/components/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub struct ProgressBarProps {
pub auto_tick: Option<Duration>,
pub bar_color: Option<Color>,
pub bar_width: i32,
pub char_filled: char,
pub char_position: char,
pub char_unfilled: char,
pub char_filled: Option<char>,
pub char_position: Option<char>,
pub char_unfilled: Option<char>,
pub default_max: i32,
pub default_message: String,
pub default_value: i32,
Expand All @@ -85,9 +85,9 @@ impl Default for ProgressBarProps {
auto_tick: None,
bar_color: None,
bar_width: 30,
char_filled: '█',
char_position: '▒',
char_unfilled: '░',
char_filled: None,
char_position: None,
char_unfilled: None,
default_max: 100,
default_message: "".into(),
default_value: 0,
Expand Down Expand Up @@ -173,6 +173,13 @@ pub fn ProgressBar<'a>(
}
});

let char_filled = props.char_filled.unwrap_or(theme.progress_bar_filled_char);
let char_unfilled = props
.char_unfilled
.unwrap_or(theme.progress_bar_unfilled_char);
let char_position = props
.char_position
.unwrap_or(theme.progress_bar_position_char);
let bar_color = props.bar_color.unwrap_or(theme.brand_color);
let bar_percent = max.get() as f32 * (value.get() as f32 / 100.0);
let bar_total_width = props.bar_width as u32;
Expand All @@ -194,7 +201,7 @@ pub fn ProgressBar<'a>(
Group(gap: 1) {
Box(width: Size::Length(bar_total_width)) {
Text(content:
String::from(props.char_filled).repeat(bar_filled_width as usize),
String::from(char_filled).repeat(bar_filled_width as usize),
color: bar_color,
)

Expand All @@ -203,14 +210,14 @@ pub fn ProgressBar<'a>(
} else {
Some(element! {
Text(
content: String::from(props.char_position),
content: String::from(char_position),
color: bar_color,
)
})
})

Text(
content: String::from(props.char_unfilled).repeat(bar_unfilled_width as usize),
content: String::from(char_unfilled).repeat(bar_unfilled_width as usize),
color: bar_color,
)
}
Expand Down
8 changes: 8 additions & 0 deletions crates/console/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub struct ConsoleTheme {
pub border_color: Color,
pub border_focus_color: Color,

// Progress
pub progress_bar_filled_char: char,
pub progress_bar_position_char: char,
pub progress_bar_unfilled_char: char,

// Variants
pub variant_caution: Color,
pub variant_failure: Color,
Expand All @@ -33,6 +38,9 @@ impl Default for ConsoleTheme {
bg_alt_color: Color::AnsiValue(234),
border_color: style_to_color(Style::Muted),
border_focus_color: style_to_color(Style::MutedLight),
progress_bar_filled_char: '█',
progress_bar_position_char: '▒',
progress_bar_unfilled_char: '░',
variant_caution: style_to_color(Style::Caution),
variant_failure: style_to_color(Style::Failure),
variant_info: style_to_color(Style::Label),
Expand Down
5 changes: 4 additions & 1 deletion examples/term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ async fn render(session: TestSession, ui: String) {
default_message: "Unfilled".to_owned()
)
ProgressBar(
default_message: "Partially filled".to_owned(),
char_filled: '━',
char_position: '╾',
char_unfilled: '─',
default_message: "Partially filled (custom bar)".to_owned(),
default_value: 50
)
ProgressBar(
Expand Down

0 comments on commit d6634a1

Please sign in to comment.