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

Revamp command duration helper/plugin #1906

Merged
merged 7 commits into from
Mar 4, 2022

Conversation

gaelicWizard
Copy link
Contributor

@gaelicWizard gaelicWizard commented Jul 26, 2021

Description

Combine themes/command_duration.theme.bash and plugins/cmd-returned-notify.plugin.bash, and rewrite them to use $EPOCHSECONDS (or $SECONDS on older Bash) while eliminating temporary files on disk.

Motivation and Context

Removing the temp file reduces complexity (no need to locate $TMPDIR, read file back), reduces bug surface (what happens if /tmp cleaner runs before prompt returns?), and improves performance (no need to touch the disk!). Adopting $EPOCHSECONDS removes entirely the need for subshells or external date invocation, improving performance and reducing points of failure. Sharing the preexec hook reduces total hooks running, and reduces duplicate code.

And, of course, command_duration didn't work at all on Mac OS X since BSD date doesn't support %1N; now it works perfectly on both Mac and Linux (and BSD!) and doesn't use date at all.

How Has This Been Tested?

This change is live in my branch on my system now.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the code style of this project.
  • If my change requires a change to the documentation, I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • If I have added a new file, I also added it to clean_files.txt and formatted it using lint_clean_files.sh.
  • I have added tests to cover my changes, and all the new and existing tests pass.

@gaelicWizard gaelicWizard force-pushed the command_duration branch 3 times, most recently from a70f425 to 3a8c78e Compare July 26, 2021 22:25
@gaelicWizard
Copy link
Contributor Author

I've just discovered cmd-returned-notify.plugin and now I'm confused why there's two of these...?

@NoahGorny
Copy link
Member

I've just discovered cmd-returned-notify.plugin and now I'm confused why there's two of these...?

So, command-duration is used for themes (to display information in the prompt) while the plugin is used to notify (BEL) when tasks are completed. They have similar usage, but quite different user interface

Copy link
Member

@NoahGorny NoahGorny left a comment

Choose a reason for hiding this comment

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

I have left a comment. Also, for some reason, this patch does not for me, the preexec function seems to not execute.. Does this work on your end @gaelicWizard ?

themes/command_duration.theme.bash Outdated Show resolved Hide resolved
@gaelicWizard gaelicWizard force-pushed the command_duration branch 3 times, most recently from 475c56f to 2a1aceb Compare August 15, 2021 03:13
@gaelicWizard gaelicWizard marked this pull request as draft August 22, 2021 20:29
@gaelicWizard
Copy link
Contributor Author

I'm marking this draft b/c I think I'm going to try to just merge with the plugin. Use one function to store the start time, then let the plugin and the theme separately use the same variable for (a) beep, or (b) prompt display.

@gaelicWizard gaelicWizard force-pushed the command_duration branch 2 times, most recently from 6ba524b to 0e38baf Compare August 26, 2021 21:25
@gaelicWizard gaelicWizard force-pushed the command_duration branch 9 times, most recently from 21fcd63 to cf6ef65 Compare September 9, 2021 18:41
@gaelicWizard
Copy link
Contributor Author

I've had to rebase on my shellcheck (#1917) branch, but shellcheck is passing now!

@gaelicWizard gaelicWizard force-pushed the command_duration branch 4 times, most recently from 569cfeb to 92175b6 Compare September 10, 2021 21:18
@davidpfarrell davidpfarrell changed the title Remove COMMAND_DURATION_FILE DRAFT: Remove COMMAND_DURATION_FILE Sep 11, 2021
@gaelicWizard gaelicWizard marked this pull request as ready for review January 26, 2022 18:21
@gaelicWizard gaelicWizard force-pushed the command_duration branch 2 times, most recently from 5da7bcd to fa5aabe Compare January 28, 2022 12:02
@gaelicWizard
Copy link
Contributor Author

Rebased on current master

@gaelicWizard gaelicWizard force-pushed the command_duration branch 2 times, most recently from 9726068 to 3e3bd4a Compare February 15, 2022 04:54
@gaelicWizard
Copy link
Contributor Author

Rebased & ready!

@gaelicWizard gaelicWizard force-pushed the command_duration branch 2 times, most recently from 401e032 to 161a9eb Compare February 20, 2022 19:34
Copy link
Member

@NoahGorny NoahGorny left a comment

Choose a reason for hiding this comment

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

LGTM

@gaelicWizard gaelicWizard force-pushed the command_duration branch 2 times, most recently from 1c3f5c5 to 71c5a75 Compare March 4, 2022 07:44
@gaelicWizard
Copy link
Contributor Author

BATS error should be fixed by #2105.

Calculate the position (from 1 to 12) of the hour hand on the clock emoji used for the _command_duration string.

Expressly handle COMMAND_DURATION_COLOR as blank when undefined.
gaelicWizard and others added 5 commits March 4, 2022 12:58
Fallback to `$SECONDS` for older versions of _Bash_.

Instead of shortcircuiting the definition, just short-circuit the function. This allows the variable to be set later, e.g. on theme change.
Use `$EPOCHREALTIME` (or `$SECONDS`) built-in variable provided by Bash instead of `date +%s`. We're only measuing the difference in seconds, so avoid both the binary invocation as well as the subshell.

Alsö, Reduce environmental pollution by not exporting every variable, and unsetting when done.

Change variable names to match lib/command-duration

Remove `preexec_return_notification()` in favor of `lib/command-duration`'s `_command_duration_pre_exec()`.

This should now use the same preexec hook and variables as the theme library `command_duration`.

tests: handle nanoseconds
Rename the `theme/command_duration.theme` file as it's not really got anything to do with theming or SCM.
@gaelicWizard gaelicWizard merged commit 3a77807 into Bash-it:master Mar 4, 2022
@gaelicWizard gaelicWizard deleted the command_duration branch March 4, 2022 21:00
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.

None yet

2 participants