Skip to content

Commit

Permalink
Add hover-clear-buffer and hover-clear-buffer-on-hot-restart
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdallo committed Sep 2, 2020
1 parent 6f9ed1a commit 989faeb
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions hover.el
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
;;; hover.el --- Package to use hover with flutter -*- lexical-binding: t; -*-
;;; hover.el --- Package to use hover with flutter -*- lexical-binding: t; -*-

;; Copyright (C) 2020

;; Author: Eric Dallo
;; Version: 0.1.0
;; Version: 0.2
;; Package-Requires: ((emacs "24.5"))
;; Keywords: hover, flutter, mobile, tools
;; URL: https://github.com/ericdallo/hover.el
Expand Down Expand Up @@ -52,7 +52,7 @@

(defcustom hover-hot-reload-on-save nil
"If non-nil, triggers hot-reload on buffer save."
:type '(choice (const nil) (other t))
:type 'boolean
:group 'hover)

(defcustom hover-screenshot-path nil
Expand All @@ -72,6 +72,11 @@ Default is hover's default uri"
:type 'string
:group 'hover)

(defcustom hover-clear-buffer-on-hot-restart nil
"Clear hover buffer after a hot restart."
:type 'boolean
:group 'hover)

(defvar hover-mode-map (copy-keymap comint-mode-map)
"Basic mode map for `hover-run'.")

Expand Down Expand Up @@ -134,11 +139,6 @@ The function's name will be NAME prefixed with 'hover-'."
(select-window (get-buffer-window current)))
(hover--send-command command))

(defun hover--hot-reload ()
"Hot reload hover if it is already running."
(when (hover--running-p)
(hover--run-command-on-hover-buffer "r")))

(defun hover--build-screenshot-file-name ()
"Build screenshot file name with a timestamp."
(let ((formatted-timestamp (format-time-string "%Y-%m-%dT%T")))
Expand All @@ -151,6 +151,14 @@ Save on FILE-PATH and use the observatory URI given."
(format "%s screenshot --type=rasterizer --out=%s --observatory-uri=%s" (hover-build-flutter-command) file-path uri)
t))

(defun hover--clear-buffer ()
"Clear hover buffer."
(if (hover--running-p)
(with-current-buffer hover-buffer-name
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer)))
(error "Hover is not running")))


;;; Key bindings

Expand Down Expand Up @@ -203,38 +211,23 @@ the `hover` process."
(concat (file-name-as-directory hover-flutter-sdk-path) "bin/flutter")
"flutter"))

;;;###autoload
(defun hover-run (&optional args)
"Execute `hover run` inside Emacs.
ARGS is a space-delimited string of CLI flags passed to
`hover`, and can be nil. Call with a prefix to be prompted for
args."
(interactive
(list (when current-prefix-arg
(read-string "Args: "))))
(hover--with-run-proc
args
(pop-to-buffer-same-window buffer)))

;;;###autoload
(defun hover-run-or-hot-reload ()
"Start `hover run` or hot-reload if already running."
(interactive)
(if (hover--running-p)
(hover--run-command-on-hover-buffer "r")
(hover-run)))

;;;###autoload
(defun hover-run-or-hot-restart ()
"Start `hover run` or hot-restart if already running."
(interactive)
(if (hover--running-p)
(hover--run-command-on-hover-buffer "R")
(progn
(hover--run-command-on-hover-buffer "R")
(when hover-clear-buffer-on-hot-restart
(hover--clear-buffer)))
(hover-run)))

; flutter screenshot --observatory-uri=http://127.0.0.1:50300 --type=rasterizer
;;;###autoload
(defun hover-take-screenshot ()
"Take screenshot of current `hover` application using `flutter screenshot`.
Saves screenshot on `hover-screenshot-path`."
Expand All @@ -246,6 +239,31 @@ Saves screenshot on `hover-screenshot-path`."
(file-path (concat screenshot-path file-name)))
(hover--take-screenshot file-path hover-observatory-uri)))

(defun hover-clear-buffer ()
"Clear current hover buffer output."
(interactive)
(hover--clear-buffer))

;;;###autoload
(defun hover-hot-reload ()
"Hot reload hover if it is already running."
(when (hover--running-p)
(hover--run-command-on-hover-buffer "r")))

;;;###autoload
(defun hover-run (&optional args)
"Execute `hover run` inside Emacs.
ARGS is a space-delimited string of CLI flags passed to
`hover`, and can be nil. Call with a prefix to be prompted for
args."
(interactive
(list (when current-prefix-arg
(read-string "Args: "))))
(hover--with-run-proc
args
(pop-to-buffer-same-window buffer)))

;;;###autoload
(define-derived-mode hover-mode comint-mode "Hover"
"Major mode for `hover-run'."
Expand All @@ -255,7 +273,7 @@ Saves screenshot on `hover-screenshot-path`."
(let ((flutter-command-path (concat (file-name-as-directory hover-flutter-sdk-path) "bin")))
(setenv "PATH" (concat flutter-command-path ":" (getenv "PATH")))))
(when hover-hot-reload-on-save
(add-hook 'after-save-hook #'hover--hot-reload)))
(add-hook 'after-save-hook #'hover-hot-reload)))

(provide 'hover)
;;; hover.el ends here

0 comments on commit 989faeb

Please sign in to comment.