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>
This commit is contained in:
moko256 2022-02-06 05:13:42 +09:00 committed by GitHub
parent 0b184c3ccb
commit bbdb584f45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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