itools_tui/components/
progress_bar.rs1use crate::{layout::Rect, render::Frame, style::Style};
4
5pub struct ProgressBar {
7 total: u64,
9 current: u64,
11 width: u16,
13 style: Style,
15 filled_style: Style,
17}
18
19impl ProgressBar {
20 pub fn new(total: u64, width: u16) -> Self {
22 Self { total, current: 0, width, style: Style::new(), filled_style: Style::new().reversed() }
23 }
24
25 pub fn style(mut self, style: Style) -> Self {
27 self.style = style;
28 self
29 }
30
31 pub fn filled_style(mut self, style: Style) -> Self {
33 self.filled_style = style;
34 self
35 }
36
37 pub fn update(&mut self, current: u64) {
39 self.current = current;
40 }
41
42 pub fn inc(&mut self, delta: u64) {
44 self.current += delta;
45 }
46
47 pub fn finish(&mut self) {
49 self.current = self.total;
50 }
51
52 pub fn percentage(&self) -> f64 {
54 if self.total > 0 { (self.current as f64 / self.total as f64) * 100.0 } else { 0.0 }
55 }
56}
57
58impl super::Component for ProgressBar {
59 fn render(&self, frame: &mut Frame, area: Rect) {
60 frame.render_progress_bar(self.current, self.total, self.width, area, self.style.clone(), self.filled_style.clone());
61 }
62
63 fn handle_event(&mut self, _event: &crate::event::Event) -> bool {
64 false
65 }
66}