Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add module for showing exit codes of shell pipeline #370

Closed
wants to merge 25 commits into from

Conversation

nrabulinski
Copy link
Contributor

@nrabulinski nrabulinski commented Sep 14, 2019

Description

I implemented status module which can show the exit code of the last command or all exit codes of the last pipeline

Motivation and Context

Closes #193
Closes #882

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Screenshots (if appropriate):

Default configuration:
image
Symbols for exit codes:
image
No pipeline:
image

How Has This Been Tested?

  • I have tested using MacOS
  • I have tested using Linux
  • I have tested using Windows

Checklist:

  • I have updated the documentation accordingly.
  • I have updated the tests accordingly.

@nrabulinski nrabulinski marked this pull request as ready for review September 14, 2019 14:16
@nrabulinski
Copy link
Contributor Author

Oh no, I forgot about the quirk @lilyball talked about - when using ! true the exit code should be 1 even though pipestatus returns 0.
Will fix in the next commit.

@nickwb
Copy link
Contributor

nickwb commented Sep 15, 2019

I must admit, I'm a little confused by all of the configuration options - hopefully some new documentation could help with that? 🙂

This is just a thought - do you think there's value in modelling the display mode in an enum?

For example:

enum DisplayMode {
    ShowPipeline,              // Always show the pipeline
    ShowPipelineOnError,       // Show the pipeline on error, otherwise nothing
    ShowLastOrPipelineOnError, // Show the pipeline on error, otherwise the last status
    ShowLast,                  // Always show the last status
    ShowLastOnError            // Show the last status on error, otherwise nothing
}

Copy link
Member

@jletey jletey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to work on this @nrabulinski 🎉!

Everything looks great!

I just have one request: Please take a look at #367, and do the same here. It would be nice to be able to change the delimiters surrounding the exit codes.

@jletey jletey changed the title feat: module for showing exit codes from previous pipeline feat: Add module for showing exit codes of shell pipeline Sep 15, 2019
@nrabulinski
Copy link
Contributor Author

I must admit, I'm a little confused by all of the configuration options - hopefully some new documentation could help with that? slightly_smiling_face

This is just a thought - do you think there's value in modelling the display mode in an enum?

For example:

enum DisplayMode {
    ShowPipeline,              // Always show the pipeline
    ShowPipelineOnError,       // Show the pipeline on error, otherwise nothing
    ShowLastOrPipelineOnError, // Show the pipeline on error, otherwise the last status
    ShowLast,                  // Always show the last status
    ShowLastOnError            // Show the last status on error, otherwise nothing
}

I've already talked about it in the Discord server, but I don't personally think there's sense to add ShowLastOrPipelineOnError. Other than that I think it was a great idea and I knew I had to change all those bools anyway. I don't know however if the way I solved it is the best/most intuitive one out there, so if you have any other suggestions - I'm all ears!

and a few smaller fixes
one being changing delimeter_... to prefix/suffix as in starship#367
@lilyball
Copy link

Judging from the name, ShowLastOrPipelineOnError matches the behavior I use personally.

@nrabulinski
Copy link
Contributor Author

@lilyball I just don’t see how it’d be different from my current OnError other than it’d show 0 each time there’s no error which isn’t very helpful. Although if it is something that people want, I can implement it.

@lilyball
Copy link

Skimming the code, it looks like what you've actually implemented doesn't do what I thought it did. In fact, it doesn't even implement the behavior of showing an error on ! false, because that has a pipestatus length of 1 and errored is false.

@nrabulinski
Copy link
Contributor Author

@lilyball
And it’s only natural that a negated error is considered a success, isn’t it?
As I mentioned earlier - I don’t see the point of adding the mode you’re talking about since the only way it differs from Always is that it’d show 0 when there was no error instead of potentially whole pipeline.

It’d probably be much more viable to iterate through all the status codes in pipeline and show them when at least one of them errored which I might add with the next commit but as it is and considering what (at least I think) you’re talking about - it doesn’t make sense to add.

@nkoehring
Copy link

I'm new to the code of this project so I don't really know how other modules are doing it, but for the sake of readability I would suggest to move the default values (for example in line 57, line 59, line 60 and others) into constants in (or close to) the beginning of status.rs.

@lilyball
Copy link

@nrabulinski The whole point of my bash prompt status code setup is to convey the results of the previous job without extraneous info. The results include answering the question "is the status different from the pipestatus?"

So basically, my prompt shows pipestatus, unless it's all zeroes. If pipestatus disagrees with status, it shows that disagreement parenthesized. And it colors the output accordingly, so ! false shows an output in grey of [1 (0)]; the 1 because it's nonzero, and the (0) because that's the status diasgreeing with pipestatus. But ! true shows output in red of [(1)], pipestatus is omitted since it's all zeroes, but status disagrees so it's shown. This same output works for ! true | true as well, since again pipestatus is all zeroes. But e.g. ! false | false is (in grey) [1 1 (0)]. And ! false | true is (in red) [1 0 (1)]; the pipestatus is shown here because it has a nonzero component.

