diff --git a/docs/config/README.md b/docs/config/README.md index 3cb44634..bfba86a0 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 24a10dbb..80abe108 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, /// 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 e0b0f1ff..d7299c0d 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, 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::() { + 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::() - .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 1cada5f4..af8775e6 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()