Skip to content

Commit ebee9a4

Browse files
committed
refactor(shell): Move tree's unicode detection to shell
1 parent 6ca9398 commit ebee9a4

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use cargo::ops::Packages;
77
use cargo::util::print_available_packages;
88
use cargo::util::CargoResult;
99
use std::collections::HashSet;
10-
use std::io::IsTerminal as _;
1110
use std::str::FromStr;
1211

1312
pub fn cli() -> Command {
@@ -186,13 +185,6 @@ subtree of the package given to -p.\n\
186185
.map(|c| tree::Charset::from_str(c))
187186
.transpose()
188187
.map_err(|e| anyhow::anyhow!("{}", e))?;
189-
let charset = charset.unwrap_or_else(|| {
190-
if supports_unicode::supports_unicode() || !std::io::stdout().is_terminal() {
191-
tree::Charset::Utf8
192-
} else {
193-
tree::Charset::Ascii
194-
}
195-
});
196188
let opts = tree::TreeOptions {
197189
cli_features: args.cli_features()?,
198190
packages,

src/cargo/core/shell.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ impl Shell {
5353
color_choice: auto_clr,
5454
hyperlinks: supports_hyperlinks(),
5555
stderr_tty: std::io::stderr().is_terminal(),
56+
stdout_unicode: supports_unicode(&std::io::stdout()),
57+
stderr_unicode: supports_unicode(&std::io::stderr()),
5658
},
5759
verbosity: Verbosity::Verbose,
5860
needs_clear: false,
@@ -259,6 +261,20 @@ impl Shell {
259261
Ok(())
260262
}
261263

264+
pub fn out_unicode(&self) -> bool {
265+
match &self.output {
266+
ShellOut::Write(_) => true,
267+
ShellOut::Stream { stdout_unicode, .. } => *stdout_unicode,
268+
}
269+
}
270+
271+
pub fn err_unicode(&self) -> bool {
272+
match &self.output {
273+
ShellOut::Write(_) => true,
274+
ShellOut::Stream { stderr_unicode, .. } => *stderr_unicode,
275+
}
276+
}
277+
262278
/// Gets the current color choice.
263279
///
264280
/// If we are not using a color stream, this will always return `Never`, even if the color
@@ -384,6 +400,8 @@ enum ShellOut {
384400
stderr_tty: bool,
385401
color_choice: ColorChoice,
386402
hyperlinks: bool,
403+
stdout_unicode: bool,
404+
stderr_unicode: bool,
387405
},
388406
}
389407

@@ -519,6 +537,10 @@ fn supports_color(choice: anstream::ColorChoice) -> bool {
519537
}
520538
}
521539

540+
fn supports_unicode(stream: &dyn IsTerminal) -> bool {
541+
!stream.is_terminal() || supports_unicode::supports_unicode()
542+
}
543+
522544
fn supports_hyperlinks() -> bool {
523545
#[allow(clippy::disallowed_methods)] // We are reading the state of the system, not config
524546
if std::env::var_os("TERM_PROGRAM").as_deref() == Some(std::ffi::OsStr::new("iTerm.app")) {

src/cargo/ops/tree/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct TreeOptions {
4040
/// `invert`.
4141
pub duplicates: bool,
4242
/// The style of characters to use.
43-
pub charset: Charset,
43+
pub charset: Option<Charset>,
4444
/// A format string indicating how each package should be displayed.
4545
pub format: String,
4646
/// Includes features in the tree as separate nodes.
@@ -68,6 +68,7 @@ impl Target {
6868
}
6969
}
7070

71+
#[derive(Copy, Clone)]
7172
pub enum Charset {
7273
Utf8,
7374
Ascii,
@@ -236,7 +237,14 @@ fn print(
236237
let format = Pattern::new(&opts.format)
237238
.with_context(|| format!("tree format `{}` not valid", opts.format))?;
238239

239-
let symbols = match opts.charset {
240+
let charset = opts.charset.unwrap_or_else(|| {
241+
if gctx.shell().out_unicode() {
242+
Charset::Utf8
243+
} else {
244+
Charset::Ascii
245+
}
246+
});
247+
let symbols = match charset {
240248
Charset::Utf8 => &UTF8_SYMBOLS,
241249
Charset::Ascii => &ASCII_SYMBOLS,
242250
};

0 commit comments

Comments
 (0)