diff options
author | moko256 <koutaro.mo@gmail.com> | 2022-02-06 05:13:42 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-05 21:13:42 +0100 |
commit | bbdb584f45ddfe20a7f9b3aef665ce322f7db056 (patch) | |
tree | 3f55660122f70ca249fb570e16e9d399a992fca9 | |
parent | 0b184c3ccbb9f97029642e139c604615eeb4ac95 (diff) |
fix(status): Enable to convert from i64 to hex_status by casting instead of parsing status. (#3462)
* fix(status): Enable to convert from i64 to hex_status by casting instead of parsing status.
* Apply comment to src/context.rs
Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
* Update README.md in configuration
Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
-rw-r--r-- | docs/config/README.md | 1 | ||||
-rw-r--r-- | src/context.rs | 2 | ||||
-rw-r--r-- | src/modules/status.rs | 18 | ||||
-rw-r--r-- | src/test/mod.rs | 4 |
4 files changed, 12 insertions, 13 deletions
diff --git a/docs/config/README.md b/docs/config/README.md index 3cb44634e..bfba86a07 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2984,6 +2984,7 @@ format = '[📦 \[$env\]]($style) ' The `status` module displays the exit code of the previous command. The module will be shown only if the exit code is not `0`. +The status code will cast to a signed 32-bit integer. ::: tip diff --git a/src/context.rs b/src/context.rs index 24a10dbb6..80abe1088 100644 --- a/src/context.rs +++ b/src/context.rs @@ -575,7 +575,7 @@ pub enum Target { /// Properties as passed on from the shell as arguments #[derive(Parser, Debug)] pub struct Properties { - /// The status code of the previously run command + /// The status code of the previously run command as an unsigned or signed 32bit integer #[clap(short = 's', long = "status")] pub status_code: Option<String>, /// Bash, Fish and Zsh support returning codes for each process in a pipeline. diff --git a/src/modules/status.rs b/src/modules/status.rs index e0b0f1ff5..d7299c0dd 100644 --- a/src/modules/status.rs +++ b/src/modules/status.rs @@ -6,7 +6,7 @@ use crate::configs::status::StatusConfig; use crate::formatter::{string_formatter::StringFormatterError, StringFormatter}; use crate::segment::Segment; -type ExitCode = i64; +type ExitCode = i32; type SignalNumber = u32; #[derive(PartialEq)] enum PipeStatusStatus<'a> { @@ -101,18 +101,16 @@ fn format_exit_code<'a>( config: &'a StatusConfig, context: &'a Context, ) -> Result<Vec<Segment>, StringFormatterError> { - let exit_code_int: ExitCode = match exit_code.parse() { - Ok(i) => i, + // First, parse as i64 to accept both i32 or u32, then normalize to i32. + let exit_code_int: ExitCode = match exit_code.parse::<i64>() { + Ok(i) => i as ExitCode, Err(_) => { log::warn!("Error parsing exit_code string to int"); return Ok(Vec::new()); } }; - let hex_status = exit_code - .parse::<i32>() - .ok() - .map(|code| format!("0x{:X}", code)); + let hex_status = format!("0x{:X}", exit_code_int); let common_meaning = status_common_meaning(exit_code_int); @@ -156,7 +154,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(), + "hex_status" => Some(Ok(hex_status.as_ref())), "int" => Some(Ok(exit_code)), "maybe_int" => Ok(maybe_exit_code_number).transpose(), "common_meaning" => Ok(common_meaning).transpose(), @@ -290,8 +288,8 @@ mod tests { #[test] fn failure_hex_status() { - let exit_values = [1, 2, 130, -2147467260]; - let string_values = ["0x1", "0x2", "0x82", "0x80004004"]; + let exit_values = [1, 2, 130, -2147467260, 2147500036]; + let string_values = ["0x1", "0x2", "0x82", "0x80004004", "0x80004004"]; for (exit_value, string_value) in exit_values.iter().zip(string_values) { let expected = Some(format!( diff --git a/src/test/mod.rs b/src/test/mod.rs index 1cada5f45..af8775e61 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -126,7 +126,7 @@ impl<'a> ModuleRenderer<'a> { self } - pub fn status(mut self, status: i32) -> Self { + pub fn status(mut self, status: i64) -> Self { self.context.properties.status_code = Some(status.to_string()); self } @@ -140,7 +140,7 @@ impl<'a> ModuleRenderer<'a> { self } - pub fn pipestatus(mut self, status: &[i32]) -> Self { + pub fn pipestatus(mut self, status: &[i64]) -> Self { self.context.properties.pipestatus = Some( status .iter() |