Skip to content

Commit 655eb33

Browse files
committed
feat(config): Expose term.charset
1 parent e83d006 commit 655eb33

File tree

4 files changed

+105
-29
lines changed

4 files changed

+105
-29
lines changed

src/cargo/core/shell.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::util::errors::CargoResult;
99
use crate::util::hostname;
1010
use crate::util::style::*;
1111

12+
pub use crate::util::config::Charset;
13+
1214
pub enum TtyWidth {
1315
NoTty,
1416
Known(usize),
@@ -39,34 +41,6 @@ impl TtyWidth {
3941
}
4042
}
4143

42-
#[derive(Copy, Clone, Debug)]
43-
pub enum Charset {
44-
Utf8,
45-
Ascii,
46-
}
47-
48-
impl Charset {
49-
fn auto_with(stream: &dyn IsTerminal) -> Self {
50-
if !stream.is_terminal() || supports_unicode::supports_unicode() {
51-
Self::Utf8
52-
} else {
53-
Self::Ascii
54-
}
55-
}
56-
}
57-
58-
impl std::str::FromStr for Charset {
59-
type Err = &'static str;
60-
61-
fn from_str(s: &str) -> Result<Charset, &'static str> {
62-
match s {
63-
"utf8" => Ok(Charset::Utf8),
64-
"ascii" => Ok(Charset::Ascii),
65-
_ => Err("invalid charset"),
66-
}
67-
}
68-
}
69-
7044
/// The requested verbosity of output.
7145
#[derive(Debug, Clone, Copy, PartialEq)]
7246
pub enum Verbosity {

src/cargo/util/config/mod.rs

+45
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,9 @@ impl Config {
10551055
if let Some(hyperlinks) = term.hyperlinks {
10561056
self.shell().set_hyperlinks(hyperlinks)?;
10571057
}
1058+
if let Some(charset) = term.charset {
1059+
self.shell().set_charset(charset)?;
1060+
}
10581061
self.progress_config = term.progress.unwrap_or_default();
10591062
self.extra_verbose = extra_verbose;
10601063
self.frozen = frozen;
@@ -2605,11 +2608,53 @@ struct TermConfig {
26052608
quiet: Option<bool>,
26062609
color: Option<String>,
26072610
hyperlinks: Option<bool>,
2611+
charset: Option<Charset>,
26082612
#[serde(default)]
26092613
#[serde(deserialize_with = "progress_or_string")]
26102614
progress: Option<ProgressConfig>,
26112615
}
26122616

2617+
#[derive(Copy, Clone, Debug)]
2618+
pub enum Charset {
2619+
Utf8,
2620+
Ascii,
2621+
}
2622+
2623+
impl Charset {
2624+
pub fn auto_with(stream: &dyn std::io::IsTerminal) -> Self {
2625+
if !stream.is_terminal() || supports_unicode::supports_unicode() {
2626+
Self::Utf8
2627+
} else {
2628+
Self::Ascii
2629+
}
2630+
}
2631+
}
2632+
2633+
impl std::str::FromStr for Charset {
2634+
type Err = &'static str;
2635+
2636+
fn from_str(s: &str) -> Result<Charset, &'static str> {
2637+
match s {
2638+
"utf8" => Ok(Charset::Utf8),
2639+
"ascii" => Ok(Charset::Ascii),
2640+
_ => Err("invalid charset"),
2641+
}
2642+
}
2643+
}
2644+
2645+
impl<'de> Deserialize<'de> for Charset {
2646+
fn deserialize<D>(d: D) -> Result<Self, D::Error>
2647+
where
2648+
D: serde::de::Deserializer<'de>,
2649+
{
2650+
use serde::de::Error as _;
2651+
UntaggedEnumVisitor::new()
2652+
.expecting("a charset")
2653+
.string(|value| value.parse().map_err(serde_untagged::de::Error::custom))
2654+
.deserialize(d)
2655+
}
2656+
}
2657+
26132658
#[derive(Debug, Default, Deserialize)]
26142659
pub struct ProgressConfig {
26152660
pub when: ProgressWhen,

src/doc/src/reference/config.md

+11
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ quiet = false # whether cargo output is quiet
181181
verbose = false # whether cargo provides verbose output
182182
color = 'auto' # whether cargo colorizes output
183183
hyperlinks = true # whether cargo inserts links into output
184+
charset = "utf8" # what cargo is limited to in rendering output
184185
progress.when = 'auto' # whether cargo shows progress bar
185186
progress.width = 80 # width of progress bar
186187
```
@@ -1279,6 +1280,16 @@ Can be overridden with the `--color` command-line option.
12791280

12801281
Controls whether or not hyperlinks are used in the terminal.
12811282

1283+
#### `term.charset`
1284+
* Type: string
1285+
* Default: auto-detect
1286+
* Environment: `CARGO_TERM_CHARSET`
1287+
1288+
Sets what characters can be used when rendering cargo output. Possible values:
1289+
1290+
* `utf8`: Use the full set of UTF-8 characters for drawing
1291+
* `ascii`: Only use 7-bit ASCII characters for drawing
1292+
12821293
#### `term.progress.when`
12831294
* Type: string
12841295
* Default: "auto"

tests/testsuite/tree.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ dupe-dep v2.0.0
11391139
}
11401140

11411141
#[cargo_test]
1142-
fn charset() {
1142+
fn charset_cli() {
11431143
let p = make_simple_proj();
11441144
p.cargo("tree --charset ascii")
11451145
.with_stdout(
@@ -1160,6 +1160,52 @@ foo v0.1.0 ([..]/foo)
11601160
.run();
11611161
}
11621162

1163+
#[cargo_test]
1164+
fn charset_config() {
1165+
let p = make_simple_proj();
1166+
p.cargo("tree")
1167+
.env("CARGO_TERM_CHARSET", "ascii")
1168+
.with_stdout(
1169+
"\
1170+
foo v0.1.0 ([..]/foo)
1171+
|-- a v1.0.0
1172+
| `-- b v1.0.0
1173+
| `-- c v1.0.0
1174+
`-- c v1.0.0
1175+
[build-dependencies]
1176+
`-- bdep v1.0.0
1177+
`-- b v1.0.0 (*)
1178+
[dev-dependencies]
1179+
`-- devdep v1.0.0
1180+
`-- b v1.0.0 (*)
1181+
",
1182+
)
1183+
.run();
1184+
}
1185+
1186+
#[cargo_test]
1187+
fn charset_cli_preferred_over_config() {
1188+
let p = make_simple_proj();
1189+
p.cargo("tree --charset ascii")
1190+
.env("CARGO_TERM_CHARSET", "unicode")
1191+
.with_stdout(
1192+
"\
1193+
foo v0.1.0 ([..]/foo)
1194+
|-- a v1.0.0
1195+
| `-- b v1.0.0
1196+
| `-- c v1.0.0
1197+
`-- c v1.0.0
1198+
[build-dependencies]
1199+
`-- bdep v1.0.0
1200+
`-- b v1.0.0 (*)
1201+
[dev-dependencies]
1202+
`-- devdep v1.0.0
1203+
`-- b v1.0.0 (*)
1204+
",
1205+
)
1206+
.run();
1207+
}
1208+
11631209
#[cargo_test]
11641210
fn format() {
11651211
Package::new("dep", "1.0.0").publish();

0 commit comments

Comments
 (0)