Show More
@@ -139,13 +139,19 b' impl Config {' | |||
|
139 | 139 | Ok(config) |
|
140 | 140 | } |
|
141 | 141 | |
|
142 |
pub fn load_cli_args |
|
|
142 | pub fn load_cli_args( | |
|
143 | 143 | &mut self, |
|
144 | 144 | cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>, |
|
145 | color_arg: Option<Vec<u8>>, | |
|
145 | 146 | ) -> Result<(), ConfigError> { |
|
146 | 147 | if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? { |
|
147 | 148 | self.layers.push(layer) |
|
148 | 149 | } |
|
150 | if let Some(arg) = color_arg { | |
|
151 | let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor); | |
|
152 | layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None); | |
|
153 | self.layers.push(layer) | |
|
154 | } | |
|
149 | 155 | Ok(()) |
|
150 | 156 | } |
|
151 | 157 |
@@ -301,10 +301,11 b' pub enum ConfigOrigin {' | |||
|
301 | 301 | File(PathBuf), |
|
302 | 302 | /// From a `--config` CLI argument |
|
303 | 303 | CommandLine, |
|
304 | /// From a `--color` CLI argument | |
|
305 | CommandLineColor, | |
|
304 | 306 | /// From environment variables like `$PAGER` or `$EDITOR` |
|
305 | 307 | Environment(Vec<u8>), |
|
306 | /* TODO cli | |
|
307 | * TODO defaults (configitems.py) | |
|
308 | /* TODO defaults (configitems.py) | |
|
308 | 309 | * TODO extensions |
|
309 | 310 | * TODO Python resources? |
|
310 | 311 | * Others? */ |
@@ -318,6 +319,7 b' impl DisplayBytes for ConfigOrigin {' | |||
|
318 | 319 | match self { |
|
319 | 320 | ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)), |
|
320 | 321 | ConfigOrigin::CommandLine => out.write_all(b"--config"), |
|
322 | ConfigOrigin::CommandLineColor => out.write_all(b"--color"), | |
|
321 | 323 | ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e), |
|
322 | 324 | } |
|
323 | 325 | } |
@@ -66,6 +66,14 b' fn main_with_result(' | |||
|
66 | 66 | .takes_value(true) |
|
67 | 67 | .global(true), |
|
68 | 68 | ) |
|
69 | .arg( | |
|
70 | Arg::with_name("color") | |
|
71 | .help("when to colorize (boolean, always, auto, never, or debug)") | |
|
72 | .long("--color") | |
|
73 | .value_name("TYPE") | |
|
74 | .takes_value(true) | |
|
75 | .global(true), | |
|
76 | ) | |
|
69 | 77 | .version("0.0.1"); |
|
70 | 78 | let app = add_subcommand_args(app); |
|
71 | 79 | |
@@ -179,7 +187,7 b' fn main() {' | |||
|
179 | 187 | }); |
|
180 | 188 | |
|
181 | 189 | non_repo_config |
|
182 |
.load_cli_args |
|
|
190 | .load_cli_args(early_args.config, early_args.color) | |
|
183 | 191 | .unwrap_or_else(|error| { |
|
184 | 192 | exit( |
|
185 | 193 | &initial_current_dir, |
@@ -526,6 +534,8 b' struct NoRepoInCwdError {' | |||
|
526 | 534 | struct EarlyArgs { |
|
527 | 535 | /// Values of all `--config` arguments. (Possibly none) |
|
528 | 536 | config: Vec<Vec<u8>>, |
|
537 | /// Value of all the `--color` argument, if any. | |
|
538 | color: Option<Vec<u8>>, | |
|
529 | 539 | /// Value of the `-R` or `--repository` argument, if any. |
|
530 | 540 | repo: Option<Vec<u8>>, |
|
531 | 541 | /// Value of the `--cwd` argument, if any. |
@@ -536,6 +546,7 b' impl EarlyArgs {' | |||
|
536 | 546 | fn parse(args: impl IntoIterator<Item = OsString>) -> Self { |
|
537 | 547 | let mut args = args.into_iter().map(get_bytes_from_os_str); |
|
538 | 548 | let mut config = Vec::new(); |
|
549 | let mut color = None; | |
|
539 | 550 | let mut repo = None; |
|
540 | 551 | let mut cwd = None; |
|
541 | 552 | // Use `while let` instead of `for` so that we can also call |
@@ -549,6 +560,14 b' impl EarlyArgs {' | |||
|
549 | 560 | config.push(value.to_owned()) |
|
550 | 561 | } |
|
551 | 562 | |
|
563 | if arg == b"--color" { | |
|
564 | if let Some(value) = args.next() { | |
|
565 | color = Some(value) | |
|
566 | } | |
|
567 | } else if let Some(value) = arg.drop_prefix(b"--color=") { | |
|
568 | color = Some(value.to_owned()) | |
|
569 | } | |
|
570 | ||
|
552 | 571 | if arg == b"--cwd" { |
|
553 | 572 | if let Some(value) = args.next() { |
|
554 | 573 | cwd = Some(value) |
@@ -567,7 +586,12 b' impl EarlyArgs {' | |||
|
567 | 586 | repo = Some(value.to_owned()) |
|
568 | 587 | } |
|
569 | 588 | } |
|
570 | Self { config, repo, cwd } | |
|
589 | Self { | |
|
590 | config, | |
|
591 | color, | |
|
592 | repo, | |
|
593 | cwd, | |
|
594 | } | |
|
571 | 595 | } |
|
572 | 596 | } |
|
573 | 597 |
General Comments 0
You need to be logged in to leave comments.
Login now