Ultimately, I'm left with the shortest prompt that doesn't discard any status info. I can tell at a glance what both $? and $PIPESTATUS are. And uninteresting output is omitted so I don't have to parse it just to find out that it's boring.

@chipbuster
Copy link
Contributor

@nrabulinski Do you plan to make more changes to this PR or are we good to review as-is?

@nrabulinski
Copy link
Contributor Author

@chipbuster I'll commit the changes @lilyball suggested in a day or two and then it should be ready to review

Copy link
Member

@matchai matchai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 👍
Just seems you need to rerun the formatter.
If you can change the module to be disabled by default, we can go ahead and merge this.

Requesting review from another astronaut since this is a pretty big PR.

| `success_symbol` | "✔" | The symbol used if the exit code is equal to 0. |
| `error_symbol` | "✖" | The symbol used if the exit code is non-zero. |
| `simple_pipeline` | true | Show only one exit code if all values in the pipeline are equal. |
| `disabled` | false | Disables the `status` module. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking it over, I think we should keep this module disabled by default.
Could you please the configuration to reflect that?

None => vec![&exit_code],
};

// kind of a hack to not show `<previous status code>` when a user sends ^C to clear the line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let pipeline_error = pipestatus.iter().any(|&code| code != "0");
let mismatch = exit_code != *pipestatus.last()?;

let display_mode = status_config.display_mode; //get_display_mode(&module);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment meant to be in the PR?

@@ -0,0 +1,209 @@
use ansi_term::Color;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests look great! 👍

@matchai matchai requested a review from a team December 24, 2019 04:02
@chipbuster
Copy link
Contributor

Looks like rustfmt failed on....whitespace? Not entirely sure what happened there but hopefully it's easy to fix.

@lilyball
Copy link

It looks like the empty line has 2 stray spaces in it, which rustfmt wants to delete.

@jletey
Copy link
Member

jletey commented Jan 12, 2020

Looks like rustfmt failed on....whitespace

I am still utterly confused by the fix 😅, but all tests are passing now!

@matchai
Copy link
Member

matchai commented Jan 12, 2020

@nrabulinski A friendly reminder about those review comments. ☝️

"always" => DisplayMode::Always,
"error" => DisplayMode::OnExitError,
"any error" => DisplayMode::OnError,
/*"mismatch" |*/ _ => DisplayMode::OnErrorOrMismatch,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend explicitly matching "mismatch" here, and panicking if it doesn't match one of those strings. Worst case, the panic is unreachable otherwise.

let pipestatus_arg = Arg::with_name("pipestatus")
.long("pipestatus")
.value_name("PIPESTATUS")
.help("Status codes from the previous pipeline")
Copy link
Contributor

@Lucretiel Lucretiel Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend adding a long_help here that documents the specific argument we're looking for. Is it a single, space-separated argument? Several individual arguments? Something else? If possible, I'd consider using Arg::multiple, which means that the expected format would be: --pipestatus 0 1 0 2 (as opposed to --pipestatus "0 1 0 2").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that’s cool, I’m not really familiar with clap so I wasn’t aware this was a thing, thank you very much!

@@ -24,7 +24,7 @@ starship_preexec() {
# Will be run before the prompt is drawn
starship_precmd() {
# Save the status, because commands in this pipeline will change $?
STATUS=$?
local STATUS=$? PIPE="${PIPESTATUS[@]}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks when using bash-preexec this will have to be BP_PIPESTATUS: https://github.com/rcaloras/bash-preexec/blob/master/bash-preexec.sh#L41-L45

@matchai matchai mentioned this pull request May 7, 2020
8 tasks
@tiffafoo
Copy link
Member

Closing this in favor of a newer one: #1165

@tiffafoo tiffafoo closed this May 19, 2020
izissise added a commit to izissise/starship that referenced this pull request Mar 20, 2021
izissise added a commit to izissise/starship that referenced this pull request Mar 22, 2021
izissise added a commit to izissise/starship that referenced this pull request Mar 26, 2021
izissise added a commit to izissise/starship that referenced this pull request Mar 27, 2021
izissise added a commit to izissise/starship that referenced this pull request Apr 7, 2021
izissise added a commit to izissise/starship that referenced this pull request Apr 17, 2021
izissise added a commit to izissise/starship that referenced this pull request May 24, 2021
izissise added a commit to izissise/starship that referenced this pull request Jul 1, 2021
matchai pushed a commit that referenced this pull request Jul 28, 2021
* feat: Add pipestatus display in status module

This MR is based on this one #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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for status codes Option to show job exit status in prompt
10 participants