Skip to content

Commit

Permalink
feat(config): Expose term.charset
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 22, 2024
1 parent e83d006 commit 655eb33
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 29 deletions.
30 changes: 2 additions & 28 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::util::errors::CargoResult;
use crate::util::hostname;
use crate::util::style::*;

pub use crate::util::config::Charset;

pub enum TtyWidth {
NoTty,
Known(usize),
Expand Down Expand Up @@ -39,34 +41,6 @@ impl TtyWidth {
}
}

#[derive(Copy, Clone, Debug)]
pub enum Charset {
Utf8,
Ascii,
}

impl Charset {
fn auto_with(stream: &dyn IsTerminal) -> Self {
if !stream.is_terminal() || supports_unicode::supports_unicode() {
Self::Utf8
} else {
Self::Ascii
}
}
}

impl std::str::FromStr for Charset {
type Err = &'static str;

fn from_str(s: &str) -> Result<Charset, &'static str> {
match s {
"utf8" => Ok(Charset::Utf8),
"ascii" => Ok(Charset::Ascii),
_ => Err("invalid charset"),
}
}
}

/// The requested verbosity of output.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Verbosity {
Expand Down
45 changes: 45 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,9 @@ impl Config {
if let Some(hyperlinks) = term.hyperlinks {
self.shell().set_hyperlinks(hyperlinks)?;
}
if let Some(charset) = term.charset {
self.shell().set_charset(charset)?;
}
self.progress_config = term.progress.unwrap_or_default();
self.extra_verbose = extra_verbose;
self.frozen = frozen;
Expand Down Expand Up @@ -2605,11 +2608,53 @@ struct TermConfig {
quiet: Option<bool>,
color: Option<String>,
hyperlinks: Option<bool>,
charset: Option<Charset>,
#[serde(default)]
#[serde(deserialize_with = "progress_or_string")]
progress: Option<ProgressConfig>,
}

#[derive(Copy, Clone, Debug)]
pub enum Charset {
Utf8,
Ascii,
}

impl Charset {
pub fn auto_with(stream: &dyn std::io::IsTerminal) -> Self {
if !stream.is_terminal() || supports_unicode::supports_unicode() {
Self::Utf8
} else {
Self::Ascii
}
}
}

impl std::str::FromStr for Charset {
type Err = &'static str;

fn from_str(s: &str) -> Result<Charset, &'static str> {
match s {
"utf8" => Ok(Charset::Utf8),
"ascii" => Ok(Charset::Ascii),
_ => Err("invalid charset"),
}
}
}

impl<'de> Deserialize<'de> for Charset {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
use serde::de::Error as _;
UntaggedEnumVisitor::new()
.expecting("a charset")
.string(|value| value.parse().map_err(serde_untagged::de::Error::custom))
.deserialize(d)
}
}

#[derive(Debug, Default, Deserialize)]
pub struct ProgressConfig {
pub when: ProgressWhen,
Expand Down
11 changes: 11 additions & 0 deletions src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ quiet = false # whether cargo output is quiet
verbose = false # whether cargo provides verbose output
color = 'auto' # whether cargo colorizes output
hyperlinks = true # whether cargo inserts links into output
charset = "utf8" # what cargo is limited to in rendering output
progress.when = 'auto' # whether cargo shows progress bar
progress.width = 80 # width of progress bar
```
Expand Down Expand Up @@ -1279,6 +1280,16 @@ Can be overridden with the `--color` command-line option.

Controls whether or not hyperlinks are used in the terminal.

#### `term.charset`
* Type: string
* Default: auto-detect
* Environment: `CARGO_TERM_CHARSET`

Sets what characters can be used when rendering cargo output. Possible values:

* `utf8`: Use the full set of UTF-8 characters for drawing
* `ascii`: Only use 7-bit ASCII characters for drawing

#### `term.progress.when`
* Type: string
* Default: "auto"
Expand Down
48 changes: 47 additions & 1 deletion tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ dupe-dep v2.0.0
}

#[cargo_test]
fn charset() {
fn charset_cli() {
let p = make_simple_proj();
p.cargo("tree --charset ascii")
.with_stdout(
Expand All @@ -1160,6 +1160,52 @@ foo v0.1.0 ([..]/foo)
.run();
}

#[cargo_test]
fn charset_config() {
let p = make_simple_proj();
p.cargo("tree")
.env("CARGO_TERM_CHARSET", "ascii")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
|-- a v1.0.0
| `-- b v1.0.0
| `-- c v1.0.0
`-- c v1.0.0
[build-dependencies]
`-- bdep v1.0.0
`-- b v1.0.0 (*)
[dev-dependencies]
`-- devdep v1.0.0
`-- b v1.0.0 (*)
",
)
.run();
}

#[cargo_test]
fn charset_cli_preferred_over_config() {
let p = make_simple_proj();
p.cargo("tree --charset ascii")
.env("CARGO_TERM_CHARSET", "unicode")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
|-- a v1.0.0
| `-- b v1.0.0
| `-- c v1.0.0
`-- c v1.0.0
[build-dependencies]
`-- bdep v1.0.0
`-- b v1.0.0 (*)
[dev-dependencies]
`-- devdep v1.0.0
`-- b v1.0.0 (*)
",
)
.run();
}

#[cargo_test]
fn format() {
Package::new("dep", "1.0.0").publish();
Expand Down

0 comments on commit 655eb33

Please sign in to comment.