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

post_cmd functionality addition #134

Closed
Tallicia opened this issue Aug 27, 2022 · 2 comments
Closed

post_cmd functionality addition #134

Tallicia opened this issue Aug 27, 2022 · 2 comments

Comments

@Tallicia
Copy link

Is there a way to add a post_cmd array so after the pre_cmds and command is run something additional is run, such as a duration caclulation?

@akinomyoga
Copy link
Contributor

Is there a way to add a post_cmd array so after the pre_cmds and command is run something additional is run, such as a duration caclulation?

I think the names of the hooks are somehow opposite. I guess what you are calling pre_cmds is actually what is called preexec in bash-preexec, and what you call post_cmd is usually called postexec (unsupported by bash-preexec) but is mostly the same as what is called precmd in bash-preexec. The difference is that precmd is also called on the startup and after empty commands, while postexec is called only after the actual user commands. You can set in preexec a flag variable telling that the command has been executed, and then you check and clear that flag in precmd to determine whether it should run postexec hooks.

@dimo414
Copy link
Collaborator

dimo414 commented Sep 4, 2022

For calculating execution duration you can indeed use precmd + preexec (I have an example in prompt.gem, see prompt::_command_start and prompt::_set_ps1).

However note that bash-preexec is simply a user-friendly wrapper around the hooks bash exposes, and bash doesn't expose enough hooks to fully implement pre- and post- hooks for all events in an interactive shell. For example #28 discusses using the relatively-new $PS0 hook but the consensus there is it's not powerful enough to be generally useful. Similarly, precmd is not equivalent to "postexec" even though it's usually similar enough to not matter. precmd runs before a new command prompt is printed, which generally happens right after an execution finishes but isn't strictly required (e.g. precmd runs when a new shell opens, even though nothing has executed yet).

@dimo414 dimo414 closed this as completed Sep 4, 2022
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

No branches or pull requests

3 participants