janetsh icon indicating copy to clipboard operation
janetsh copied to clipboard

WIP: Run hook functions before & after line execution

Open x4lldux opened this issue 5 years ago • 12 comments

Runs *pre-exec-hook* & *post-exec-hook* functions before/after execution of a line. Can be used to time how long a command took:

(var *last-command-duration* 0)
(var last-command-start 0)

(var *pre-exec-hook* (fn [&] (set last-command-start (os/clock))))
(var *post-exec-hook* (fn [&]
                        (set *last-command-duration*
                             (- (os/clock) last-command-start))))

x4lldux avatar Jun 04 '19 17:06 x4lldux

I don't fully understand the use for this feature, if I want to time something usually I would just write a time function.

andrewchambers avatar Jun 04 '19 21:06 andrewchambers

Borrowed the idea from ZSH. It's usually used to enhance prompt and timing is just an example (though I'm often wandering how long does something takes only after starting it ;) ). This is needed for another PR which allows cd - go to previous directory.

x4lldux avatar Jun 04 '19 21:06 x4lldux

I understand - I am still unsure, I will need to think about it and read the zsh documentation.

andrewchambers avatar Jun 04 '19 22:06 andrewchambers

It seems to me this patch breaks multi line input.

andrewchambers avatar Jun 05 '19 05:06 andrewchambers

Should be easy to fix. Basically need to do the same thing you do in want-implicit-parens and detect that. I'll test it in few days.

x4lldux avatar Jun 05 '19 06:06 x4lldux

Right, I am leaning towards allowing the repl changes to hide the exit codes, in which case that fix may be able to go there.

andrewchambers avatar Jun 05 '19 06:06 andrewchambers

There is a kind of hook commonly found in shells which is executed right before showing the prompt (and sometimes also after showing it).

This is used by direnv integrations, which I must currently call in my prompt function.

For reference:

  • ZSH has precmd_functions http://zsh.sourceforge.net/Doc/Release/Functions.html#index-functions_002c-hook
  • bash has $PROMPT_COMMAND https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
  • fish has --on-event fish_prompt https://fishshell.com/docs/current/commands.html#fish_prompt
  • elvish has @edit:before-readline` https://manpages.debian.org/testing/elvish/elvish-edit.7.en.html#Hooks

pauldub avatar Jun 05 '19 06:06 pauldub

@pauldub You're right, equivalent of $PROMPT_COMMAND/precmd_function should be added. Calling direnv in *prompt* is kind a hackish and entangles concerns.

x4lldux avatar Jun 05 '19 08:06 x4lldux

I like direnv, so that is a good argument for me.

andrewchambers avatar Jun 05 '19 08:06 andrewchambers

Once changes to repl in #161 are done, I will update this & add precmd hook also.

x4lldux avatar Jun 06 '19 05:06 x4lldux

I would prefer on-crash be renamed to on-error or something else, it isn't necessarily a 'crash'. I know one use for this hook is what nixos does, which is to offer a list of packages for a user to install if a binary is missing.

andrewchambers avatar Jun 06 '19 05:06 andrewchambers

Sure, so *post-crash-hook* -> *post-error-hook*? Fefora has something similar. Use cases for those hooks are many! I saw a precmd hook function that changes terminal's profile so the background is red when you ssh to a remote server just to be extra alert.

x4lldux avatar Jun 06 '19 08:06 x4lldux