termprogress/
lib.rs

1#![cfg_attr(nightly, feature(never_type))] 
2
3#![allow(dead_code)]
4
5macro_rules! flush {
6    ($stream:expr) => {
7	{
8	    #![allow(unused_imports)]
9	    use std::io::Write;
10	    let _ = $stream.flush();
11	}
12    };
13    () => {
14	    use std::io::Write;
15	    let _ = ::std::io::stdout().flush();
16    };
17    (? $stream:expr) => {
18	{
19	    #[allow(unused_imports)]
20	    use std::io::Write;
21	    $stream.flush()
22	}
23    };
24    (?) => {
25	    use std::io::Write;
26	    ::std::io::stdout().flush()
27    };
28}
29
30/// The default place to write bars to if an output is not user-specified.
31pub(crate) type DefaultOutputDevice = std::io::Stdout;
32/// A function that creates the default output device object for constructing a progress bar.
33///
34/// This must return multiple handles, since multiple bars can exist throughout the program at overlapping lifetimes.
35/// `DefaultOutputDevice` should internally manage this state.
36pub(crate) const CREATE_DEFAULT_OUTPUT_DEVICE_FUNC: fn () -> DefaultOutputDevice = std::io::stdout;
37
38/// Create an object for the default output device.
39#[inline] 
40pub(crate) fn create_default_output_device() -> DefaultOutputDevice
41{
42    CREATE_DEFAULT_OUTPUT_DEVICE_FUNC()
43}
44
45#[cfg(feature="size")]
46#[inline(always)] 
47fn terminal_size_of(f: &(impl AsRawFd + ?Sized)) -> Option<(terminal_size::Width, terminal_size::Height)>
48{
49    terminal_size::terminal_size_using_fd(f.as_raw_fd())
50}
51
52use atomic_refcell::AtomicRefCell;
53
54//#[cfg(feature="size")] TODO: How to add `AsRawFd` bound to `Bar` *only* when `size` feature is enabled?
55use std::os::unix::io::*;
56
57mod util;
58mod inter;
59pub use inter::*;
60
61pub mod progress;
62pub mod wheel;
63pub mod spinner;
64pub mod silent;
65
66/// Returns true if `stdout` has a terminal output and can be used with terminal size responsiveness.
67///
68/// Requires `size` feature.
69#[cfg(feature="size")] 
70pub fn has_terminal_output_default() -> bool
71{
72    terminal_size::terminal_size().is_some()
73}
74
75/// Returns true if `f` has a terminal output and can be used with terminal size responsiveness.
76///
77/// Requires `size` feature.
78#[cfg(feature="size")] 
79pub fn has_terminal_output(f: &(impl AsRawFd + ?Sized)) -> bool
80{
81    terminal_size::terminal_size_using_fd(f.as_raw_fd()).is_some()
82}
83
84/// The prelude exposes the traits for spinners and progress bars, and the `spinner::Spin` and `progress::Bar` types for easy access and use.
85pub mod prelude {
86    pub use super::inter::*;
87    pub use super::{
88	spinner::Spin,
89	progress::Bar,
90	silent::Silent,
91    };
92}