eshell-atuin: fix post-exec

This commit is contained in:
Pavel Korytov 2024-03-07 15:57:37 +03:00
parent 1e7ccba75c
commit 5043d8b879

View file

@ -59,7 +59,7 @@
(with-temp-buffer (with-temp-buffer
(let ((ret (call-process (let ((ret (call-process
eshell-atuin-executable nil t nil eshell-atuin-executable nil t nil
"history" "start" input))) "history" "start" "--" input)))
(unless (= 0 ret) (unless (= 0 ret)
(error "`atuin history start' retured %s: %s" ret (buffer-string))) (error "`atuin history start' retured %s: %s" ret (buffer-string)))
(buffer-substring-no-properties (buffer-substring-no-properties
@ -67,27 +67,38 @@
(setq eshell-atuin--last-command-start (current-time)))) (setq eshell-atuin--last-command-start (current-time))))
(defun eshell-atuin--post-exec () (defun eshell-atuin--post-exec ()
(let* ((proc-args `("history" "end" "-e" (when eshell-atuin--history-id
,(number-to-string eshell-last-command-status) (let* ((proc-args
,@(when eshell-atuin--last-command-start `(,eshell-atuin-executable
(list "-d" "history" "end"
(prog1 "--exit" ,(number-to-string eshell-last-command-status)
(number-to-string ,@(when eshell-atuin--last-command-start
(float-time (list
(time-subtract "--duration"
(current-time) (prog1
eshell-atuin--last-command-start))) (concat (number-to-string
(setq eshell-atuin--last-command-start nil)))) (round
,eshell-atuin--history-id)) (*
(buf (generate-new-buffer "*atuin-output*")) 1000000000 ; nanoseconds
(proc (apply #'start-process "atuin-history-stop" buf (float-time
eshell-atuin-executable proc-args))) (time-subtract
(set-process-sentinel (current-time)
proc eshell-atuin--last-command-start))))))
(lambda (process _msg) (setq eshell-atuin--last-command-start nil))))
(pcase (process-status process) ,eshell-atuin--history-id))
('exit (kill-buffer buf)) (buf (generate-new-buffer "*atuin-output*"))
('fatal (error "Error in running 'atuin history stop'. See *atuin-output*"))))))) (proc (with-environment-variables (("ATUIN_LOG" "error"))
(start-process-shell-command "atuin-history-stop" buf
(string-join proc-args " ")))))
(set-process-sentinel
proc
(lambda (process _msg)
(when (eq (process-status process) 'exit)
(unwind-protect
(unless (= (process-exit-status process) 0)
(error "`atuin history end' returned %s: %s" (process-exit-status process)
(with-current-buffer buf (buffer-string))))
(kill-buffer buf))))))))
(defun eshell-atuin--init-session () (defun eshell-atuin--init-session ()
(setenv "ATUIN_SESSION" (setenv "ATUIN_SESSION"
@ -111,5 +122,19 @@
(advice-remove #'eshell-send-input #'eshell-atuin--pre-exec) (advice-remove #'eshell-send-input #'eshell-atuin--pre-exec)
(remove-hook 'eshell-post-command-hook #'eshell-atuin--post-exec)))) (remove-hook 'eshell-post-command-hook #'eshell-atuin--post-exec))))
(defun eshell-atuin--get-history ()
(with-temp-buffer
(let ((ret (call-process
eshell-atuin-executable nil t nil
"history" "list" "-f" "")))
(unless (= 0 ret)
(error "`atuin history list' retured %s: %s" ret (buffer-string)))
(buffer-substring-no-properties
(point-min) (point-max)))))
(defun eshell-atuin-history ()
(interactive)
)
(provide 'eshell-atuin) (provide 'eshell-atuin)
;;; eshell-atuin.el ends here ;;; eshell-atuin.el ends here