fix(properties): fix regressions in status, pipestatus and terminal-width handling (#3399)

This commit is contained in:
David Knaack
2022-01-15 10:21:38 +01:00
committed by GitHub
parent 8985499c95
commit 0fd6f05da4
5 changed files with 41 additions and 47 deletions
+32 -38
View File
@@ -43,9 +43,6 @@ pub struct Context<'a> {
/// Properties to provide to modules.
pub properties: Properties,
/// Pipestatus of processes in pipe
pub pipestatus: Option<Vec<String>>,
/// Private field to store Git information for modules who need it
repo: OnceCell<Repo>,
@@ -107,7 +104,7 @@ impl<'a> Context<'a> {
/// Create a new instance of Context for the provided directory
pub fn new_with_shell_and_path(
properties: Properties,
mut properties: Properties,
shell: Shell,
target: Target,
path: PathBuf,
@@ -115,12 +112,24 @@ impl<'a> Context<'a> {
) -> Context<'a> {
let config = StarshipConfig::initialize();
let pipestatus = properties
// If the vector is zero-length, we should pretend that we didn't get a
// pipestatus at all (since this is the input `--pipestatus=""`)
if properties
.pipestatus
.as_deref()
.map(Context::get_and_flatten_pipestatus)
.flatten();
log::trace!("Received completed pipestatus of {:?}", pipestatus);
.map_or(false, |p| p.len() == 1 && p[0].is_empty())
{
properties.pipestatus = None;
}
log::trace!(
"Received completed pipestatus of {:?}",
properties.pipestatus
);
// If status-code is empty, set it to None
if matches!(properties.status_code.as_deref(), Some("")) {
properties.status_code = None;
}
// Canonicalize the current path to resolve symlinks, etc.
// NOTE: On Windows this converts the path to extended-path syntax.
@@ -138,7 +147,6 @@ impl<'a> Context<'a> {
Context {
config,
properties,
pipestatus,
current_dir,
logical_dir,
dir_contents: OnceCell::new(),
@@ -201,27 +209,6 @@ impl<'a> Context<'a> {
dir
}
/// Reads and appropriately flattens multiple args for pipestatus
// TODO: Replace with value_delimiter = ' ' clap option?
pub fn get_and_flatten_pipestatus(args: &[String]) -> Option<Vec<String>> {
// Due to shell differences, we can potentially receive individual or space
// separated inputs, e.g. "0","1","2","0" is the same as "0 1 2 0" and
// "0 1", "2 0". We need to accept all these formats and return a Vec<String>
let parsed_vals = args
.iter()
.map(|x| x.split_ascii_whitespace())
.flatten()
.map(|x| x.to_string())
.collect::<Vec<String>>();
// If the vector is zero-length, we should pretend that we didn't get a
// pipestatus at all (since this is the input `--pipestatus=""`)
if parsed_vals.is_empty() {
None
} else {
Some(parsed_vals)
}
}
/// Create a new module
pub fn new_module(&self, name: &str) -> Module {
let config = self.config.get_module_config(name);
@@ -571,10 +558,6 @@ pub enum Shell {
Unknown,
}
fn default_width() -> usize {
terminal_size().map_or(80, |(w, _)| w.0 as usize)
}
/// Which kind of prompt target to print (main prompt, rprompt, ...)
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Target {
@@ -588,12 +571,12 @@ pub enum Target {
pub struct Properties {
/// The status code of the previously run command
#[clap(short = 's', long = "status")]
pub status_code: Option<i32>,
pub status_code: Option<String>,
/// Bash, Fish and Zsh support returning codes for each process in a pipeline.
#[clap(long)]
pipestatus: Option<Vec<String>>,
#[clap(long, value_delimiter = ' ')]
pub pipestatus: Option<Vec<String>>,
/// The width of the current interactive terminal.
#[clap(short = 'w', long, default_value_t=default_width())]
#[clap(short = 'w', long, default_value_t=default_width(), parse(try_from_str=parse_width))]
terminal_width: usize,
/// The path that the prompt should render for.
#[clap(short, long)]
@@ -632,6 +615,17 @@ fn parse_jobs(jobs: &str) -> Result<i64, ParseIntError> {
jobs.trim().parse::<i64>()
}
fn default_width() -> usize {
terminal_size().map_or(80, |(w, _)| w.0 as usize)
}
fn parse_width(width: &str) -> Result<usize, ParseIntError> {
if width.is_empty() {
return Ok(default_width());
}
width.trim().parse::<usize>()
}
#[cfg(test)]
mod tests {
use super::*;