From cb40787e2a5057bcc3a174545983d7efa011eda4 Mon Sep 17 00:00:00 2001 From: Mika Fischer Date: Tue, 21 Dec 2021 11:05:28 +0100 Subject: [PATCH] feat(status): Add hex_status (#3312) --- docs/config/README.md | 1 + src/modules/status.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index 2bdd0dea..8a211301 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2941,6 +2941,7 @@ This module is not supported on elvish and nu shell. | Variable | Example | Description | | ----------------------- | ------- | ----------------------------------------------------------------------- | | status | `127` | The exit code of the last command | +| hex_status | `0x7F` | The exit code of the last command in hex | | int | `127` | The exit code of the last command | | common_meaning | `ERROR` | Meaning of the code if not a signal | | signal_number | `9` | Signal number corresponding to the exit code, only if signalled | diff --git a/src/modules/status.rs b/src/modules/status.rs index 26c1c8ef..b811453d 100644 --- a/src/modules/status.rs +++ b/src/modules/status.rs @@ -106,6 +106,11 @@ fn format_exit_code<'a>( } }; + let hex_status = exit_code + .parse::() + .ok() + .map(|code| format!("0x{:X}", code)); + let common_meaning = status_common_meaning(exit_code_int); let raw_signal_number = match config.recognize_signal_code { @@ -148,6 +153,7 @@ fn format_exit_code<'a>( }) .map(|variable| match variable { "status" => Some(Ok(exit_code)), + "hex_status" => Ok(hex_status.as_deref().or(Some(exit_code))).transpose(), "int" => Some(Ok(exit_code)), "maybe_int" => Ok(maybe_exit_code_number).transpose(), "common_meaning" => Ok(common_meaning).transpose(), @@ -279,6 +285,29 @@ mod tests { } } + #[test] + fn failure_hex_status() { + let exit_values = [1, 2, 130, -2147467260]; + let string_values = ["0x1", "0x2", "0x82", "0x80004004"]; + + for (exit_value, string_value) in exit_values.iter().zip(string_values) { + let expected = Some(format!( + "{} ", + Color::Red.bold().paint(format!("✖{}", string_value)) + )); + let actual = ModuleRenderer::new("status") + .config(toml::toml! { + [status] + symbol = "✖" + disabled = false + format = "[${symbol}${hex_status}]($style) " + }) + .status(*exit_value) + .collect(); + assert_eq!(expected, actual); + } + } + #[test] fn signal_name() { let exit_values = [1, 2, 126, 127, 130, 101];