rhg: Add parsing for the --color global CLI argument
authorSimon Sapin <simon.sapin@octobus.net>
Thu, 10 Feb 2022 13:56:43 +0100
changeset 48742 d4a5c2197208
parent 48741 f591b377375f
child 48743 39c447e03dbc
rhg: Add parsing for the --color global CLI argument Differential Revision: https://phab.mercurial-scm.org/D12166
rust/hg-core/src/config/config.rs
rust/hg-core/src/config/layer.rs
rust/rhg/src/main.rs
--- a/rust/hg-core/src/config/config.rs	Thu Feb 10 12:52:25 2022 +0100
+++ b/rust/hg-core/src/config/config.rs	Thu Feb 10 13:56:43 2022 +0100
@@ -139,13 +139,19 @@
         Ok(config)
     }
 
-    pub fn load_cli_args_config(
+    pub fn load_cli_args(
         &mut self,
         cli_config_args: impl IntoIterator<Item = impl AsRef<[u8]>>,
+        color_arg: Option<Vec<u8>>,
     ) -> Result<(), ConfigError> {
         if let Some(layer) = ConfigLayer::parse_cli_args(cli_config_args)? {
             self.layers.push(layer)
         }
+        if let Some(arg) = color_arg {
+            let mut layer = ConfigLayer::new(ConfigOrigin::CommandLineColor);
+            layer.add(b"ui"[..].into(), b"color"[..].into(), arg, None);
+            self.layers.push(layer)
+        }
         Ok(())
     }
 
--- a/rust/hg-core/src/config/layer.rs	Thu Feb 10 12:52:25 2022 +0100
+++ b/rust/hg-core/src/config/layer.rs	Thu Feb 10 13:56:43 2022 +0100
@@ -301,10 +301,11 @@
     File(PathBuf),
     /// From a `--config` CLI argument
     CommandLine,
+    /// From a `--color` CLI argument
+    CommandLineColor,
     /// From environment variables like `$PAGER` or `$EDITOR`
     Environment(Vec<u8>),
-    /* TODO cli
-     * TODO defaults (configitems.py)
+    /* TODO defaults (configitems.py)
      * TODO extensions
      * TODO Python resources?
      * Others? */
@@ -318,6 +319,7 @@
         match self {
             ConfigOrigin::File(p) => out.write_all(&get_bytes_from_path(p)),
             ConfigOrigin::CommandLine => out.write_all(b"--config"),
+            ConfigOrigin::CommandLineColor => out.write_all(b"--color"),
             ConfigOrigin::Environment(e) => write_bytes!(out, b"${}", e),
         }
     }
--- a/rust/rhg/src/main.rs	Thu Feb 10 12:52:25 2022 +0100
+++ b/rust/rhg/src/main.rs	Thu Feb 10 13:56:43 2022 +0100
@@ -66,6 +66,14 @@
                 .takes_value(true)
                 .global(true),
         )
+        .arg(
+            Arg::with_name("color")
+                .help("when to colorize (boolean, always, auto, never, or debug)")
+                .long("--color")
+                .value_name("TYPE")
+                .takes_value(true)
+                .global(true),
+        )
         .version("0.0.1");
     let app = add_subcommand_args(app);
 
@@ -179,7 +187,7 @@
         });
 
     non_repo_config
-        .load_cli_args_config(early_args.config)
+        .load_cli_args(early_args.config, early_args.color)
         .unwrap_or_else(|error| {
             exit(
                 &initial_current_dir,
@@ -526,6 +534,8 @@
 struct EarlyArgs {
     /// Values of all `--config` arguments. (Possibly none)
     config: Vec<Vec<u8>>,
+    /// Value of all the `--color` argument, if any.
+    color: Option<Vec<u8>>,
     /// Value of the `-R` or `--repository` argument, if any.
     repo: Option<Vec<u8>>,
     /// Value of the `--cwd` argument, if any.
@@ -536,6 +546,7 @@
     fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
         let mut args = args.into_iter().map(get_bytes_from_os_str);
         let mut config = Vec::new();
+        let mut color = None;
         let mut repo = None;
         let mut cwd = None;
         // Use `while let` instead of `for` so that we can also call
@@ -549,6 +560,14 @@
                 config.push(value.to_owned())
             }
 
+            if arg == b"--color" {
+                if let Some(value) = args.next() {
+                    color = Some(value)
+                }
+            } else if let Some(value) = arg.drop_prefix(b"--color=") {
+                color = Some(value.to_owned())
+            }
+
             if arg == b"--cwd" {
                 if let Some(value) = args.next() {
                     cwd = Some(value)
@@ -567,7 +586,12 @@
                 repo = Some(value.to_owned())
             }
         }
-        Self { config, repo, cwd }
+        Self {
+            config,
+            color,
+            repo,
+            cwd,
+        }
     }
 }