Remove Ticker alias

The `Counter` NewType can fill this role.
This commit is contained in:
Alexandre Bury 2016-07-27 23:54:49 -07:00
parent 5ce1094931
commit 261d2aac48
3 changed files with 12 additions and 20 deletions

View File

@ -4,7 +4,7 @@ extern crate rand;
use rand::Rng; use rand::Rng;
use cursive::prelude::*; use cursive::prelude::*;
use cursive::view::{Counter, Ticker}; use cursive::view::Counter;
use std::thread; use std::thread;
use std::cmp::min; use std::cmp::min;
@ -26,11 +26,11 @@ fn main() {
} }
// Function to simulate a long process. // Function to simulate a long process.
fn fake_load(n_max: usize, ticker: Ticker) { fn fake_load(n_max: usize, counter: Counter) {
for _ in 0..n_max { for _ in 0..n_max {
thread::sleep(Duration::from_millis(5)); thread::sleep(Duration::from_millis(5));
// The ticker method increases the progress value // The `counter.tick()` method increases the progress value
ticker(1); counter.tick(1);
} }
} }
@ -46,9 +46,9 @@ fn phase_1(s: &mut Cursive) {
s.pop_layer(); s.pop_layer();
s.add_layer(Panel::new(FullView::full_width(ProgressBar::new() s.add_layer(Panel::new(FullView::full_width(ProgressBar::new()
.range(0, n_max) .range(0, n_max)
.with_task(move |ticker| { .with_task(move |counter| {
// This closure will be called in a separate thread. // This closure will be called in a separate thread.
fake_load(n_max, ticker); fake_load(n_max, counter);
// When we're done, send a callback through the channel // When we're done, send a callback through the channel
cb.send(Box::new(coffee_break)).unwrap(); cb.send(Box::new(coffee_break)).unwrap();

View File

@ -94,7 +94,7 @@ pub use self::menubar::Menubar;
pub use self::menu_popup::MenuPopup; pub use self::menu_popup::MenuPopup;
pub use self::view_path::ViewPath; pub use self::view_path::ViewPath;
pub use self::panel::Panel; pub use self::panel::Panel;
pub use self::progress_bar::{Counter, ProgressBar, Ticker}; pub use self::progress_bar::{Counter, ProgressBar};
pub use self::select_view::SelectView; pub use self::select_view::SelectView;
pub use self::shadow_view::ShadowView; pub use self::shadow_view::ShadowView;
pub use self::sized_view::SizedView; pub use self::sized_view::SizedView;

View File

@ -68,11 +68,6 @@ pub struct ProgressBar {
label_maker: Box<Fn(usize, (usize, usize)) -> String>, label_maker: Box<Fn(usize, (usize, usize)) -> String>,
} }
/// Function used by tasks given to a `ProgressBar`.
///
/// This function will increment the progress bar counter by the given value.
pub type Ticker = Box<Fn(usize) + Send>;
fn make_percentage(value: usize, (min, max): (usize, usize)) -> String { fn make_percentage(value: usize, (min, max): (usize, usize)) -> String {
if value < min { if value < min {
@ -115,15 +110,12 @@ impl ProgressBar {
/// Starts a function in a separate thread, and monitor the progress. /// Starts a function in a separate thread, and monitor the progress.
/// ///
/// `f` will be given a `Ticker` to increment the bar's progress. /// `f` will be given a `Counter` to increment the bar's progress.
/// ///
/// This does not reset the value, so it can be called several times /// This does not reset the value, so it can be called several times
/// to advance the progress in multiple sessions. /// to advance the progress in multiple sessions.
pub fn start<F: FnOnce(Ticker) + Send + 'static>(&mut self, f: F) { pub fn start<F: FnOnce(Counter) + Send + 'static>(&mut self, f: F) {
let value = self.value.clone(); let ticker: Counter = self.value.clone();
let ticker: Ticker = Box::new(move |ticks| {
value.tick(ticks);
});
thread::spawn(move || { thread::spawn(move || {
f(ticker); f(ticker);
@ -133,7 +125,7 @@ impl ProgressBar {
/// Starts a function in a separate thread, and monitor the progress. /// Starts a function in a separate thread, and monitor the progress.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn with_task<F: FnOnce(Ticker) + Send + 'static>(mut self, task: F) pub fn with_task<F: FnOnce(Counter) + Send + 'static>(mut self, task: F)
-> Self { -> Self {
self.start(task); self.start(task);
self self