diff --git a/src/utils/counter.rs b/src/utils/counter.rs new file mode 100644 index 0000000..5a13875 --- /dev/null +++ b/src/utils/counter.rs @@ -0,0 +1,30 @@ +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +/// Atomic counter used by [`ProgressBar`]. +/// +/// [`ProgressBar`]: ../views/struct.ProgressBar.html +#[derive(Clone)] +pub struct Counter(pub Arc); + +impl Counter { + /// Creates a new `Counter` starting with the given value. + pub fn new(value: usize) -> Self { + Counter(Arc::new(AtomicUsize::new(value))) + } + + /// Retrieves the current progress value. + pub fn get(&self) -> usize { + self.0.load(Ordering::Relaxed) + } + + /// Sets the current progress value. + pub fn set(&self, value: usize) { + self.0.store(value, Ordering::Relaxed); + } + + /// Increase the current progress by `ticks`. + pub fn tick(&self, ticks: usize) { + self.0.fetch_add(ticks, Ordering::Relaxed); + } +} diff --git a/src/utils/lines/simple/lines_iterator.rs b/src/utils/lines/simple/lines_iterator.rs index 8305739..c14696d 100644 --- a/src/utils/lines/simple/lines_iterator.rs +++ b/src/utils/lines/simple/lines_iterator.rs @@ -70,6 +70,10 @@ impl<'a> Iterator for LinesIterator<'a> { let width = row.width; - Some(Row { start, end, width }) + Some(Row { + start, + end, + width, + }) } } diff --git a/src/utils/lines/spans/chunk_iterator.rs b/src/utils/lines/spans/chunk_iterator.rs index a92fa73..9034279 100644 --- a/src/utils/lines/spans/chunk_iterator.rs +++ b/src/utils/lines/spans/chunk_iterator.rs @@ -47,7 +47,10 @@ where } // Skip empty spans - if self.source.spans()[self.current_span].as_ref().is_empty() { + if self.source.spans()[self.current_span] + .as_ref() + .is_empty() + { self.current_span += 1; return self.next(); } @@ -120,9 +123,13 @@ where self.current_span += 1; // Skip empty spans - while let Some(true) = - self.source.spans().get(self.current_span).map(|span| { - span.as_ref().resolve(self.source.source()).is_empty() + while let Some(true) = self.source + .spans() + .get(self.current_span) + .map(|span| { + span.as_ref() + .resolve(self.source.source()) + .is_empty() }) { self.current_span += 1; } diff --git a/src/utils/lines/spans/lines_iterator.rs b/src/utils/lines/spans/lines_iterator.rs index 5acd9af..e14e831 100644 --- a/src/utils/lines/spans/lines_iterator.rs +++ b/src/utils/lines/spans/lines_iterator.rs @@ -76,8 +76,11 @@ where self.width }; - let mut chunks = - prefix(&mut self.iter, allowed_width, &mut self.chunk_offset); + let mut chunks = prefix( + &mut self.iter, + allowed_width, + &mut self.chunk_offset, + ); // println!("Chunks..: {:?}", chunks); @@ -162,6 +165,9 @@ where // TODO: merge consecutive segments of the same span - Some(Row { segments, width }) + Some(Row { + segments, + width, + }) } } diff --git a/src/utils/lines/spans/mod.rs b/src/utils/lines/spans/mod.rs index 2fbda35..ab4c275 100644 --- a/src/utils/lines/spans/mod.rs +++ b/src/utils/lines/spans/mod.rs @@ -4,13 +4,13 @@ //! //! Computed rows will include a list of span segments. //! Each segment include the source span ID, and start/end byte offsets. -mod lines_iterator; -mod chunk_iterator; -mod segment_merge_iterator; -mod row; -mod prefix; mod chunk; +mod chunk_iterator; +mod lines_iterator; +mod prefix; +mod row; mod segment; +mod segment_merge_iterator; #[cfg(test)] mod tests; diff --git a/src/utils/lines/spans/tests.rs b/src/utils/lines/spans/tests.rs index f4d645c..32fe742 100644 --- a/src/utils/lines/spans/tests.rs +++ b/src/utils/lines/spans/tests.rs @@ -8,7 +8,9 @@ fn input() -> StyledString { text.append(StyledString::styled("didn't", Effect::Bold)); text.append(StyledString::plain(" say ")); text.append(StyledString::styled("half", Effect::Italic)); - text.append(StyledString::plain(" the things people say I did.")); + text.append(StyledString::plain( + " the things people say I did.", + )); text.append(StyledString::plain("\n")); text.append(StyledString::plain("\n")); text.append(StyledString::plain(" - A. Einstein")); diff --git a/src/utils/markup/markdown.rs b/src/utils/markup/markdown.rs index 781b5fe..0bf983e 100644 --- a/src/utils/markup/markdown.rs +++ b/src/utils/markup/markdown.rs @@ -139,8 +139,10 @@ Attention ==== I *really* love __Cursive__!"; let spans = parse_spans(input); - let spans: Vec<_> = - spans.iter().map(|span| span.resolve(input)).collect(); + let spans: Vec<_> = spans + .iter() + .map(|span| span.resolve(input)) + .collect(); // println!("{:?}", spans); assert_eq!( diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 06d9b9b..88ee597 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,8 +1,10 @@ //! Toolbox to make text layout easier. -mod reader; -pub mod span; +mod counter; pub mod lines; pub mod markup; +mod reader; +pub mod span; +pub use self::counter::Counter; pub use self::reader::ProgressReader; diff --git a/src/utils/reader.rs b/src/utils/reader.rs index 056ccea..03ccad0 100644 --- a/src/utils/reader.rs +++ b/src/utils/reader.rs @@ -1,5 +1,5 @@ use std::io::{self, Read}; -use views::Counter; +use utils::Counter; /// Wrapper around a `Read` that reports the progress made. /// diff --git a/src/views/mod.rs b/src/views/mod.rs index b26217a..3661037 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -77,7 +77,7 @@ pub use self::menu_popup::MenuPopup; pub use self::menubar::Menubar; pub use self::on_event_view::OnEventView; pub use self::panel::Panel; -pub use self::progress_bar::{Counter, ProgressBar}; +pub use self::progress_bar::ProgressBar; pub use self::radio::{RadioButton, RadioGroup}; pub use self::select_view::SelectView; pub use self::shadow_view::ShadowView; diff --git a/src/views/progress_bar.rs b/src/views/progress_bar.rs index 44538e4..3377024 100644 --- a/src/views/progress_bar.rs +++ b/src/views/progress_bar.rs @@ -1,40 +1,13 @@ use Printer; use align::HAlign; use std::cmp; -use std::sync::Arc; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::thread; use theme::{ColorStyle, Effect}; +use utils::Counter; use view::View; // pub type CbPromise = Option>; -/// Atomic counter used by `ProgressBar`. -#[derive(Clone)] -pub struct Counter(pub Arc); - -impl Counter { - /// Creates a new `Counter` starting with the given value. - pub fn new(value: usize) -> Self { - Counter(Arc::new(AtomicUsize::new(value))) - } - - /// Retrieves the current progress value. - pub fn get(&self) -> usize { - self.0.load(Ordering::Relaxed) - } - - /// Sets the current progress value. - pub fn set(&self, value: usize) { - self.0.store(value, Ordering::Relaxed); - } - - /// Increase the current progress by `ticks`. - pub fn tick(&self, ticks: usize) { - self.0.fetch_add(ticks, Ordering::Relaxed); - } -} - /// Animated bar showing a progress value. /// /// This bar has an internal counter, and adapts the length of the displayed