feat(status): Support formatting of pipestatus separator (#4264)

* Support formatting of pipestatus separator

* Format pipestatus separator with each pipestatus

* Add third exit code to pipestatus test

* Clean up pipestatus mapping

* Add comment that was removed
This commit is contained in:
Aaron Kollasch 2022-10-07 05:44:14 -04:00 committed by GitHub
parent 1480d121f4
commit 6e35dfa85a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 13 deletions

View File

@ -3407,7 +3407,7 @@ To enable it, set `disabled` to `false` in your configuration file.
| `recognize_signal_code` | `true` | Enable signal mapping from exit code | | `recognize_signal_code` | `true` | Enable signal mapping from exit code |
| `map_symbol` | `false` | Enable symbols mapping from exit code | | `map_symbol` | `false` | Enable symbols mapping from exit code |
| `pipestatus` | `false` | Enable pipestatus reporting | | `pipestatus` | `false` | Enable pipestatus reporting |
| `pipestatus_separator` | <code>&vert;</code> | The symbol used to separate pipestatus segments | | `pipestatus_separator` | <code>&vert;</code> | The symbol used to separate pipestatus segments (supports formatting) |
| `pipestatus_format` | `'\[$pipestatus\] => [$symbol$common_meaning$signal_name$maybe_int]($style)'` | The format of the module when the command is a pipeline | | `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 | | `pipestatus_segment_format` | | When specified, replaces `format` when formatting pipestatus segments |
| `disabled` | `true` | Disables the `status` module. | | `disabled` | `true` | Disables the `status` module. |

View File

@ -55,22 +55,30 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
let segment_format = config.pipestatus_segment_format.unwrap_or(config.format); let segment_format = config.pipestatus_segment_format.unwrap_or(config.format);
let segment_format_with_separator = [segment_format, config.pipestatus_separator].join("");
// Create pipestatus string // Create pipestatus string
let pipestatus = match pipestatus_status { let pipestatus = match pipestatus_status {
PipeStatusStatus::Pipe(pipestatus) => pipestatus PipeStatusStatus::Pipe(pipestatus) => pipestatus
.iter() .iter()
.map( .enumerate()
|ec| match format_exit_code(ec.as_str(), segment_format, None, &config, context) { .filter_map(|(i, ec)| {
Ok(segments) => segments format_exit_code(
.into_iter() ec.as_str(),
.map(|s| s.to_string()) if i == pipestatus.len() - 1 {
.collect::<String>(), segment_format
Err(_) => "".to_string(), } else {
&segment_format_with_separator
}, },
None,
&config,
context,
) )
.collect::<Vec<String>>() .ok()
.join(config.pipestatus_separator), .map(|segments| segments.into_iter().map(|s| s.to_string()))
})
.flatten()
.collect::<String>(),
_ => "".to_string(), _ => "".to_string(),
}; };
@ -225,7 +233,7 @@ fn status_signal_name(signal: SignalNumber) -> Option<&'static str> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use nu_ansi_term::Color; use nu_ansi_term::{Color, Style};
use crate::test::ModuleRenderer; use crate::test::ModuleRenderer;
@ -708,4 +716,37 @@ mod tests {
.collect(); .collect();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test]
fn pipestatus_separator_format() {
let pipe_exit_code = &[0, 1, 2];
let main_exit_code = 2;
let expected_style = Style::new().on(Color::Red).fg(Color::White).bold();
let expected = Some(format!(
"{}{}{}{}{}{}{}",
expected_style.paint("["),
expected_style.paint("0"),
expected_style.paint("|"),
expected_style.paint("1"),
expected_style.paint("|"),
expected_style.paint("2"),
expected_style.paint("] => <2>"),
));
let actual = ModuleRenderer::new("status")
.config(toml::toml! {
[status]
format = "\\($status\\)"
style = "fg:white bg:red bold"
pipestatus = true
pipestatus_separator = "[|]($style)"
pipestatus_format = "[\\[]($style)$pipestatus[\\] => <$status>]($style)"
pipestatus_segment_format = "[$status]($style)"
disabled = false
})
.status(main_exit_code)
.pipestatus(pipe_exit_code)
.collect();
assert_eq!(expected, actual);
}
} }