feat(status): Add pipestatus display in status module (#2481)

* feat: Add pipestatus display in status module

This MR is based on this one https://github.com/starship/starship/pull/370

* Documentation

* Add a test with map_symbol false

* Handle bash preexec pipestatus

* Add zsh support

* Add fish support

Thanks kidonng for the diff patch

* Rename sucess_symbol to success_symbol
This commit is contained in:
Hugues Morisset 2021-07-28 18:26:00 +02:00 committed by GitHub
parent 2fa7f56cec
commit 40cb667b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 360 additions and 86 deletions

View File

@ -2703,6 +2703,7 @@ This module is not supported on elvish and nu shell.
| ------------------------- | ----------------------------- | ------------------------------------------------------ |
| `format` | `"[$symbol$status]($style) "` | The format of the module |
| `symbol` | `"✖"` | The symbol displayed on program error |
| `success_symbol` | `"✔️"` | The symbol displayed on program success |
| `not_executable_symbol` | `"🚫"` | The symbol displayed when file isn't executable |
| `not_found_symbol` | `"🔍"` | The symbol displayed when the command can't be found |
| `sigint_symbol` | `"🧱"` | The symbol displayed on SIGINT (Ctrl + c) |
@ -2710,6 +2711,9 @@ This module is not supported on elvish and nu shell.
| `style` | `"bold red"` | The style for the module. |
| `recognize_signal_code` | `true` | Enable signal mapping from exit code |
| `map_symbol` | `false` | Enable symbols mapping from exit code |
| `pipestatus` | `false` | Enable pipestatus reporting |
| `pipestatus_separator` | `|` | The symbol that separate in pipe program exit codes |
| `pipestatus_format` | `\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)` | The format of the module when the command is a pipeline |
| `disabled` | `true` | Disables the `status` module. |
### Variables
@ -2722,6 +2726,7 @@ This module is not supported on elvish and nu shell.
| signal_number | `9` | Signal number corresponding to the exit code, only if signalled |
| signal_name | `KILL` | Name of the signal corresponding to the exit code, only if signalled |
| maybe_int | `7` | Contains the exit code number when no meaning has been found |
| pipestatus | | Rendering of in pipeline programs's exit codes, this is only available in pipestatus_format |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

View File

@ -7,6 +7,7 @@ use starship_module_config_derive::ModuleConfig;
pub struct StatusConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub success_symbol: &'a str,
pub not_executable_symbol: &'a str,
pub not_found_symbol: &'a str,
pub sigint_symbol: &'a str,
@ -14,6 +15,9 @@ pub struct StatusConfig<'a> {
pub style: &'a str,
pub map_symbol: bool,
pub recognize_signal_code: bool,
pub pipestatus: bool,
pub pipestatus_separator: &'a str,
pub pipestatus_format: &'a str,
pub disabled: bool,
}
@ -22,6 +26,7 @@ impl<'a> Default for StatusConfig<'a> {
StatusConfig {
format: "[$symbol$status]($style) ",
symbol: "",
success_symbol: "✔️",
not_executable_symbol: "🚫",
not_found_symbol: "🔍",
sigint_symbol: "🧱",
@ -29,6 +34,10 @@ impl<'a> Default for StatusConfig<'a> {
style: "bold red",
map_symbol: false,
recognize_signal_code: true,
pipestatus: false,
pipestatus_separator: "|",
pipestatus_format:
"\\[$pipestatus\\] => [$symbol$common_meaning$signal_name$maybe_int]($style)",
disabled: true,
}
}

View File

@ -36,6 +36,9 @@ pub struct Context<'a> {
/// Properties to provide to modules.
pub properties: HashMap<&'a str, String>,
/// Pipestatus of processes in pipe
pub pipestatus: Option<Vec<String>>,
/// Private field to store Git information for modules who need it
repo: OnceCell<Repo>,
@ -74,7 +77,7 @@ impl<'a> Context<'a> {
.or_else(|| arguments.value_of("logical_path").map(PathBuf::from))
.unwrap_or_default();
// Retrive the "logical directory".
// Retrieve the "logical directory".
// If the path argument is not set fall back to the PWD env variable set by many shells
// or to the other path.
let logical_path = arguments
@ -96,8 +99,6 @@ impl<'a> Context<'a> {
let config = StarshipConfig::initialize();
// Unwrap the clap arguments into a simple hashtable
// we only care about single arguments at this point, there isn't a
// use-case for a list of arguments yet.
let properties: HashMap<&str, std::string::String> = arguments
.args
.iter()
@ -105,6 +106,11 @@ impl<'a> Context<'a> {
.map(|(a, b)| (*a, b.vals.first().cloned().unwrap().into_string().unwrap()))
.collect();
// Pipestatus is an arguments list
let pipestatus = arguments
.values_of("pipestatus")
.map(|args| args.into_iter().map(String::from).collect());
// Canonicalize the current path to resolve symlinks, etc.
// NOTE: On Windows this converts the path to extended-path syntax.
let current_dir = Context::expand_tilde(path);
@ -116,6 +122,7 @@ impl<'a> Context<'a> {
Context {
config,
properties,
pipestatus,
current_dir,
logical_dir,
dir_contents: OnceCell::new(),

View File

@ -29,7 +29,10 @@ starship_preexec() {
# Will be run before the prompt is drawn
starship_precmd() {
# Save the status, because commands in this pipeline will change $?
STARSHIP_CMD_STATUS=$?
STARSHIP_CMD_STATUS=$? STARSHIP_PIPE_STATUS=(${PIPESTATUS[@]})
if [[ "${#BP_PIPESTATUS[@]}" -gt "${#STARSHIP_PIPE_STATUS[@]}" ]]; then
STARSHIP_PIPE_STATUS=(${BP_PIPESTATUS[@]})
fi
local NUM_JOBS=0
# Evaluate the number of jobs before running the preseved prompt command, so that tools
@ -46,10 +49,10 @@ starship_precmd() {
if [[ $STARSHIP_START_TIME ]]; then
STARSHIP_END_TIME=$(::STARSHIP:: time)
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
PS1="$(::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --jobs="$NUM_JOBS" --cmd-duration=$STARSHIP_DURATION)"
PS1="$(::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --pipestatus ${STARSHIP_PIPE_STATUS[@]} --jobs="$NUM_JOBS" --cmd-duration=$STARSHIP_DURATION)"
unset STARSHIP_START_TIME
else
PS1="$(::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --jobs="$NUM_JOBS")"
PS1="$(::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --pipestatus ${STARSHIP_PIPE_STATUS[@]} --jobs="$NUM_JOBS")"
fi
STARSHIP_PREEXEC_READY=true # Signal that we can safely restart the timer
}

View File

@ -8,7 +8,7 @@ function fish_prompt
set STARSHIP_CMD_STATUS $status
# Account for changes in variable name between v2.7 and v3.0
set STARSHIP_DURATION "$CMD_DURATION$cmd_duration"
::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --keymap=$STARSHIP_KEYMAP --cmd-duration=$STARSHIP_DURATION --jobs=(count (jobs -p))
::STARSHIP:: prompt --status=$STARSHIP_CMD_STATUS --pipestatus=$pipestatus --keymap=$STARSHIP_KEYMAP --cmd-duration=$STARSHIP_DURATION --jobs=(count (jobs -p))
end
# Disable virtualenv prompt, it breaks starship

View File

@ -26,7 +26,7 @@ fi
# Will be run before every prompt draw
starship_precmd() {
# Save the status, because commands in this pipeline will change $?
STARSHIP_CMD_STATUS=$?
STARSHIP_CMD_STATUS=$? STARSHIP_PIPE_STATUS=(${pipestatus[@]})
# Compute cmd_duration, if we have a time to consume, otherwise clear the
# previous duration
@ -91,4 +91,4 @@ export STARSHIP_SESSION_KEY=${STARSHIP_SESSION_KEY:0:16}; # Trim to 16-digits if
VIRTUAL_ENV_DISABLE_PROMPT=1
setopt promptsubst
PROMPT='$(::STARSHIP:: prompt --keymap="$KEYMAP" --status="$STARSHIP_CMD_STATUS" --cmd-duration="$STARSHIP_DURATION" --jobs="$STARSHIP_JOBS_COUNT")'
PROMPT='$(::STARSHIP:: prompt --keymap="$KEYMAP" --status="$STARSHIP_CMD_STATUS" --pipestatus ${STARSHIP_PIPE_STATUS[@]} --cmd-duration="$STARSHIP_DURATION" --jobs="$STARSHIP_JOBS_COUNT")'

View File

@ -23,6 +23,13 @@ fn main() {
.help("The status code of the previously run command")
.takes_value(true);
let pipestatus_arg = Arg::with_name("pipestatus")
.long("pipestatus")
.value_name("PIPESTATUS")
.help("Status codes from a command pipeline")
.long_help("Bash and Zsh supports returning codes for each process in a pipeline.")
.multiple(true);
let path_arg = Arg::with_name("path")
.short("p")
.long("path")
@ -93,6 +100,7 @@ fn main() {
SubCommand::with_name("prompt")
.about("Prints the full starship prompt")
.arg(&status_code_arg)
.arg(&pipestatus_arg)
.arg(&path_arg)
.arg(&logical_path_arg)
.arg(&cmd_duration_arg)
@ -115,6 +123,7 @@ fn main() {
.help("List out all supported modules"),
)
.arg(&status_code_arg)
.arg(&pipestatus_arg)
.arg(&path_arg)
.arg(&logical_path_arg)
.arg(&cmd_duration_arg)

View File

@ -1,10 +1,19 @@
use std::string::ToString;
use super::{Context, Module, RootModuleConfig};
use crate::configs::status::StatusConfig;
use crate::formatter::StringFormatter;
use crate::formatter::{string_formatter::StringFormatterError, StringFormatter};
use crate::segment::Segment;
type ExitCode = i64;
type SignalNumber = u32;
#[derive(PartialEq)]
enum PipeStatusStatus<'a> {
Disabled,
NoPipe,
Pipe(&'a Vec<std::string::String>),
}
/// Creates a module with the status of the last command
///
@ -15,83 +24,142 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.get("status_code")
.map_or("0", String::as_str);
if exit_code == "0" {
None
} else {
let mut module = context.new_module("status");
let config = StatusConfig::try_load(module.config);
// As we default to disabled=true, we have to check here after loading our config module,
// before it was only checking against whatever is in the config starship.toml
if config.disabled {
return None;
};
let exit_code_int: ExitCode = match exit_code.parse() {
Ok(i) => i,
Err(_) => return None,
};
let common_meaning = status_common_meaning(exit_code_int);
let raw_signal_number = match config.recognize_signal_code {
true => status_to_signal(exit_code_int),
false => None,
};
let signal_number = raw_signal_number.map(|sn| sn.to_string());
let signal_name = raw_signal_number
.and_then(|sn| status_signal_name(sn).or_else(|| signal_number.as_deref()));
// If not a signal and not a common meaning, it should at least print the raw exit code number
let maybe_exit_code_number = match common_meaning.is_none() && signal_name.is_none() {
true => Some(exit_code),
false => None,
};
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => match exit_code_int {
126 if config.map_symbol => Some(config.not_executable_symbol),
127 if config.map_symbol => Some(config.not_found_symbol),
130 if config.recognize_signal_code && config.map_symbol => {
Some(config.sigint_symbol)
}
x if (129..256).contains(&x)
&& config.recognize_signal_code
&& config.map_symbol =>
{
Some(config.signal_symbol)
}
_ => Some(config.symbol),
},
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"status" => Some(Ok(exit_code)),
"int" => Some(Ok(exit_code)),
"maybe_int" => Ok(maybe_exit_code_number.as_deref()).transpose(),
"common_meaning" => Ok(common_meaning.as_deref()).transpose(),
"signal_number" => Ok(signal_number.as_deref()).transpose(),
"signal_name" => Ok(signal_name.as_deref()).transpose(),
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(_error) => {
log::warn!("Error parsing format string in `status.format`");
return None;
}
});
Some(module)
let pipestatus_status = match &context.pipestatus {
None => PipeStatusStatus::Disabled,
Some(ps) => match ps.len() > 1 {
true => PipeStatusStatus::Pipe(&ps),
false => PipeStatusStatus::NoPipe,
},
};
if exit_code == "0"
&& (pipestatus_status == PipeStatusStatus::Disabled
|| pipestatus_status == PipeStatusStatus::NoPipe)
{
return None;
}
let mut module = context.new_module("status");
let config = StatusConfig::try_load(module.config);
// As we default to disabled=true, we have to check here after loading our config module,
// before it was only checking against whatever is in the config starship.toml
if config.disabled {
return None;
};
let pipestatus_status = match config.pipestatus {
true => pipestatus_status,
false => PipeStatusStatus::Disabled,
};
// Create pipestatus string
let pipestatus = match pipestatus_status {
PipeStatusStatus::Pipe(pipestatus) => pipestatus
.iter()
.map(
|ec| match format_exit_code(ec.as_str(), config.format, None, &config) {
Ok(segments) => segments
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join(""),
Err(_) => "".to_string(),
},
)
.collect::<Vec<String>>()
.join(config.pipestatus_separator),
_ => "".to_string(),
};
let main_format = match pipestatus_status {
PipeStatusStatus::Pipe(_) => config.pipestatus_format,
_ => config.format,
};
let parsed = format_exit_code(exit_code, main_format, Some(&pipestatus), &config);
module.set_segments(match parsed {
Ok(segments) => segments,
Err(_error) => {
log::warn!("Error parsing format string in `status.format`");
return None;
}
});
Some(module)
}
fn format_exit_code<'a>(
exit_code: &'a str,
format: &'a str,
pipestatus: Option<&str>,
config: &'a StatusConfig,
) -> Result<Vec<Segment>, StringFormatterError> {
let exit_code_int: ExitCode = match exit_code.parse() {
Ok(i) => i,
Err(_) => {
log::warn!("Error parsing exit_code string to int");
return Ok(Vec::new());
}
};
let common_meaning = status_common_meaning(exit_code_int);
let raw_signal_number = match config.recognize_signal_code {
true => status_to_signal(exit_code_int),
false => None,
};
let signal_number = raw_signal_number.map(|sn| sn.to_string());
let signal_name = raw_signal_number
.and_then(|sn| status_signal_name(sn).or_else(|| signal_number.as_deref()));
// If not a signal and not a common meaning, it should at least print the raw exit code number
let maybe_exit_code_number = match common_meaning.is_none() && signal_name.is_none() {
true => Some(exit_code),
false => None,
};
StringFormatter::new(format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => match exit_code_int {
0 => Some(config.success_symbol),
126 if config.map_symbol => Some(config.not_executable_symbol),
127 if config.map_symbol => Some(config.not_found_symbol),
130 if config.recognize_signal_code && config.map_symbol => {
Some(config.sigint_symbol)
}
x if (129..256).contains(&x)
&& config.recognize_signal_code
&& config.map_symbol =>
{
Some(config.signal_symbol)
}
_ => Some(config.symbol),
},
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"status" => Some(Ok(exit_code)),
"int" => Some(Ok(exit_code)),
"maybe_int" => Ok(maybe_exit_code_number.as_deref()).transpose(),
"common_meaning" => Ok(common_meaning.as_deref()).transpose(),
"signal_number" => Ok(signal_number.as_deref()).transpose(),
"signal_name" => Ok(signal_name.as_deref()).transpose(),
"pipestatus" => {
let pipestatus = pipestatus.unwrap_or_else(|| {
// We might enter this case if pipestatus hasn't
// been processed yet, which means that it has been
// set in format
log::warn!("pipestatus variable is only available in pipestatus_format");
""
});
Some(Ok(pipestatus))
}
_ => None,
})
.parse(None)
})
}
fn status_common_meaning(ex: ExitCode) -> Option<&'static str> {
@ -100,6 +168,7 @@ fn status_common_meaning(ex: ExitCode) -> Option<&'static str> {
return None;
}
match ex {
0 => Some(""),
1 => Some("ERROR"),
2 => Some("USAGE"),
126 => Some("NOPERM"),
@ -336,4 +405,171 @@ mod tests {
assert_eq!(expected, actual);
}
}
#[test]
fn pipeline_uses_pipestatus_format() {
let exit_values = [
[0, 0, 0, 0],
[0, 1, 2, 3],
[130, 126, 131, 127],
[1, 1, 1, 1],
];
let exit_values_rendered = [
"PSF 🟢=🟢 🟢 🟢",
"PSF 🟢=🔴 🔴 🔴",
"PSF 🧱=🚫 ⚡ 🔍",
"PSF 🔴=🔴 🔴 🔴",
];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) {
let main_exit_code = status[0];
let pipe_exit_code = &status[1..];
let expected = Some(rendered.to_string());
let actual = ModuleRenderer::new("status")
.config(toml::toml! {
[status]
format = "$symbol"
symbol = "🔴"
success_symbol = "🟢"
not_executable_symbol = "🚫"
not_found_symbol = "🔍"
sigint_symbol = "🧱"
signal_symbol = ""
recognize_signal_code = true
map_symbol = true
pipestatus = true
pipestatus_separator = " "
pipestatus_format = "PSF $symbol=$pipestatus"
disabled = false
})
.status(main_exit_code)
.pipestatus(pipe_exit_code)
.collect();
assert_eq!(expected, actual);
}
}
#[test]
fn pipeline_no_map_symbols() {
let exit_values = [
[0, 0, 0, 0],
[0, 1, 2, 3],
[130, 126, 131, 127],
[1, 1, 1, 1],
];
let exit_values_rendered = [
"PSF 🟢=🟢0 🟢0 🟢0",
"PSF 🟢=🔴1 🔴2 🔴3",
"PSF INT🔴=🔴126 🔴1313 🔴127",
"PSF 🔴=🔴1 🔴1 🔴1",
];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) {
let main_exit_code = status[0];
let pipe_exit_code = &status[1..];
let expected = Some(rendered.to_string());
let actual = ModuleRenderer::new("status")
.config(toml::toml! {
[status]
format = "$symbol$int$signal_number"
symbol = "🔴"
success_symbol = "🟢"
not_executable_symbol = "🚫"
not_found_symbol = "🔍"
sigint_symbol = "🧱"
signal_symbol = ""
recognize_signal_code = true
map_symbol = false
pipestatus = true
pipestatus_separator = " "
pipestatus_format = "PSF $signal_name$symbol=$pipestatus"
disabled = false
})
.status(main_exit_code)
.pipestatus(pipe_exit_code)
.collect();
assert_eq!(expected, actual);
}
}
#[test]
fn pipeline_disabled() {
let exit_values = [
[0, 0, 0, 0],
[0, 1, 2, 3],
[130, 126, 131, 127],
[1, 1, 1, 1],
];
let exit_values_rendered = ["F 🟢", "F 🟢", "F 🧱", "F 🔴"];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) {
let main_exit_code = status[0];
let pipe_exit_code = &status[1..];
let expected = Some(rendered.to_string());
let actual = ModuleRenderer::new("status")
.config(toml::toml! {
[status]
format = "F $symbol"
symbol = "🔴"
success_symbol = "🟢"
not_executable_symbol = "🚫"
not_found_symbol = "🔍"
sigint_symbol = "🧱"
signal_symbol = ""
recognize_signal_code = true
map_symbol = true
pipestatus = false
pipestatus_separator = " "
pipestatus_format = "PSF $symbol=$pipestatus"
disabled = false
})
.status(main_exit_code)
.pipestatus(pipe_exit_code)
.collect();
assert_eq!(expected, actual);
}
}
#[test]
fn pipeline_long() {
let exit_values = [
[130, 0, 0, 0, 30, 1, 2, 3, 142, 0, 0, 0, 130],
[1, 0, 0, 0, 30, 127, 126, 3, 142, 0, 230, 0, 2],
];
let exit_values_rendered = [
"PSF 130INT=🟢|🟢|🟢|🔴30|🔴|🔴|🔴3|⚡|🟢|🟢|🟢|🧱",
"PSF 1ERROR=🟢|🟢|🟢|🔴30|🔍|🚫|🔴3|⚡|🟢|⚡|🟢|🔴",
];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) {
let main_exit_code = status[0];
let pipe_exit_code = &status[1..];
let expected = Some(rendered.to_string());
let actual = ModuleRenderer::new("status")
.config(toml::toml! {
[status]
format = "$symbol$maybe_int"
symbol = "🔴"
success_symbol = "🟢"
not_executable_symbol = "🚫"
not_found_symbol = "🔍"
sigint_symbol = "🧱"
signal_symbol = ""
recognize_signal_code = true
map_symbol = true
pipestatus = true
pipestatus_separator = "|"
pipestatus_format = "PSF $int$common_meaning$signal_name=$pipestatus"
disabled = false
})
.status(main_exit_code)
.pipestatus(pipe_exit_code)
.collect();
assert_eq!(expected, actual);
}
}
}

View File

@ -132,6 +132,11 @@ impl<'a> ModuleRenderer<'a> {
self
}
pub fn pipestatus(mut self, status: &[i32]) -> Self {
self.context.pipestatus = Some(status.iter().map(|i| i.to_string()).collect());
self
}
/// Renders the module returning its output
pub fn collect(self) -> Option<String> {
let ret = crate::print::get_module(self.name, self.context);