fish shell (and
zsh) let you register commands that run before and after every command.
I have my shell configured to print the exit status if nonzero and the amount of time taken to execute the command in seconds if longer than 10 seconds.
Here's an implementation of that behavior in
# ~/.config/fish/config.fish # set a global variable to the current epoch time in seconds # before executing any comment function get_start --on-event fish_preexec set -g cmdstart (/bin/date '+%s') end # inspect previous exit status, write "Exit n" # where n is a non-zero exit status function show_exit_status switch $status case 0 true case '*' echo "Exit $status" end end # compute the time in seconds since the command was started. # print it if the time exceeds 10 seconds function show_time_consumed set taken (math (/bin/date '+%s') - $cmdstart) if math $taken '>=' 10 >/dev/null printf 'Time: %ss\n' $taken end end # show the time consumed and the exit status # after the command is run function postcmd --on-event fish_postexec show_exit_status show_time_consumed echo end
And an example of what it does
> sleep 14 Time: 14s sleep 14; /bin/false Exit 1 Time: 14s
I'm trying to figure out how to emulate the same behavior in
Here's my initial attempt to port it:
(setq inhibit-startup-echo-area-message "USERNAME") (setq inhibit-startup-message t) (defvar eshell-current-command-start-time (float-time)) (defun eshell-current-command-start () "store the current starting time" (setf eshell-current-command-start-time (float-time))) (defun eshell-current-command-stop () "write the stopping time" (let ((the-time (float-time))) (message (number-to-string (- the-time eshell-current-command-start-time))))) (add-hook 'eshell-pre-command-hook #'eshell-current-command-start) (add-hook 'eshell-post-command-hook #'eshell-current-command-stop)
It has some problems that hamper its usefulness.
- When you hit enter after an empty command, the
post-command-hookruns, but the
Is there a straightforward way of grabbing the time using
float-time before and after every command executes (including empty commands) and displaying it in the buffer the same way that the output of a command would be displayed?