From 61438484bdc76601a185298f14337cfb4d5b4e0b Mon Sep 17 00:00:00 2001 From: Artyom Belousov Date: Tue, 19 Jul 2022 16:07:50 +0300 Subject: [PATCH] feat(status): Add pipestatus_segment_format option to status module (#4103) Add pipestatus_segment_format --- .github/config-schema.json | 6 ++++++ docs/config/README.md | 33 +++++++++++++++++---------------- src/configs/status.rs | 3 +++ src/modules/status.rs | 26 +++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 99d164f7..351dd985 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -4521,6 +4521,12 @@ "default": "\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)", "type": "string" }, + "pipestatus_segment_format": { + "type": [ + "string", + "null" + ] + }, "disabled": { "default": true, "type": "boolean" diff --git a/docs/config/README.md b/docs/config/README.md index 7316b86d..cba894e7 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3322,22 +3322,23 @@ To enable it, set `disabled` to `false` in your configuration file. ### Options -| Option | Default | Description | -| ----------------------- | ----------------------------------------------------------------------------- | ------------------------------------------------------- | -| `format` | `"[$symbol$status]($style) "` | The format of the module | -| `symbol` | `"✖"` | The symbol displayed on program error | -| `success_symbol` | `""` | The symbol displayed on program success | -| `not_executable_symbol` | `"🚫"` | The symbol displayed when file isn't executable | -| `not_found_symbol` | `"🔍"` | The symbol displayed when the command can't be found | -| `sigint_symbol` | `"🧱"` | The symbol displayed on SIGINT (Ctrl + c) | -| `signal_symbol` | `"⚡"` | The symbol displayed on any signal | -| `style` | `"bold red"` | The style for the module. | -| `recognize_signal_code` | `true` | Enable signal mapping from exit code | -| `map_symbol` | `false` | Enable symbols mapping from exit code | -| `pipestatus` | `false` | Enable pipestatus reporting | -| `pipestatus_separator` | ` | ` | -| `pipestatus_format` | `\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)` | The format of the module when the command is a pipeline | -| `disabled` | `true` | Disables the `status` module. | +| Option | Default | Description | +| --------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------- | +| `format` | `"[$symbol$status]($style) "` | The format of the module | +| `symbol` | `"✖"` | The symbol displayed on program error | +| `success_symbol` | `""` | The symbol displayed on program success | +| `not_executable_symbol` | `"🚫"` | The symbol displayed when file isn't executable | +| `not_found_symbol` | `"🔍"` | The symbol displayed when the command can't be found | +| `sigint_symbol` | `"🧱"` | The symbol displayed on SIGINT (Ctrl + c) | +| `signal_symbol` | `"⚡"` | The symbol displayed on any signal | +| `style` | `"bold red"` | The style for the module. | +| `recognize_signal_code` | `true` | Enable signal mapping from exit code | +| `map_symbol` | `false` | Enable symbols mapping from exit code | +| `pipestatus` | `false` | Enable pipestatus reporting | +| `pipestatus_separator` | | | The symbol used to separate pipestatus segments | +| `pipestatus_format` | `\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)` | The format of the module when the command is a pipeline | +| `pipestatus_segment_format` | | When specified, replaces `format` when formatting pipestatus segments | +| `disabled` | `true` | Disables the `status` module. | ### Variables diff --git a/src/configs/status.rs b/src/configs/status.rs index 422369b6..839c57e6 100644 --- a/src/configs/status.rs +++ b/src/configs/status.rs @@ -17,6 +17,8 @@ pub struct StatusConfig<'a> { pub pipestatus: bool, pub pipestatus_separator: &'a str, pub pipestatus_format: &'a str, + #[serde(skip_serializing_if = "Option::is_none")] + pub pipestatus_segment_format: Option<&'a str>, pub disabled: bool, } @@ -37,6 +39,7 @@ impl<'a> Default for StatusConfig<'a> { pipestatus_separator: "|", pipestatus_format: "\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)", + pipestatus_segment_format: None, disabled: true, } } diff --git a/src/modules/status.rs b/src/modules/status.rs index e1c7e8cb..444b5a30 100644 --- a/src/modules/status.rs +++ b/src/modules/status.rs @@ -54,12 +54,14 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } + let segment_format = config.pipestatus_segment_format.unwrap_or(config.format); + // Create pipestatus string let pipestatus = match pipestatus_status { PipeStatusStatus::Pipe(pipestatus) => pipestatus .iter() .map( - |ec| match format_exit_code(ec.as_str(), config.format, None, &config, context) { + |ec| match format_exit_code(ec.as_str(), segment_format, None, &config, context) { Ok(segments) => segments .into_iter() .map(|s| s.to_string()) @@ -684,4 +686,26 @@ mod tests { assert_eq!(expected, actual); } } + + #[test] + fn pipestatus_segment_format() { + let pipe_exit_code = &[0, 1]; + let main_exit_code = 1; + + let expected = Some("[0]|[1] => <1>".to_string()); + let actual = ModuleRenderer::new("status") + .config(toml::toml! { + [status] + format = "\\($status\\)" + pipestatus = true + pipestatus_separator = "|" + pipestatus_format = "$pipestatus => <$status>" + pipestatus_segment_format = "\\[$status\\]" + disabled = false + }) + .status(main_exit_code) + .pipestatus(pipe_exit_code) + .collect(); + assert_eq!(expected, actual); + } }