From 57d795abdf564efdd2898b967e1b8e30ea36ffcc Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Sat, 23 Sep 2023 11:51:26 -0700 Subject: [PATCH] Split out options.rs --- src/lib.rs | 136 ++----------------------------------------------- src/options.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 132 deletions(-) create mode 100644 src/options.rs diff --git a/src/lib.rs b/src/lib.rs index 3ffa7a5..4e1df26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -214,12 +214,13 @@ is welcome. use std::fmt::Display; use std::io::{self, Write}; use std::sync::{Arc, Mutex}; -use std::time::{Duration, Instant}; +use std::time::Instant; mod ansi; mod destination; mod helpers; pub mod models; +mod options; mod width; #[cfg(windows)] mod windows; @@ -230,8 +231,9 @@ pub mod _changelog { use super::*; // so that hyperlinks work } +pub use crate::destination::Destination; pub use crate::helpers::*; -pub use destination::Destination; +pub use crate::options::Options; /// An application-defined type that holds whatever state is relevant to the /// progress bar, and that can render it into one or more lines of text. @@ -870,133 +872,3 @@ impl InnerView { .clone() } } - -/// Options controlling a View. -/// -/// These are supplied to a constructor like [View::new], and cannot be changed after the view is created. -/// -/// The default options created by [Options::default] should be reasonable -/// for most applications. -/// -/// # Example -/// -/// ``` -/// let options = nutmeg::Options::default() -/// .progress_enabled(false); // Don't draw bars, only print. -/// ``` -/// -/// Options can be constructed as a static or constant value, using [Options::new]. -/// -/// ``` -/// use std::time::Duration; -/// use nutmeg::Options; -/// -/// static NUTMEG_OPTIONS: Options = Options::new() -/// .update_interval(Duration::from_millis(100)) -/// .progress_enabled(true) -/// .destination(nutmeg::Destination::Stderr); -/// ``` -#[derive(Debug, Clone)] -pub struct Options { - /// Target interval to repaint the progress bar. - update_interval: Duration, - - /// How long to wait after printing output before drawing the progress bar again. - print_holdoff: Duration, - - /// Is the progress bar drawn at all? - progress_enabled: bool, - - /// Use a fake clock for testing. - fake_clock: bool, - - /// Write progress and messages to stdout, stderr, or a capture buffer for tests? - destination: Destination, -} - -impl Options { - /// Return some reasonable default options. - /// - /// The update interval and print holdoff are 100ms, the progress bar is enabled, - /// and output is sent to stdout. - pub const fn new() -> Options { - Options { - update_interval: Duration::from_millis(100), - print_holdoff: Duration::from_millis(100), - progress_enabled: true, - fake_clock: false, - destination: Destination::Stdout, - } - } - - /// Set whether the progress bar will be drawn. - /// - /// By default it is drawn, except that this value will be ignored by [View::new] if stdout is not a terminal. - pub const fn progress_enabled(self, progress_enabled: bool) -> Options { - Options { - progress_enabled, - ..self - } - } - - /// Set the minimal interval to repaint the progress bar. - /// - /// `Duration::ZERO` can be used to cause the bar to repaint on every update. - pub const fn update_interval(self, update_interval: Duration) -> Options { - Options { - update_interval, - ..self - } - } - - /// Set the minimal interval between printing a message and painting - /// the progress bar. - /// - /// This is used to avoid the bar flickering if the application is - /// repeatedly printing messages at short intervals. - /// - /// `Duration::ZERO` can be used to disable this behavior. - pub const fn print_holdoff(self, print_holdoff: Duration) -> Options { - Options { - print_holdoff, - ..self - } - } - - /// Enable use of a fake clock, for testing. - /// - /// When true, all calculations of when to repaint use the fake - /// clock rather than the real system clock. - /// - /// The fake clock begins at [Instant::now()] when the [View] is - /// constructed. - /// - /// If this is enabled the fake clock can be updated with - /// [View::set_fake_clock]. - pub const fn fake_clock(self, fake_clock: bool) -> Options { - Options { fake_clock, ..self } - } - - /// Set whether progress bars are drawn to stdout, stderr, or an internal capture buffer. - /// - /// [Destination::Stdout] is the default. - /// - /// [Destination::Stderr] may be useful for programs that expect stdout to be redirected - /// to a file and that want to draw progress output that is not captured by the - /// redirection. - pub const fn destination(self, destination: Destination) -> Options { - Options { - destination, - ..self - } - } -} - -impl Default for Options { - /// Create default reasonable view options. - /// - /// This is the same as [Options::new]. - fn default() -> Options { - Options::new() - } -} diff --git a/src/options.rs b/src/options.rs new file mode 100644 index 0000000..2305c3b --- /dev/null +++ b/src/options.rs @@ -0,0 +1,135 @@ +// Copyright 2022-2023 Martin Pool. + +use std::time::Duration; + +use crate::Destination; + +/// Options controlling a View. +/// +/// These are supplied to a constructor like [View::new], and cannot be changed after the view is created. +/// +/// The default options created by [Options::default] should be reasonable +/// for most applications. +/// +/// # Example +/// +/// ``` +/// let options = nutmeg::Options::default() +/// .progress_enabled(false); // Don't draw bars, only print. +/// ``` +/// +/// Options can be constructed as a static or constant value, using [Options::new]. +/// +/// ``` +/// use std::time::Duration; +/// use nutmeg::Options; +/// +/// static NUTMEG_OPTIONS: Options = Options::new() +/// .update_interval(Duration::from_millis(100)) +/// .progress_enabled(true) +/// .destination(nutmeg::Destination::Stderr); +/// ``` +#[derive(Debug, Clone)] +pub struct Options { + /// Target interval to repaint the progress bar. + pub(crate) update_interval: Duration, + + /// How long to wait after printing output before drawing the progress bar again. + pub(crate) print_holdoff: Duration, + + /// Is the progress bar drawn at all? + pub(crate) progress_enabled: bool, + + /// Use a fake clock for testing. + pub(crate) fake_clock: bool, + + /// Write progress and messages to stdout, stderr, or a capture buffer for tests? + pub(crate) destination: Destination, +} + +impl Options { + /// Return some reasonable default options. + /// + /// The update interval and print holdoff are 100ms, the progress bar is enabled, + /// and output is sent to stdout. + pub const fn new() -> Options { + Options { + update_interval: Duration::from_millis(100), + print_holdoff: Duration::from_millis(100), + progress_enabled: true, + fake_clock: false, + destination: Destination::Stdout, + } + } + + /// Set whether the progress bar will be drawn. + /// + /// By default it is drawn, except that this value will be ignored by [View::new] if stdout is not a terminal. + pub const fn progress_enabled(self, progress_enabled: bool) -> Options { + Options { + progress_enabled, + ..self + } + } + + /// Set the minimal interval to repaint the progress bar. + /// + /// `Duration::ZERO` can be used to cause the bar to repaint on every update. + pub const fn update_interval(self, update_interval: Duration) -> Options { + Options { + update_interval, + ..self + } + } + + /// Set the minimal interval between printing a message and painting + /// the progress bar. + /// + /// This is used to avoid the bar flickering if the application is + /// repeatedly printing messages at short intervals. + /// + /// `Duration::ZERO` can be used to disable this behavior. + pub const fn print_holdoff(self, print_holdoff: Duration) -> Options { + Options { + print_holdoff, + ..self + } + } + + /// Enable use of a fake clock, for testing. + /// + /// When true, all calculations of when to repaint use the fake + /// clock rather than the real system clock. + /// + /// The fake clock begins at [Instant::now()] when the [View] is + /// constructed. + /// + /// If this is enabled the fake clock can be updated with + /// [View::set_fake_clock]. + pub const fn fake_clock(self, fake_clock: bool) -> Options { + Options { fake_clock, ..self } + } + + /// Set whether progress bars are drawn to stdout, stderr, or an internal capture buffer. + /// + /// [Destination::Stdout] is the default. + /// + /// [Destination::Stderr] may be useful for programs that expect stdout to be redirected + /// to a file and that want to draw progress output that is not captured by the + /// redirection. + pub const fn destination(self, destination: Destination) -> Options { + Options { + destination, + ..self + } + } +} + +impl Default for Options { + /// Create default reasonable view options. + /// + /// This is the same as [Options::new]. + fn default() -> Options { + Options::new() + } +}