Refactor: rename doom-version-parser to doom-modeline-env.

This commit is contained in:
Vincent Zhang 2019-01-14 00:48:54 +08:00
parent 3f5d13fc4c
commit ea93d636f9
6 changed files with 59 additions and 56 deletions

View file

@ -19,7 +19,7 @@ The `doom-modeline` was designed for minimalism, and offers:
- A match count panel (for `anzu`, `iedit`, `multiple-cursors`, `symbol-overlay`, - A match count panel (for `anzu`, `iedit`, `multiple-cursors`, `symbol-overlay`,
`evil-search` and `evil-substitute`) `evil-search` and `evil-substitute`)
- An indicator for recording a macro - An indicator for recording a macro
- Local `python`/`ruby`/`perl`/`go`/`elixir` version in the major-mode - Current environment version (e.g. `python`, `ruby`, `go`, etc.) in the major-mode
- A customizable mode-line height (see `doom-modeline-height`) - A customizable mode-line height (see `doom-modeline-height`)
- A minor modes segment which is compatible with `minions` - A minor modes segment which is compatible with `minions`
- An error/warning count segment for `flymake`/`flycheck` - An error/warning count segment for `flymake`/`flycheck`

View file

@ -1,4 +1,4 @@
;;; doom-version-parser.el --- A version parser for doom-modeline -*- lexical-binding: t -*- ;;; doom-modeline-env.el --- A environment parser for doom-modeline -*- lexical-binding: t -*-
;; Copyright (C) 2019 Justin Barclay, Vincent Zhang ;; Copyright (C) 2019 Justin Barclay, Vincent Zhang
@ -29,19 +29,19 @@
(require 'subr-x) (require 'subr-x)
(defun doom-version-parser--ruby (line) (defun doom-modeline-env--ruby (line)
"Parse Ruby version from LINE." "Parse Ruby version from LINE."
(car (split-string (car (split-string
(cadr (cadr
(split-string line)) (split-string line))
"p"))) "p")))
(defun doom-version-parser--elixir (line) (defun doom-modeline-env--elixir (line)
"Parse Elixir version from LINE." "Parse Elixir version from LINE."
(cadr (cadr
(split-string line))) (split-string line)))
(defun doom-version-parser--rustc (line) (defun doom-modeline-env--rustc (line)
"Parse Rust version from LINE." "Parse Rust version from LINE."
(car (car
(split-string (split-string
@ -49,7 +49,7 @@
(split-string line)) (split-string line))
"-"))) "-")))
(defun doom-version-parser--go (line) (defun doom-modeline-env--go (line)
"Parse Go version from LINE." "Parse Go version from LINE."
(cadr (cadr
(split-string (split-string
@ -59,7 +59,7 @@
line))) line)))
"go"))) "go")))
(defun doom-version-parser--perl (line) (defun doom-modeline-env--perl (line)
"Parse Perl version from LINE." "Parse Perl version from LINE."
(cadr (cadr
(split-string (split-string
@ -70,17 +70,17 @@
")")) ")"))
"v"))) "v")))
(defun doom-version-parser--python (line) (defun doom-modeline-env--python (line)
"Parse Python version from LINE." "Parse Python version from LINE."
(cadr (cadr
(split-string line))) (split-string line)))
(defun doom-version-parser--get (prog args callback) (defun doom-modeline-env--get (prog args callback)
"Start a sub process using PROG and apply the ARGS to the sub process. "Start a sub process using PROG and apply the ARGS to the sub process.
Once it recieves information from STDOUT, it closes off the subprocess and Once it recieves information from STDOUT, it closes off the subprocess and
passes on the information into the CALLBACK. passes on the information into the CALLBACK.
Example: Example:
(doom-version-parser--get (doom-modeline-env--get
\"ruby\" \"ruby\"
'(\"version\") '(\"version\")
(lambda (line) (lambda (line)
@ -101,6 +101,6 @@ Example:
(funcall parser line))) (funcall parser line)))
nil)) nil))
(provide 'doom-version-parser) (provide 'doom-modeline-env)
;;; doom-version-parser.el ends here ;;; doom-modeline-env.el ends here

View file

