shell: Measure command durations.

master
Pierre Neidhardt 2020-05-23 14:00:12 +02:00
parent e0e5a6f01d
commit 778b9a1691
1 changed files with 49 additions and 3 deletions

View File

@ -32,10 +32,56 @@ prefix arg shell buffer doesn't exists, create it and switch to it."
(advice-add 'helm-ff-switch-to-eshell :override 'ambrevar/helm-ff-switch-to-shell)
(defun ambrevar/shell-ignore-history ()
(defun ambrevar/shell-setup ()
(set (make-variable-buffer-local 'comint-input-history-ignore)
(regexp-opt '("^#" "^ " "^cd "))))
(regexp-opt '("^#" "^ " "^cd ")))
(setq comint-prompt-regexp "^[^#$%>]*
[#$%>] *"))
(add-hook 'shell-mode-hook 'ambrevar/shell-ignore-history)
(add-hook 'shell-mode-hook 'ambrevar/shell-setup)
(defun ambrevar/shell-prompt ()
(buffer-substring-no-properties
(save-excursion
(re-search-backward comint-prompt-regexp))
(save-excursion
(comint-bol))))
(defun ambrevar/shell-propertize-prompt () ; Inspired by `shx--propertize-prompt'.
"Add a mouseover timestamp to the last prompt."
(let ((inhibit-read-only t)
(inhibit-field-text-motion t))
(add-text-properties
(save-excursion
(re-search-backward comint-prompt-regexp)
(point))
(process-mark (get-buffer-process (current-buffer)))
`(help-echo ,(format-time-string "%F %T")))))
(defun ambrevar/shell-send-input ()
"Send or parse the input currently written at the prompt.
In normal circumstances this input is additionally filtered by
`shx-filter-input' via `comint-mode'."
(interactive)
(ambrevar/shell-propertize-prompt)
(comint-send-input))
(define-key shell-mode-map (kbd "<return>") 'ambrevar/shell-send-input)
(defun ambrevar/shell-command-duration ()
"Return duration of command at point in a `shell' buffer."
(interactive)
(let ((begin (save-excursion
(comint-previous-prompt 1)
(re-search-backward comint-prompt-regexp)
(parse-time-string (get-text-property (point) 'help-echo))))
(end (save-excursion
(comint-next-prompt 1)
(re-search-backward comint-prompt-regexp)
(parse-time-string (ambrevar/shell-prompt)))))
(message "Command took %fs seconds."
(- (float-time (apply 'encode-time end))
(float-time (apply 'encode-time begin))))))
;; TODO: Narrow to output.
(provide 'init-shell)