andrewchambers / janetsh

A powerful new shell that uses the janet programming language for both the implementation and repl.
https://janet-shell.org
MIT License
372 stars 13 forks source link

WIP: Run hook functions before & after line execution #194

Open x4lldux opened 5 years ago

x4lldux commented 5 years ago

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))))
andrewchambers commented 5 years ago

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

x4lldux commented 5 years ago

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.

andrewchambers commented 5 years ago

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

andrewchambers commented 5 years ago

It seems to me this patch breaks multi line input.

x4lldux commented 5 years ago

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.

andrewchambers commented 5 years ago

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.

pauldub commented 5 years ago

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:

x4lldux commented 5 years ago

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

andrewchambers commented 5 years ago

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

x4lldux commented 5 years ago

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

andrewchambers commented 5 years ago

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.

x4lldux commented 5 years ago

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.