Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 212ce36

Browse files
committed
Add render-line-autoconfigure feature toggle
1 parent 0d41f11 commit 212ce36

8 files changed

Lines changed: 62 additions & 16 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ doctest = true
1515
[[example]]
1616
name = "dashboard"
1717
path = "examples/dashboard.rs"
18-
required-features = ["render-tui", "render-tui-crossterm", "render-line", "render-line-crossterm", "ctrlc"]
18+
required-features = ["render-tui", "render-tui-crossterm", "render-line", "render-line-crossterm", "ctrlc", "render-line-autoconfigure"]
1919

2020
[[example]]
2121
name = "dashboard-termion"
@@ -58,6 +58,7 @@ render-tui = ["tui",
5858
render-line = ["crosstermion/color", "humantime", "unicode-width"]
5959
render-line-crossterm = ["crosstermion/crossterm"]
6060
render-line-termion = ["crosstermion/termion"]
61+
render-line-autoconfigure = ["atty"]
6162

6263
localtime = ["time"]
6364

@@ -86,6 +87,7 @@ time = { version = "=0.2.22", optional = true, features = ["std"], default-featu
8687

8788
# line renderer
8889
ctrlc = { version = "3.1.4", optional = true, default-features = false, features = ['termination'] }
90+
atty = { version = "0.2.14", optional = true }
8991

9092
# units
9193
bytesize = { version = "1.0.1", optional = true }

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ check: ## build features in commmon combination to be sure it all stays together
1616
cargo check --features render-line,render-line-termion
1717
cargo check --features render-line,render-line-crossterm
1818
cargo check --features render-line,render-line-termion,render-tui,render-tui-termion --example dashboard-termion
19-
cargo check --features render-line,render-line-crossterm,render-tui,render-tui-crossterm,ctrlc --example dashboard
19+
cargo check --features render-line,render-line-crossterm,render-tui,render-tui-crossterm,ctrlc,render-line-autoconfigure --example dashboard
2020
cargo check --features unit-bytes,unit-duration,unit-human,render-tui,render-tui-crossterm,render-line,render-line-crossterm,ctrlc --example units
2121
cargo check
2222

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ This crate comes with various cargo features to tailor it to your needs.
4141
* **render-line-crossterm** - use the _crossterm_ backend, useful for working on windows
4242
* **render-line-termion** - use the _termion_ backend, useful for lean unix-only builds
4343
* _Optional features_
44+
* **render-line-autoconfigure**
45+
* If enabled, calls to `render::line::Options::auto_configure()` will configure the display based on whether or not we are in a terminal
46+
and set its color mode based on what's possible or desired.
4447
* **ctrlc**
4548
* If set, and the `hide_cursor` line renderer option is set, the cursor will be hidden **and** *SIG_INT* and *SIG_TERM* handlers will be
4649
installed to reset the cursor on exit. Otherwise you have to make sure to call `shutdown_and_wait()` on the `JoinHandle` returned

examples/shared/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct Options {
7373
#[argh(option, short = 'R')]
7474
pub renderer: Option<String>,
7575

76-
/// if set, coloring of the line renderer is forcefully disabled
76+
/// has not effect - use the NO_COLOR environment variable instead.
7777
#[argh(switch)]
7878
pub no_line_color: bool,
7979
}

examples/shared/mod.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,12 @@ pub fn launch_ambient_gui(
3030
let mut interruptible = true;
3131
let render_fut = match renderer {
3232
"line" => async move {
33-
let output_is_terminal = atty::is(atty::Stream::Stderr);
3433
let mut handle = line::render(
3534
std::io::stderr(),
3635
progress,
3736
line::Options {
38-
output_is_terminal,
39-
#[cfg(feature = "ctrlc")]
40-
hide_cursor: true,
41-
#[cfg(not(feature = "ctrlc"))]
42-
hide_cursor: false,
43-
terminal_dimensions: crosstermion::terminal::size()
44-
.ok()
45-
.map(|(w, h)| args.line_column_count.map(|width| (width, h)).unwrap_or((w, h)))
46-
.unwrap_or((80, 20)),
37+
terminal_dimensions: args.line_column_count.map(|width| (width, 20)).unwrap_or((80, 20)),
4738
timestamp: args.line_timestamp,
48-
colored: !args.no_line_color && output_is_terminal && crosstermion::color::allowed(),
4939
level_filter: Some(RangeInclusive::new(
5040
args.line_start.unwrap_or(1),
5141
args.line_end.unwrap_or(2),
@@ -54,7 +44,9 @@ pub fn launch_ambient_gui(
5444
frames_per_second: args.fps,
5545
keep_running_if_progress_is_empty: true,
5646
throughput,
57-
},
47+
..Default::default()
48+
}
49+
.auto_configure(line::StreamKind::Stderr),
5850
);
5951
handle.disconnect();
6052
blocking::unblock(move || handle.wait()).await;

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Please have a look at the [dashboard demo](https://github.com/Byron/crates-io-cl
3535
3636
Run it with `cargo run --example dashboard` and see what else it can do by checking out `cargo run --example dashboard -- --help`.
3737
*/
38+
#[cfg(feature = "atty")]
39+
pub use atty;
40+
3841
#[cfg(feature = "progress-tree")]
3942
///
4043
pub mod tree;

src/render/line/engine.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Options {
1515
pub output_is_terminal: bool,
1616

1717
/// If true, _(default: true)_ we will display color. You should use `output_is_terminal && crosstermion::should_colorize()`
18+
/// to determine this value.
1819
///
1920
/// Please note that you can enforce color even if the output stream is not connected to a terminal by setting
2021
/// this field to true.
@@ -62,6 +63,51 @@ pub struct Options {
6263
pub keep_running_if_progress_is_empty: bool,
6364
}
6465

66+
/// The kind of stream to use for auto-configuration.
67+
pub enum StreamKind {
68+
/// Standard output
69+
Stdout,
70+
/// Standard error
71+
Stderr,
72+
}
73+
74+
#[cfg(feature = "render-line-autoconfigure")]
75+
impl From<StreamKind> for atty::Stream {
76+
fn from(s: StreamKind) -> Self {
77+
match s {
78+
StreamKind::Stdout => atty::Stream::Stdout,
79+
StreamKind::Stderr => atty::Stream::Stderr,
80+
}
81+
}
82+
}
83+
84+
/// Convenience
85+
impl Options {
86+
/// Automatically configure (and overwrite) the following fields based on terminal configuration.
87+
///
88+
/// * output_is_terminal
89+
/// * colored
90+
/// * terminal_dimensions
91+
/// * hide-cursor (based on presence of 'ctrlc' feature.
92+
#[cfg(feature = "render-line-autoconfigure")]
93+
pub fn auto_configure(mut self, output: StreamKind) -> Self {
94+
self.output_is_terminal = atty::is(output.into());
95+
self.colored = self.output_is_terminal && crosstermion::color::allowed();
96+
self.terminal_dimensions = crosstermion::terminal::size().unwrap_or((80, 20));
97+
self.auto_hide_cursor();
98+
self
99+
}
100+
#[cfg(all(feature = "render-line-autoconfigure", feature = "ctrlc"))]
101+
fn auto_hide_cursor(&mut self) {
102+
self.hide_cursor = true;
103+
}
104+
#[cfg(not(feature = "render-line-autoconfigure"))]
105+
/// No-op - only available with the `render-line-autoconfigure` feature toggle.
106+
pub fn auto_configure(self, _output: StreamKind) -> Self {
107+
self
108+
}
109+
}
110+
65111
impl Default for Options {
66112
fn default() -> Self {
67113
Options {

src/render/line/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ compile_error!("Please choose either one of these features: 'render-line-crosste
77
mod draw;
88
mod engine;
99

10-
pub use engine::*;
10+
pub use engine::{render, JoinHandle, Options, StreamKind};

0 commit comments

Comments
 (0)