Mode-line indicator for new release

This commit is contained in:
syl20bnr 2015-01-08 00:34:23 -05:00
parent b12a167106
commit 03943dd059
9 changed files with 164 additions and 10 deletions

View File

@ -22,6 +22,7 @@
- [OS X](#os-x)
- [Install](#install)
- [Update](#update)
- [Update notification](#update-notification)
- [Configuration](#configuration)
- [Configuration layers](#configuration-layers)
- [Dotfile (.spacemacs)](#dotfile-spacemacs)
@ -192,6 +193,20 @@ Spacemacs currently requires manual updates using the following procedure:
3. Restart Emacs to complete the upgrade.
## Update notification
For convenience an indicator is displayed in the mode-line whenever a new
version of `Spacemacs` is available.
Symbol | Description
:------------------------------------:|----------------------------------
![git-new](doc/img/update-green.png) | less than 3 releases behind
![git-del](doc/img/update-orange.png) | less than 5 releases behind
![git-mod](doc/img/update-red.png) | 5 or more releases behind
**Note:**
Update of `Spacemacs` by clicking on the indicator will be implemented _soon_.
# Configuration
`Spacemacs` divides its configuration into self-contained units called

View File

@ -588,6 +588,6 @@ deleted safely."
(format "[%s packages loaded in %.3fs]\n"
(configuration-layer//initialized-packages-count)
elapsed)))
)))
(spacemacs/check-for-new-version spacemacs-version-check-interval))))
(provide 'configuration-layer)

View File

@ -4,12 +4,21 @@
(require 'subr-x nil 'noerror)
(require 'emacs-backports)
(defconst spacemacs-version "0.43.0"
(defconst spacemacs-version "0.40.0"
"Spacemacs version.")
(defconst spacemacs-min-version "24.3"
"Mininal required version of Emacs.")
(defconst spacemacs-repository "spacemacs"
"Name of the Spacemacs remote repository.")
(defconst spacemacs-repository-owner "syl20bnr"
"Name of the Spacemacs remote repository owner.")
(defgroup spacemacs nil
"Spacemacs customizations."
:group 'starter-kit
:prefix 'spacemacs-)
;; paths
(defconst spacemacs-core-directory
(expand-file-name (concat user-emacs-directory "core/"))
@ -36,6 +45,16 @@
;; if you have a dropbox, then ~/Dropbox/emacs is added to load path
(add-to-list 'load-path (concat user-dropbox-directory "emacs/"))
;; new version variables
(defvar spacemacs-new-version nil
"If non-nil a new Spacemacs version is available.")
(defvar spacemacs-version-check-timer nil
"The current timer for new version check.")
(defvar spacemacs-version-check-interval "6 hours"
"Time between two version checks.")
(defvar spacemacs-version-check-lighter "[+]"
"Text displayed in the mode-line when a new version is available.")
;; loading progress bar variables
(defvar spacemacs-title-length 75)
(defvar spacemacs-loading-counter 0)
@ -51,11 +70,6 @@
(defvar spacemacs-loading-dots-chunk-threshold 0)
(defvar spacemacs-solarized-dark-createdp nil)
(defgroup spacemacs nil
"Spacemacs customizations."
:group 'starter-kit
:prefix 'spacemacs-)
(define-derived-mode spacemacs-mode special-mode "spacemacs-mode"
"Spacemacs major mode for startup screen."
:syntax-table nil
@ -140,6 +154,10 @@
(setq-default evil-want-fine-undo nil)
(spacemacs/load-or-install-package 'evil t)
(spacemacs/load-or-install-package 'evil-leader t)
;; check for new version
(if dotspacemacs-mode-line-unicode-symbols
(setq-default spacemacs-version-check-lighter "[⬆]"))
(spacemacs/set-new-version-lighter-mode-line-faces)
;; motion state since this is a special mode
(add-to-list 'evil-motion-state-modes 'spacemacs-mode))
@ -202,6 +220,93 @@ FILE-TO-LOAD is an explicit file to load after the installation."
"Change the default welcome message of minibuffer to another one."
(message "Spacemacs is ready."))
(defun spacemacs//get-last-version (repo owner)
"Return the last version fetched from Github REPO of OWNER."
(let* ((api (gh-repos-api "api"))
(repo (oref (gh-repos-repo-get api spacemacs-repository
spacemacs-repository-owner) :data))
(tags (oref (gh-repos-repo-tags api repo) :data))
(last-version (cdr (assq 'name (nth 0 tags)))))
(when last-version
(save-match-data
(string-match "^.*\\([0-9]+\\.[0-9]+\\.[0-9]+\\)$" last-version)
(match-string 1 last-version)))))
(defun spacemacs/check-for-new-version (&optional interval)
"Periodicly check for new for new Spacemacs version.
Update `spacemacs-new-version' variable if any new version has been
found."
;; (message "Start checking for new version...")
(async-start
(lambda ()
(add-to-list 'load-path (concat user-emacs-directory "core/"))
(require 'spacemacs-mode)
(spacemacs/load-or-install-package 'pcache t)
(spacemacs/load-or-install-package 'logito t)
(spacemacs/load-or-install-package 'gh t "gh-repos.el")
(spacemacs//get-last-version spacemacs-repository
spacemacs-repository-owner))
(lambda (result)
(when result
(unless (or (version< result spacemacs-version)
(string= result spacemacs-version)
(if spacemacs-new-version
(string= result spacemacs-new-version)))
(message "New version of Spacemacs available: %s" result)
(setq spacemacs-new-version result)))))
(when interval
(setq spacemacs-version-check-timer
(run-at-time t (timer-duration interval)
'spacemacs/check-for-new-version))))
(defun spacemacs//deffaces-new-version-lighter (state)
"Define a new version lighter face for the given STATE."
(let* ((fname (intern (format "spacemacs-mode-line-new-version-lighter-%s-face"
(symbol-name state))))
(foreground (face-foreground state)))
(eval `(defface ,fname '((t ()))
,(format "Color for new version lighter in mode line (%s)."
(symbol-name state))
:group 'spacemacs))
(set-face-attribute fname nil
:foreground foreground
:box (face-attribute 'mode-line :box))))
(defun spacemacs/set-new-version-lighter-mode-line-faces ()
"Define or set the new version lighter mode-line faces."
(mapcar 'spacemacs//deffaces-new-version-lighter
'(error warning success)))
(spacemacs/set-new-version-lighter-mode-line-faces)
(defun spacemacs//compute-version-score (version)
"Returns an integer from the version list.
Example: (1 42 3) = 1 042 003"
(let ((result 0)
(rev (reverse version)))
(dotimes (i 3)
(setq result (+ result (* (nth i rev) (expt 10 (* i 3))))))
result))
(defun spacemacs//compute-version-score (version)
"Returns an integer from the version list.
Example: (1 42 3) = 1 042 003"
(let ((i -1))
(reduce '+ (mapcar (lambda (n) (setq i (1+ i)) (* n (expt 10 (* i 3))))
(reverse version)))))
(defun spacemacs/get-new-version-lighter-face (current new)
"Return the new version lighter face given the difference between the CURRENT
version and the NEW version."
(let* ((lcur (version-to-list current))
(lnew (version-to-list new))
(scur (spacemacs//compute-version-score lcur))
(snew (spacemacs//compute-version-score lnew))
(diff (- snew scur)))
(cond
((< diff 3000) 'spacemacs-mode-line-new-version-lighter-success-face)
((< diff 5000) 'spacemacs-mode-line-new-version-lighter-warning-face)
(t 'spacemacs-mode-line-new-version-lighter-error-face))))
(defun spacemacs/set-font (font size &optional options)
(let* ((fontstr (if options
(format "%s-%s:%s" font size options)

View File

@ -739,6 +739,7 @@ Some elements can be dynamically toggled:
<kbd>SPC t m m</kbd> | toggle the minor mode lighters
<kbd>SPC t m b</kbd> | toggle the battery status
<kbd>SPC t m f</kbd> | toggle the flycheck info
<kbd>SPC t m v</kbd> | toggle the new version lighter
### Flycheck integration

BIN
doc/img/update-green.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
doc/img/update-orange.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

BIN
doc/img/update-red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -157,6 +157,8 @@ changed to THEME."
(spacemacs/set-state-faces))
(if (fboundp 'spacemacs/set-flycheck-mode-line-faces)
(spacemacs/set-flycheck-mode-line-faces))
(if (fboundp 'spacemacs/set-new-version-lighter-mode-line-faces)
(spacemacs/set-new-version-lighter-mode-line-faces))
(if (fboundp 'powerline-reset)
(powerline-reset)))

View File

@ -1656,6 +1656,19 @@ determine the state to enable when escaping from the insert state.")
(if dotspacemacs-mode-line-unicode-symbols " " "") 'face face)
(unless dotspacemacs-mode-line-unicode-symbols "|"))))
(defpowerline spacemacs-powerline-new-version
(propertize
spacemacs-version-check-lighter
'mouse-face 'mode-line-highlight
'help-echo (format "New version %s | Click with mouse-1 to update (Not Yet Implemented)"
spacemacs-new-version)
'local-map (let ((map (make-sparse-keymap)))
(define-key map
[mode-line down-mouse-1]
(lambda (event) (interactive "@e") (message "TODO: update"))
)
map)))
(defvar spacemacs-mode-line-minor-modesp t
"If not nil, minor modes lighter are displayed in the mode-line.")
(defun spacemacs/mode-line-minor-modes-toggle ()
@ -1666,6 +1679,16 @@ determine the state to enable when escaping from the insert state.")
(setq spacemacs-mode-line-minor-modesp t)))
(evil-leader/set-key "tmm" 'spacemacs/mode-line-minor-modes-toggle)
(defvar spacemacs-mode-line-new-version-lighterp t
"If not nil, new version lighter is displayed in the mode-line.")
(defun spacemacs/mode-line-new-version-lighter-toggle ()
"Toggle display of new version lighter."
(interactive)
(if spacemacs-mode-line-new-version-lighterp
(setq spacemacs-mode-line-new-version-lighterp nil)
(setq spacemacs-mode-line-new-version-lighterp t)))
(evil-leader/set-key "tmv" 'spacemacs/mode-line-new-version-lighter-toggle)
;; for now we hardcode the height value of powerline depending on the
;; window system, a better solution would be to compute it correctly
;; in powerline package.
@ -1770,8 +1793,16 @@ determine the state to enable when escaping from the insert state.")
(powerline-raw " " line-face))
(list
(powerline-raw global-mode-string)
(powerline-raw " " line-face))
(unless (equal '("") global-mode-string)
(powerline-raw global-mode-string)
(powerline-raw " " line-face))
;; new version
(if (and active
spacemacs-new-version
spacemacs-mode-line-new-version-lighterp)
(spacemacs-powerline-new-version
(spacemacs/get-new-version-lighter-face
spacemacs-version spacemacs-new-version) 'r)))
(list
;; percentage in the file