mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Move Counter to utils module
This commit is contained in:
parent
d5178e778b
commit
063589b0cd
30
src/utils/counter.rs
Normal file
30
src/utils/counter.rs
Normal file
@ -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<AtomicUsize>);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -70,6 +70,10 @@ impl<'a> Iterator for LinesIterator<'a> {
|
|||||||
|
|
||||||
let width = row.width;
|
let width = row.width;
|
||||||
|
|
||||||
Some(Row { start, end, width })
|
Some(Row {
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
width,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Skip empty spans
|
// 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;
|
self.current_span += 1;
|
||||||
return self.next();
|
return self.next();
|
||||||
}
|
}
|
||||||
@ -120,9 +123,13 @@ where
|
|||||||
self.current_span += 1;
|
self.current_span += 1;
|
||||||
|
|
||||||
// Skip empty spans
|
// Skip empty spans
|
||||||
while let Some(true) =
|
while let Some(true) = self.source
|
||||||
self.source.spans().get(self.current_span).map(|span| {
|
.spans()
|
||||||
span.as_ref().resolve(self.source.source()).is_empty()
|
.get(self.current_span)
|
||||||
|
.map(|span| {
|
||||||
|
span.as_ref()
|
||||||
|
.resolve(self.source.source())
|
||||||
|
.is_empty()
|
||||||
}) {
|
}) {
|
||||||
self.current_span += 1;
|
self.current_span += 1;
|
||||||
}
|
}
|
||||||
|
@ -76,8 +76,11 @@ where
|
|||||||
self.width
|
self.width
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut chunks =
|
let mut chunks = prefix(
|
||||||
prefix(&mut self.iter, allowed_width, &mut self.chunk_offset);
|
&mut self.iter,
|
||||||
|
allowed_width,
|
||||||
|
&mut self.chunk_offset,
|
||||||
|
);
|
||||||
|
|
||||||
// println!("Chunks..: {:?}", chunks);
|
// println!("Chunks..: {:?}", chunks);
|
||||||
|
|
||||||
@ -162,6 +165,9 @@ where
|
|||||||
|
|
||||||
// TODO: merge consecutive segments of the same span
|
// TODO: merge consecutive segments of the same span
|
||||||
|
|
||||||
Some(Row { segments, width })
|
Some(Row {
|
||||||
|
segments,
|
||||||
|
width,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,13 @@
|
|||||||
//!
|
//!
|
||||||
//! Computed rows will include a list of span segments.
|
//! Computed rows will include a list of span segments.
|
||||||
//! Each segment include the source span ID, and start/end byte offsets.
|
//! 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;
|
||||||
|
mod chunk_iterator;
|
||||||
|
mod lines_iterator;
|
||||||
|
mod prefix;
|
||||||
|
mod row;
|
||||||
mod segment;
|
mod segment;
|
||||||
|
mod segment_merge_iterator;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -8,7 +8,9 @@ fn input() -> StyledString {
|
|||||||
text.append(StyledString::styled("didn't", Effect::Bold));
|
text.append(StyledString::styled("didn't", Effect::Bold));
|
||||||
text.append(StyledString::plain(" say "));
|
text.append(StyledString::plain(" say "));
|
||||||
text.append(StyledString::styled("half", Effect::Italic));
|
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("\n"));
|
text.append(StyledString::plain("\n"));
|
||||||
text.append(StyledString::plain(" - A. Einstein"));
|
text.append(StyledString::plain(" - A. Einstein"));
|
||||||
|
@ -139,8 +139,10 @@ Attention
|
|||||||
====
|
====
|
||||||
I *really* love __Cursive__!";
|
I *really* love __Cursive__!";
|
||||||
let spans = parse_spans(input);
|
let spans = parse_spans(input);
|
||||||
let spans: Vec<_> =
|
let spans: Vec<_> = spans
|
||||||
spans.iter().map(|span| span.resolve(input)).collect();
|
.iter()
|
||||||
|
.map(|span| span.resolve(input))
|
||||||
|
.collect();
|
||||||
|
|
||||||
// println!("{:?}", spans);
|
// println!("{:?}", spans);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
//! Toolbox to make text layout easier.
|
//! Toolbox to make text layout easier.
|
||||||
|
|
||||||
mod reader;
|
mod counter;
|
||||||
pub mod span;
|
|
||||||
pub mod lines;
|
pub mod lines;
|
||||||
pub mod markup;
|
pub mod markup;
|
||||||
|
mod reader;
|
||||||
|
pub mod span;
|
||||||
|
|
||||||
|
pub use self::counter::Counter;
|
||||||
pub use self::reader::ProgressReader;
|
pub use self::reader::ProgressReader;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use views::Counter;
|
use utils::Counter;
|
||||||
|
|
||||||
/// Wrapper around a `Read` that reports the progress made.
|
/// Wrapper around a `Read` that reports the progress made.
|
||||||
///
|
///
|
||||||
|
@ -77,7 +77,7 @@ pub use self::menu_popup::MenuPopup;
|
|||||||
pub use self::menubar::Menubar;
|
pub use self::menubar::Menubar;
|
||||||
pub use self::on_event_view::OnEventView;
|
pub use self::on_event_view::OnEventView;
|
||||||
pub use self::panel::Panel;
|
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::radio::{RadioButton, RadioGroup};
|
||||||
pub use self::select_view::SelectView;
|
pub use self::select_view::SelectView;
|
||||||
pub use self::shadow_view::ShadowView;
|
pub use self::shadow_view::ShadowView;
|
||||||
|
@ -1,40 +1,13 @@
|
|||||||
use Printer;
|
use Printer;
|
||||||
use align::HAlign;
|
use align::HAlign;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use theme::{ColorStyle, Effect};
|
use theme::{ColorStyle, Effect};
|
||||||
|
use utils::Counter;
|
||||||
use view::View;
|
use view::View;
|
||||||
|
|
||||||
// pub type CbPromise = Option<Box<Fn(&mut Cursive) + Send>>;
|
// pub type CbPromise = Option<Box<Fn(&mut Cursive) + Send>>;
|
||||||
|
|
||||||
/// Atomic counter used by `ProgressBar`.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Counter(pub Arc<AtomicUsize>);
|
|
||||||
|
|
||||||
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.
|
/// Animated bar showing a progress value.
|
||||||
///
|
///
|
||||||
/// This bar has an internal counter, and adapts the length of the displayed
|
/// This bar has an internal counter, and adapts the length of the displayed
|
||||||
|
Loading…
Reference in New Issue
Block a user