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: implement timer module #118

Merged
merged 21 commits into from
Aug 8, 2019
Merged

feat: implement timer module #118

merged 21 commits into from
Aug 8, 2019

Conversation

chipbuster
Copy link
Contributor

This PR adds a module in starship that adds a job timer.

Description

Implement a timer module that takes a commandline argument, the number of seconds the last job took to complete, and displays it if appropriate.

Alters shell initialization files to compute this number using date +%s where needed.

Adds a config section to configure minimum amount of time before timer is shown (default is 2s)

Motivation and Context

Closes #104

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):

Top-to-bottom: bash, zsh, fish (zsh is slightly doctored because my old shell config kept throwing some lines in there)

image

How Has This Been Tested?

Tested by using in all three environments. Bash and Zsh proved surprisingly tricky to do correctly, so their shell configs are now fairly large.

Behavior is not 100% consistent between shells: when spamming empty commands, fish will continue to display the last command time, while bash and zsh will not.

Passes all integration tests.

  • 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.
    (But only in-module tests--don't know if there are tests elsewhere)

@chipbuster
Copy link
Contributor Author

A couple of notes about this: first, I've utterly goobered the git stuff--it's showing 25 commits to merge in, some of which I'm pretty sure were already merged. If anyone knows how to fix this, please let me know.

Second, I'm not sure if the design of the rendering function is sane--I think it's nice, but it also has a (&u64, &&str) as the argument, which seems a little ugly.

Finally, there are two places where the things can silently fail:

  • If a negative or very large number is passed as a minimum for displaying time, it'll simply not display the time.
  • If a bash shell already has DEBUG trapped, re-trapping it will break whatever was depending on it (and there's no way to un-trap that function). Since we can't do timing without the DEBUG trap, the timer is just silently disabled. This has already bitten me once in testing (I forgot I had a primitive timer running already), but I'm not sure if we want to display a warning on every launch.

src/init.rs Outdated Show resolved Hide resolved
src/init.rs Outdated Show resolved Hide resolved
src/init.rs Show resolved Hide resolved
src/modules/timer.rs Outdated Show resolved Hide resolved
src/modules/timer.rs Outdated Show resolved Hide resolved
src/modules/timer.rs Outdated Show resolved Hide resolved
src/modules/timer.rs Outdated Show resolved Hide resolved
src/modules/character.rs Outdated Show resolved Hide resolved
src/init.rs Outdated Show resolved Hide resolved
@matchai matchai added the ✨ enhancement A new feature implementation. label Jul 27, 2019
@matchai
Copy link
Member

matchai commented Jul 27, 2019

I've utterly goobered the git stuff--it's showing 25 commits to merge in, some of which I'm pretty sure were already merged. If anyone knows how to fix this, please let me know.

This has happened to me more times than I can count. 😅
Don't worry about it. We'll be squash-merging your changes into a single commit.

src/modules/mod.rs Outdated Show resolved Hide resolved
@jletey
Copy link
Member

jletey commented Jul 27, 2019

it's showing 25 commits to merge in, some of which I'm pretty sure were already merged.

yes @chipbuster @matchai ... this happens when you're on a branch (not the master branch) that you have previously opened a PR for (that is now merged) ... and then you create a new branch off of this non-master branch

so your new branch has all of your old commits (which have now been merged into master), and all of your new commits

src/modules/timer.rs Outdated Show resolved Hide resolved
@chipbuster
Copy link
Contributor Author

chipbuster commented Jul 28, 2019

@johnletey Thanks, that explains it. I think I accidentally merged starship/master into my branch, instead of rebasing my branch onto it, and they diverged some time ago.

If we don't squash on merge, do you know how the process is handled? Will Github detect that some of the commits are duplicate and skip over them?

@matchai
Copy link
Member

matchai commented Jul 28, 2019

If we don't squash we'll get repeat commits. 🤷‍♂️

@jletey
Copy link
Member

jletey commented Jul 29, 2019

If we don't squash on merge, do you know how the process is handled? Will Github detect that some of the commits are duplicate and skip over them?

Oh boy ... not quite sure ... that's as far as my Github expertise goes!

@Zegnat
Copy link

Zegnat commented Jul 29, 2019

[…] I've utterly goobered the git stuff--it's showing 25 commits to merge in, some of which I'm pretty sure were already merged. If anyone knows how to fix this, please let me know.

Assuming you have at least one branch that is up-to-date with starship/master, you can rebase against that one. I like to setup an upstream remote for that, then you should be able to do something like:

git rebase -i upstream/master

With this git should already try and deduplicate commits, and you will be given the option to drop any commits you recognise as unnecessary.

src/init.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
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.

Just a few last comments. This should be ready to merge once these are addressed. 😄

src/modules/cmd_duration.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@matchai
Copy link
Member

matchai commented Jul 31, 2019

I totally forgot that we also need to add some integration tests and documentation for this new module.
Would you like me to take care of it after this PR, or would you rather tackle it yourself?

The following would need doing:

  • Integration tests would be added in a cmd_duration.rs file in tests/testsuite
  • A new heading would be made in docs/config/README.md
  • README.md would be updated to no longer say that this is a planned feature

@chipbuster
Copy link
Contributor Author

I'd be happy to do it, if you're willing to deal with a couple more reviews. I'm also starting to think I should hold off on working on this until this conference is over (Friday) and I have a chance to make up some sleep--I'm made some awfully silly mistakes the last few days.

What would need to be added for integration tests? Would it just be a bunch of tests using render_module with args for various cmd-durations?

@matchai
Copy link
Member

matchai commented Jul 31, 2019

Haha, no rush. I don't mind going through some more reviews. Enjoy your conference and we can take care of this later. 😄

What would need to be added for integration tests? Would it just be a bunch of tests using render_module with args for various cmd-durations?

Yep, that should be fine. This is what I was thinking:

  • Testing the output at > and < the default cmd-duration criteria
  • The output at > and < a cmd-duration criteria set by configuration

The recent directory tests are a good reference:
https://github.com/starship/starship/pull/120/files#diff-901574afaba53d9b8e5b1e81fe984069

chipbuster and others added 9 commits August 7, 2019 22:32
This adds support for qualified paths (e.g. using
`/usr/local/bin/zsh` instead of `zsh`) to init.rs.

The function now converts the shell name into an
OsStr, then to a Path, then gets the file stem,
and unwraps back into a str. While this process can
fail (yielding a None), it's highly unlikely to unless
the user has messed with their shells or there's an
issue in Starship--therefore, the failure message in
this case simply asks the user to file a bug report.
In the bash/zsh init scripts, PS1/PROMPT are being evaluated
once (when the variable is assigned to) because double quotes
do not sufficiently defer command substitution. This leads to
the prompt getting "stuck" wherever starship was initialized.

This changes the init script to use single quotes, deferring
command substitution until the prompt variable is evaluated.
Add support for paths to init function

This adds support for qualified paths (e.g. using
`/usr/local/bin/zsh` instead of `zsh`) to init.rs.

The function now converts the shell name into an
OsStr, then to a Path, then gets the file stem,
and unwraps back into a str. While this process can
fail (yielding a None), it's highly unlikely to unless
the user has messed with their shells or there's an
issue in Starship--therefore, the failure message in
this case simply asks the user to file a bug report.
Making use of PROMPT_COMMAND in bash and precmd in zsh, the prompt is no longer being expanded and rendered when the variable is initially set.
Add all necessary code for starship to take in time elapsed since
command execution started with flag --elapsed. It is now possible to run
e.g. `starship prompt --status=0 --elapsed=100` and get a timer print.
Add support for all three shells into init.rs. This modifies the shell
init scripts quite heavily.
chipbuster and others added 8 commits August 7, 2019 22:33
The option min_time in section [timer] will affect the minimum time for
when the shell displays job time. The default value is 2.
Caused by implementing the timer module.
Co-Authored-By: Matan Kushner <hello@matchai.me>
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.

We're almost at the finish line! 🏁
Just a few small fixes below and we should be ready to merge it in. 😄

docs/config/README.md Outdated Show resolved Hide resolved
docs/config/README.md Outdated Show resolved Hide resolved
docs/config/README.md Outdated Show resolved Hide resolved
src/modules/cmd_duration.rs Outdated Show resolved Hide resolved
tests/testsuite/cmd_duration.rs Outdated Show resolved Hide resolved
tests/testsuite/cmd_duration.rs Outdated Show resolved Hide resolved
tests/testsuite/cmd_duration.rs Outdated Show resolved Hide resolved
tests/testsuite/cmd_duration.rs Show resolved Hide resolved
docs/config/README.md Outdated Show resolved Hide resolved
src/init.rs Outdated Show resolved Hide resolved
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 great! Thank you for taking the time to make sure this feature works as well as possible. I was really looking forward to having this module added to the prompt. 🎉😄

Let's ship it! :shipit:

@matchai matchai merged commit 3daf3dd into starship:master Aug 8, 2019
@matchai
Copy link
Member

matchai commented Aug 8, 2019

@all-contributors please add @chipbuster for docs and test.

@allcontributors
Copy link
Contributor

@matchai

I've put up a pull request to add @chipbuster! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement A new feature implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement the prompt module for execution time
4 participants