summaryrefslogtreecommitdiff
path: root/src/progress_bar.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-08-25 20:31:08 +0200
committermo8it <mo8it@proton.me>2024-08-25 20:31:08 +0200
commit360605e2840d7b8876afac6df5178c068484cd57 (patch)
treee453cf5ab30f40e976ce16ee373a2b0fb2f88283 /src/progress_bar.rs
parent47976caa69e24ea9ee5d38918a0abea89ff10983 (diff)
parent64772544fad6788fd3fce5db3f357dba6f2d8d23 (diff)
Merge branch 'rm-ratatui'
Diffstat (limited to 'src/progress_bar.rs')
-rw-r--r--src/progress_bar.rs100
1 files changed, 0 insertions, 100 deletions
diff --git a/src/progress_bar.rs b/src/progress_bar.rs
deleted file mode 100644
index 7f07ad5..0000000
--- a/src/progress_bar.rs
+++ /dev/null
@@ -1,100 +0,0 @@
-use anyhow::{bail, Result};
-use ratatui::text::{Line, Span};
-use std::fmt::Write;
-
-const PREFIX: &str = "Progress: [";
-const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
-// Leaving the last char empty (_) for `total` > 99.
-const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16;
-const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
-const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
-
-const PROGRESS_EXCEEDS_MAX_ERR: &str =
- "The progress of the progress bar is higher than the maximum";
-
-/// Terminal progress bar to be used when not using Ratataui.
-pub fn progress_bar(progress: u16, total: u16, line_width: u16) -> Result<String> {
- use ratatui::crossterm::style::Stylize;
-
- if progress > total {
- bail!(PROGRESS_EXCEEDS_MAX_ERR);
- }
-
- if line_width < MIN_LINE_WIDTH {
- return Ok(format!("Progress: {progress}/{total} exercises"));
- }
-
- let mut line = String::with_capacity(usize::from(line_width));
- line.push_str(PREFIX);
-
- let width = line_width - WRAPPER_WIDTH;
- let filled = (width * progress) / total;
-
- let mut green_part = String::with_capacity(usize::from(filled + 1));
- for _ in 0..filled {
- green_part.push('#');
- }
-
- if filled < width {
- green_part.push('>');
- }
- write!(line, "{}", green_part.green()).unwrap();
-
- let width_minus_filled = width - filled;
- if width_minus_filled > 1 {
- let red_part_width = width_minus_filled - 1;
- let mut red_part = String::with_capacity(usize::from(red_part_width));
- for _ in 0..red_part_width {
- red_part.push('-');
- }
- write!(line, "{}", red_part.red()).unwrap();
- }
-
- writeln!(line, "] {progress:>3}/{total} exercises").unwrap();
-
- Ok(line)
-}
-
-/// Progress bar to be used with Ratataui.
-// Not using Ratatui's Gauge widget to keep the progress bar consistent.
-pub fn progress_bar_ratatui(progress: u16, total: u16, line_width: u16) -> Result<Line<'static>> {
- use ratatui::style::Stylize;
-
- if progress > total {
- bail!(PROGRESS_EXCEEDS_MAX_ERR);
- }
-
- if line_width < MIN_LINE_WIDTH {
- return Ok(Line::raw(format!("Progress: {progress}/{total} exercises")));
- }
-
- let mut spans = Vec::with_capacity(4);
- spans.push(Span::raw(PREFIX));
-
- let width = line_width - WRAPPER_WIDTH;
- let filled = (width * progress) / total;
-
- let mut green_part = String::with_capacity(usize::from(filled + 1));
- for _ in 0..filled {
- green_part.push('#');
- }
-
- if filled < width {
- green_part.push('>');
- }
- spans.push(green_part.green());
-
- let width_minus_filled = width - filled;
- if width_minus_filled > 1 {
- let red_part_width = width_minus_filled - 1;
- let mut red_part = String::with_capacity(usize::from(red_part_width));
- for _ in 0..red_part_width {
- red_part.push('-');
- }
- spans.push(red_part.red());
- }
-
- spans.push(Span::raw(format!("] {progress:>3}/{total} exercises")));
-
- Ok(Line::from(spans))
-}