feat(status): Add hex_status (#3312)

This commit is contained in:
Mika Fischer 2021-12-21 11:05:28 +01:00 committed by GitHub
parent 16af0ffcd6
commit cb40787e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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 |

View File

@ -106,6 +106,11 @@ fn format_exit_code<'a>(
}
};
let hex_status = exit_code
.parse::<i32>()
.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];