@ -38,7 +38,7 @@
;; - A match count panel (for anzu, iedit, multiple-cursors, symbol-overlay, ;; - A match count panel (for anzu, iedit, multiple-cursors, symbol-overlay,
;; evil-search and evil-substitute) ;; evil-search and evil-substitute)
;; - An indicator for recording a macro ;; - An indicator for recording a macro
;; - Local python/ruby version in the major-mode ;; - Current environment version (e.g. python, ruby, go, etc.) in the major-mode
;; - A customizable mode-line height (see doom-modeline-height) ;; - A customizable mode-line height (see doom-modeline-height)
;; - A minor modes segment which is compatible with minions ;; - A minor modes segment which is compatible with minions
;; - An error/warning count segment for flymake/flycheck ;; - An error/warning count segment for flymake/flycheck
@ -77,9 +77,9 @@
(require 'eldoc-eval) (require 'eldoc-eval)
(require 'shrink-path) (require 'shrink-path)
(require 'subr-x) (require 'subr-x)
(require 'doom-version-parser)
(when (>= emacs-major-version 26) (when (>= emacs-major-version 26)
(require 'project)) (require 'project))
(require 'doom-modeline-env)
;; ;;
@ -544,7 +544,7 @@ If DEFAULT is non-nil, set the default mode-line for all buffers."
(defvar-local doom-modeline-env-command nil "A program that we're looking to extract version information from. Ex: \"ruby\"") (defvar-local doom-modeline-env-command nil "A program that we're looking to extract version information from. Ex: \"ruby\"")
(defvar-local doom-modeline-env-command-args nil "A list of arguments to pass to `doom-modeline-env-command` to extract the version from. Ex: '(\"--version\") ") (defvar-local doom-modeline-env-command-args nil "A list of arguments to pass to `doom-modeline-env-command` to extract the version from. Ex: '(\"--version\") ")
(defvar-local doom-modeline-env-parser nil "A function that returns version number from a programs --version (or similar) command. Ex: 'doom-version-parser--ruby") (defvar-local doom-modeline-env-parser nil "A function that returns version number from a programs --version (or similar) command. Ex: 'doom-modeline-env--ruby")
(add-hook 'find-file-hook #'doom-modeline-update-env) (add-hook 'find-file-hook #'doom-modeline-update-env)
(with-no-warnings (with-no-warnings
(if (boundp 'after-focus-change-function) (if (boundp 'after-focus-change-function)
@ -561,7 +561,7 @@ If DEFAULT is non-nil, set the default mode-line for all buffers."
doom-modeline-env-command-args doom-modeline-env-command-args
doom-modeline-env-parser) doom-modeline-env-parser)
(let ((default-directory (doom-modeline-project-root))) (let ((default-directory (doom-modeline-project-root)))
(doom-version-parser--get doom-modeline-env-command (doom-modeline-env--get doom-modeline-env-command
doom-modeline-env-command-args doom-modeline-env-command-args
(lambda (prog-version) (lambda (prog-version)
(setq doom-modeline-env-version (funcall doom-modeline-env-parser prog-version))))))) (setq doom-modeline-env-version (funcall doom-modeline-env-parser prog-version)))))))
@ -1579,6 +1579,19 @@ Returns \"\" to not break --no-window-system."
doom-modeline--bar-inactive) doom-modeline--bar-inactive)
"")) ""))
(defun doom-modeline-refresh-bars (&optional width height)
"Refresh mode-line bars with `WIDTH' and `HEIGHT'."
(setq doom-modeline--bar-active
(doom-modeline--make-xpm 'doom-modeline-bar
(or width doom-modeline-bar-width)
(max (or height doom-modeline-height)
(frame-char-height)))
doom-modeline--bar-inactive
(doom-modeline--make-xpm 'doom-modeline-inactive-bar
(or width doom-modeline-bar-width)
(max (or height doom-modeline-height)
(frame-char-height)))))
(when (>= emacs-major-version 26) (when (>= emacs-major-version 26)
(add-variable-watcher (add-variable-watcher
'doom-modeline-height 'doom-modeline-height
@ -2031,19 +2044,6 @@ mouse-1: Toggle Debug on Quit"
;; Hooks ;; Hooks
;; ;;
(defun doom-modeline-refresh-bars (&optional width height)
"Refresh mode-line bars with `WIDTH' and `HEIGHT'."
(setq doom-modeline--bar-active
(doom-modeline--make-xpm 'doom-modeline-bar
(or width doom-modeline-bar-width)
(max (or height doom-modeline-height)
(frame-char-height)))
doom-modeline--bar-inactive
(doom-modeline--make-xpm 'doom-modeline-inactive-bar
(or width doom-modeline-bar-width)
(max (or height doom-modeline-height)
(frame-char-height)))))
;;;###autoload ;;;###autoload
(defun doom-modeline-init () (defun doom-modeline-init ()
"Initialize doom mode-line." "Initialize doom mode-line."
@ -2096,40 +2096,40 @@ mouse-1: Toggle Debug on Quit"
(add-hook 'python-mode-hook (add-hook 'python-mode-hook
(lambda () (lambda ()
(cond ((and (fboundp 'pipenv-project-p) (pipenv-project-p) (executable-find "pipenv")) (cond ((and (fboundp 'pipenv-project-p) (pipenv-project-p) (executable-find "pipenv"))
(setq doom-modeline-env-parser 'doom-version-parser--python) (setq doom-modeline-env-parser 'doom-modeline-env--python)
(setq doom-modeline-env-command "pipenv") (setq doom-modeline-env-command "pipenv")
(setq doom-modeline-env-command-args '("run" "python" "--version"))) (setq doom-modeline-env-command-args '("run" "python" "--version")))
((and doom-modeline-python-executable ((and doom-modeline-python-executable
(executable-find doom-modeline-python-executable)) (executable-find doom-modeline-python-executable))
(setq doom-modeline-env-parser 'doom-version-parser--python) (setq doom-modeline-env-parser 'doom-modeline-env--python)
(setq doom-modeline-env-command doom-modeline-python-executable) (setq doom-modeline-env-command doom-modeline-python-executable)
(setq doom-modeline-env-command-args '("--version")))))) (setq doom-modeline-env-command-args '("--version"))))))
(add-hook 'ruby-mode-hook (add-hook 'ruby-mode-hook
(lambda () (lambda ()
(setq doom-modeline-env-command "ruby") (setq doom-modeline-env-command "ruby")
(setq doom-modeline-env-command-args '("--version")) (setq doom-modeline-env-command-args '("--version"))
(setq doom-modeline-env-parser 'doom-version-parser--ruby))) (setq doom-modeline-env-parser 'doom-modeline-env--ruby)))
(add-hook 'perl-mode-hook (add-hook 'perl-mode-hook
(lambda () (lambda ()
(setq doom-modeline-env-command "perl") (setq doom-modeline-env-command "perl")
(setq doom-modeline-env-command-args '("--version")) (setq doom-modeline-env-command-args '("--version"))
(setq doom-modeline-env-parser 'doom-version-parser--perl))) (setq doom-modeline-env-parser 'doom-modeline-env--perl)))
(add-hook 'go-mode-hook (add-hook 'go-mode-hook
(lambda () (lambda ()
(setq doom-modeline-env-command "go") (setq doom-modeline-env-command "go")
(setq doom-modeline-env-command-args '("version")) (setq doom-modeline-env-command-args '("version"))
(setq doom-modeline-env-parser 'doom-version-parser--go))) (setq doom-modeline-env-parser 'doom-modeline-env--go)))
(add-hook 'elixir-mode-hook (add-hook 'elixir-mode-hook
(lambda () (lambda ()
(setq doom-modeline-env-command "iex") (setq doom-modeline-env-command "iex")
(setq doom-modeline-env-command-args '("--version")) (setq doom-modeline-env-command-args '("--version"))
(setq doom-modeline-env-parser 'doom-version-parser--elixir))) (setq doom-modeline-env-parser 'doom-modeline-env--elixir)))
(add-hook 'rust-mode-hook (add-hook 'rust-mode-hook
(lambda () (lambda ()
(setq doom-modeline-env-command "rustc") (setq doom-modeline-env-command "rustc")
(setq doom-modeline-env-command-args '("--version")) (setq doom-modeline-env-command-args '("--version"))
(setq doom-modeline-env-parser 'doom-version-parser--rustc))) (setq doom-modeline-env-parser 'doom-modeline-env--rustc)))
;; Ensure modeline is inactive when Emacs is unfocused (and active otherwise) ;; Ensure modeline is inactive when Emacs is unfocused (and active otherwise)

View file

@ -1,4 +1,4 @@
;;; doom-version-parser-test.el --- Unit tests for doom-version-parser -*- lexical-binding: t; -*- ;;; doom-modeline-env-test.el --- Unit tests for doom-modeline-env -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Justin Barclay ;; Copyright (C) 2019 Justin Barclay
@ -23,33 +23,36 @@
;;; Commentary: ;;; Commentary:
;; ;;
;; Unit tests for doom-version-parser ;; Unit tests for doom-modeline-env
;; ;;
;;; Code: ;;; Code:
(ert-deftest doom-version-parser--ruby/parse-ruby-version-string ()
(require 'doom-modeline-env)
(ert-deftest doom-modeline-env--ruby/parse-ruby-version-string ()
(should (should
(string= (doom-version-parser--ruby "ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]") (string= (doom-modeline-env--ruby "ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]")
"2.3.3"))) "2.3.3")))
(ert-deftest doom-version-parser--elixir/parse-elixr-version-string () (ert-deftest doom-modeline-env--elixir/parse-elixr-version-string ()
(should (should
(string= (doom-version-parser--elixir "IEx 1.7.4 (compiled with Erlang/OTP 21)") (string= (doom-modeline-env--elixir "IEx 1.7.4 (compiled with Erlang/OTP 21)")
"1.7.4"))) "1.7.4")))
(ert-deftest doom-version-parser--rustc/parse-rustc-version-string () (ert-deftest doom-modeline-env--rustc/parse-rustc-version-string ()
(should (should
(string= (doom-version-parser--rustc "rustc 1.32.0-nightly (14997d56a 2018-12-05)") (string= (doom-modeline-env--rustc "rustc 1.32.0-nightly (14997d56a 2018-12-05)")
"1.32.0"))) "1.32.0")))
(ert-deftest doom-version-parser--go/parse-go-version-string () (ert-deftest doom-modeline-env--go/parse-go-version-string ()
(should (should
(string= (doom-version-parser--go "go version go1.11.4 darwin/amd64") (string= (doom-modeline-env--go "go version go1.11.4 darwin/amd64")
"1.11.4"))) "1.11.4")))
(ert-deftest doom-version-parser--perl/parse-perl-version-string () (ert-deftest doom-modeline-env--perl/parse-perl-version-string ()
(should (should
(string= (doom-version-parser--perl (string= (doom-modeline-env--perl
"This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level "This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail) (with 2 registered patches, see perl -V for more detail)
@ -63,9 +66,9 @@ this system using \"man perl\" or \"perldoc perl\". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.") Internet, point your browser at http://www.perl.org/, the Perl Home Page.")
"5.18.2"))) "5.18.2")))
(ert-deftest doom-version-parser--python/parse-python-version-string () (ert-deftest doom-modeline-env--python/parse-python-version-string ()
(should (should
(string= (doom-version-parser--python "Python 2.7.15") (string= (doom-modeline-env--python "Python 2.7.15")
"2.7.15"))) "2.7.15")))
;;; doom-version-parser-test.el ends here ;;; doom-modeline-env-test.el ends here

View file

@ -31,6 +31,8 @@
;;; Code: ;;; Code:
(require 'doom-modeline)
(ert-deftest doom-modeline--buffer-file-name/truncate-upto-project () (ert-deftest doom-modeline--buffer-file-name/truncate-upto-project ()
(let ((default-directory "/home/user/project/") (let ((default-directory "/home/user/project/")
(file-path "/home/user/project/relative/test.txt") (file-path "/home/user/project/relative/test.txt")

View file

@ -32,8 +32,6 @@
(add-to-list 'load-path (expand-file-name ".")) (add-to-list 'load-path (expand-file-name "."))
(require 'doom-modeline)
(defun strip-text-properties(txt) (defun strip-text-properties(txt)
"Strip text properties of TXT." "Strip text properties of TXT."
(set-text-properties 0 (length txt) nil txt) (set-text-properties 0 (length txt) nil txt)