mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-10 03:10:41 +00:00
Remove Ticker
alias
The `Counter` NewType can fill this role.
This commit is contained in:
parent
5ce1094931
commit
261d2aac48
@ -4,7 +4,7 @@ extern crate rand;
|
||||
use rand::Rng;
|
||||
|
||||
use cursive::prelude::*;
|
||||
use cursive::view::{Counter, Ticker};
|
||||
use cursive::view::Counter;
|
||||
|
||||
use std::thread;
|
||||
use std::cmp::min;
|
||||
@ -26,11 +26,11 @@ fn main() {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
// The ticker method increases the progress value
|
||||
ticker(1);
|
||||
// The `counter.tick()` method increases the progress value
|
||||
counter.tick(1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,9 +46,9 @@ fn phase_1(s: &mut Cursive) {
|
||||
s.pop_layer();
|
||||
s.add_layer(Panel::new(FullView::full_width(ProgressBar::new()
|
||||
.range(0, n_max)
|
||||
.with_task(move |ticker| {
|
||||
.with_task(move |counter| {
|
||||
// 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
|
||||
cb.send(Box::new(coffee_break)).unwrap();
|
||||
|
@ -94,7 +94,7 @@ pub use self::menubar::Menubar;
|
||||
pub use self::menu_popup::MenuPopup;
|
||||
pub use self::view_path::ViewPath;
|
||||
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::shadow_view::ShadowView;
|
||||
pub use self::sized_view::SizedView;
|
||||
|
@ -68,11 +68,6 @@ pub struct ProgressBar {
|
||||
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 {
|
||||
if value < min {
|
||||
@ -115,15 +110,12 @@ impl ProgressBar {
|
||||
|
||||
/// 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
|
||||
/// to advance the progress in multiple sessions.
|
||||
pub fn start<F: FnOnce(Ticker) + Send + 'static>(&mut self, f: F) {
|
||||
let value = self.value.clone();
|
||||
let ticker: Ticker = Box::new(move |ticks| {
|
||||
value.tick(ticks);
|
||||
});
|
||||
pub fn start<F: FnOnce(Counter) + Send + 'static>(&mut self, f: F) {
|
||||
let ticker: Counter = self.value.clone();
|
||||
|
||||
thread::spawn(move || {
|
||||
f(ticker);
|
||||
@ -133,8 +125,8 @@ impl ProgressBar {
|
||||
/// Starts a function in a separate thread, and monitor the progress.
|
||||
///
|
||||
/// Chainable variant.
|
||||
pub fn with_task<F: FnOnce(Ticker) + Send + 'static>(mut self, task: F)
|
||||
-> Self {
|
||||
pub fn with_task<F: FnOnce(Counter) + Send + 'static>(mut self, task: F)
|
||||
-> Self {
|
||||
self.start(task);
|
||||
self
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user