Skip to content

Commit

Permalink
feat(segments, iconify): use iconify as default decorator
Browse files Browse the repository at this point in the history
Instead of using `whale-line-iconify` in segments, the function in
`whale-line-segments-decorate` is used, defaulting to the prior. This
decouples segments from icons.

This also means that `iconify` no longer provides fallbacks, instead,
the segments need to take care of this.
  • Loading branch information
Walheimat committed Dec 2, 2023
1 parent 8b533fe commit 96aed58
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 121 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
edit buffer segment positioning (and inclusion) in a separate
buffer. The resulting edit can be applied or persisted (by saving
the customization). The user may also revert or abort. The changes
are validated before they are applied.
are validated before they are applied.
- `whale-line-iconify` is now set as the default value for new custom
variable `whale-line-segments-decorator`, decoupling
`whale-line-segments` from it.

### Changed

Expand All @@ -25,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Package `whale-line-core` was folded into `whale-line`. Package
`whale-line-segments` is now required during
`whale-line--build-segments`.
- `whale-line-iconify` no longer defaults, it's the responsibility of
the segment.

## [v0.8.0]

Expand Down
31 changes: 23 additions & 8 deletions test/whale-line-iconify-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@

(defvar rectangle (ert-resource-file "rectangle.txt"))

(defvar test-specs '((test . (:name "test" :font test :face test-face :fallback "?"))
(no-def . (:name "test" :font test :face test-face :fallback "?" :no-defaults t))
(merge . (:name "test" :font test :face test-face :fallback "?" :height 0.5))))
(defvar test-specs '((test . (:name "test" :font test :face test-face))
(no-def . (:name "test" :font test :face test-face :no-defaults t))
(merge . (:name "test" :font test :face test-face :height 0.5))
(fun . (:function specs-fun))
(args . (:function specs-fun :pass-args t :height 0.5))))

(defun specs-fun (&rest _args)
"Just a specs fun."
nil)

(ert-deftest from-specs ()
(let ((whale-line-iconify-specs test-specs)
(whale-line-iconify--default-specs '(:height 0.8 :v-adjust -1.1)))

(bydi (all-the-icons-test)
(bydi (all-the-icons-test
specs-fun)

(whale-line-iconify--from-specs (cdr-safe (assoc 'test whale-line-iconify-specs)))
(bydi-was-called-with all-the-icons-test '("test" :face test-face :height 0.8 :v-adjust -1.1))
Expand All @@ -29,7 +36,15 @@

(bydi-clear-mocks)
(whale-line-iconify--from-specs (cdr-safe (assoc 'merge whale-line-iconify-specs)))
(bydi-was-called-with all-the-icons-test '("test" :face test-face :height 0.5 :v-adjust -1.1)))))
(bydi-was-called-with all-the-icons-test '("test" :face test-face :height 0.5 :v-adjust -1.1))

(whale-line-iconify--from-specs (cdr-safe (assoc 'fun whale-line-iconify-specs)))

(bydi-was-called specs-fun t)

(whale-line-iconify--from-specs (cdr-safe (assoc 'args whale-line-iconify-specs)))

(bydi-was-called-with specs-fun '(:height 0.5)))))

(ert-deftest use-icons-for-p ()
(let ((whale-line-iconify-disabled nil))
Expand Down Expand Up @@ -79,17 +94,17 @@
(whale-line-iconify 'test)

(bydi-was-called-with whale-line-iconify--from-specs
'((:name "test" :font test :face test-face :fallback "?")))
'((:name "test" :font test :face test-face)))

(bydi-clear-mocks)
(whale-line-iconify 'test 'other-face)

(bydi-was-called-with whale-line-iconify--from-specs
'((:name "test" :font test :face other-face :fallback "?")))
'((:name "test" :font test :face other-face)))

(bydi-toggle-sometimes)

(should (string= "?" (whale-line-iconify 'test)))
(should-not (whale-line-iconify 'test))

(bydi-was-not-called whale-line-iconify--from-specs))))

Expand Down
50 changes: 24 additions & 26 deletions test/whale-line-segments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
(whale-line-segments--buffer-identification)))))

(ert-deftest buffer-status--dense-p ()
(bydi ((:always whale-line-iconify--can-use-p))
(bydi ((:othertimes whale-line-segments--decorate))

(let ((whale-line-iconify-disabled '(buffer-status)))
(should (whale-line-segments--buffer-status--dense-p))

(should (whale-line-segments--buffer-status--dense-p)))
(bydi-toggle-sometimes)

(ert-with-temp-file status
:buffer b
Expand All @@ -56,8 +56,8 @@
(let ((modified nil)
(buffer-file-name "some-name"))

(bydi ((:always whale-line-iconify--use-for-p)
(:mock whale-line-iconify :return "!")
(bydi ((:always whale-line-segments--decorate)
(:mock whale-line-segments--decorate :return "!")
(:mock buffer-modified-p :return modified))

(should-not (whale-line-segments--buffer-status--writable))
Expand All @@ -69,35 +69,34 @@
(setq modified t)

(should (string= "!" (whale-line-segments--buffer-status--writable)))
(bydi-was-called-last-with whale-line-iconify '(buffer-modified whale-line-shadow))
(bydi-was-called-last-with whale-line-segments--decorate '(buffer-modified whale-line-shadow))

(setq buffer-file-name "some-name")
(should (string= "!" (whale-line-segments--buffer-status--writable)))
(bydi-was-called-last-with whale-line-iconify '(buffer-modified whale-line-emphasis)))))
(bydi-was-called-last-with whale-line-segments--decorate '(buffer-modified whale-line-emphasis)))))

(ert-deftest buffer-status--not-using-icons ()
(let ((modified nil)
(buffer-file-name "some-name"))

(bydi ((:ignore whale-line-iconify--use-for-p)
(:mock whale-line-iconify :return "!")
(bydi ((:ignore whale-line-segments--decorate)
(:mock buffer-modified-p :return modified))

(should (string= "" (whale-line-segments--buffer-status--writable)))

(setq buffer-file-name nil)

(should (string= "!" (whale-line-segments--buffer-status--writable)))
(should (string= "&" (whale-line-segments--buffer-status--writable)))

(setq modified t)

(should (string= "!!" (whale-line-segments--buffer-status--writable))))))
(should (string= "&*" (whale-line-segments--buffer-status--writable))))))

(ert-deftest buffer-status--read-only ()
(bydi (whale-line-iconify)
(bydi (whale-line-segments--decorate)
(whale-line-segments--buffer-status--read-only)

(bydi-was-called-with whale-line-iconify 'buffer-read-only)))
(bydi-was-called-with whale-line-segments--decorate 'buffer-read-only)))

;;; -- Window status

Expand All @@ -106,15 +105,15 @@

(set-window-dedicated-p (selected-window) t)

(should (string= "^" (whale-line-segments--window-status)))
(should (equal '("^") (whale-line-segments--window-status)))

(set-window-parameter (selected-window) 'no-other-window t)

(should (string= "~ ^" (whale-line-segments--window-status)))
(should (equal '("~" "^") (whale-line-segments--window-status)))

(set-window-dedicated-p (selected-window) nil)

(should (string= "~" (whale-line-segments--window-status)))
(should (equal '("~") (whale-line-segments--window-status)))

(set-window-parameter (selected-window) 'no-other-window nil))

Expand Down Expand Up @@ -248,23 +247,22 @@
;;; -- Major mode

(ert-deftest major-mode--icon ()
(bydi ((:always whale-line-iconify--use-for-p)
(:mock all-the-icons-icon-for-buffer :return "?")
(bydi ((:mock whale-line-segments--decorate :return "?")
(:mock format-mode-line :return "echo"))

(should (equal '((:propertize "?" help-echo "echo" display (raise -0.135)))
(whale-line-segments--major-mode--icon)))))
(whale-line-segments--major-mode--decorated)))))

(ert-deftest major-mode--text ()
(should (equal '((:propertize (" " mode-name " ") face whale-line-highlight))
(whale-line-segments--major-mode--text))))

(ert-deftest major-mode ()
(bydi ((:sometimes whale-line-segments--major-mode--icon)
(bydi ((:sometimes whale-line-segments--major-mode--decorated)
(:always whale-line-segments--major-mode--text))

(should (whale-line-segments--major-mode))
(bydi-was-called whale-line-segments--major-mode--icon)
(bydi-was-called whale-line-segments--major-mode--decorated)
(bydi-was-not-called whale-line-segments--major-mode--text)
(bydi-toggle-sometimes)
(whale-line-segments--major-mode)
Expand Down Expand Up @@ -415,7 +413,7 @@
(should (string= "Connected to test::testing" (whale-line-segments--lsp--help))))))

(ert-deftest lsp--with-count ()
(bydi ((:mock whale-line-iconify :return "LSP")
(bydi ((:mock whale-line-segments--decorate :return "LSP")
(:sometimes whale-line-segments--lsp--uses-lsp-mode-p)
(:mock lsp-workspaces :return '(a b c)))

Expand Down Expand Up @@ -456,7 +454,7 @@
(should-not (whale-line-segments--debug--active-p))))))

(ert-deftest dap-segment ()
(bydi ((:mock whale-line-iconify :return "*")
(bydi ((:mock whale-line-segments--decorate :return "*")
(:always whale-line-segments--debug--active-p)
dap--cur-session
(:mock dap--debug-session-name :return "Test"))
Expand Down Expand Up @@ -595,7 +593,7 @@
(bydi ((:always whale-line-segments--project--display-for-buffer-p)
(:always project-current)
(:mock whale-line-segments--project--help :return "help")
(:mock whale-line-iconify :return "*")
(:mock whale-line-segments--decorate :return "*")
(:mock project-root :return "/home/test/project/")
(:mock project-name :return "project"))

Expand Down Expand Up @@ -634,7 +632,7 @@
(bydi ((:always buffer-file-name)
(:ignore whale-line-segments--vc-registered--info)
(:mock whale-line-segments--vc-unregistered--info :return "test")
(:mock whale-line-iconify :return "*"))
(:mock whale-line-segments--decorate :return "*"))

(should (equal '("*" " " "test")
(whale-line-segments--vc)))))
Expand All @@ -650,7 +648,7 @@

(bydi ((:mock vc-backend :return "none")
(:mock vc-state :return 'testing)
(:mock whale-line-iconify :return "*")
(:mock whale-line-segments--decorate :return "*")
(:mock whale-line-segments--vc--face-for-state :return 'test-face))

(should (equal '(:propertize "tests" mouse-face whale-line-highlight face test-face)
Expand Down
59 changes: 28 additions & 31 deletions whale-line-iconify.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ This has no effect if icons cannot be enabled. See
(defcustom whale-line-iconify-specs
'((project . (:name "package" :font octicon :face whale-line-emphasis))
(vc . (:name "code-fork" :face whale-line-contrast))
(buffer-read-only . (:name "lock" :face whale-line-contrast :fallback "@" :parent buffer-status))
(buffer-file-name . (:name "sticky-note-o" :face whale-line-shadow :fallback "&" :parent buffer-status))
(buffer-modified . (:name "pencil" :face whale-line-emphasis :fallback "*" :parent buffer-status))
(window-dedicated . (:name "link" :face whale-line-shadow :fallback "^" :parent window-status))
(window-no-other . (:name "low-vision" :face whale-line-shadow :fallback "~" :parent window-status))
(major-mode . (:function all-the-icons-icon-for-buffer))
(buffer-read-only . (:name "lock" :face whale-line-contrast :parent buffer-status))
(buffer-file-name . (:name "sticky-note-o" :face whale-line-shadow :parent buffer-status))
(buffer-modified . (:name "pencil" :face whale-line-emphasis :parent buffer-status))
(window-dedicated . (:name "link" :face whale-line-shadow :parent window-status))
(window-no-other . (:name "low-vision" :face whale-line-shadow :parent window-status))
(buffer-fallback . (:name "question-circle" :face whale-line-contrast :no-defaults t))
(lsp . (:name "plug" :face whale-line-contrast :fallback "LSP"))
(debug . (:name "bug" :face whale-line-urgent :fallback "BUG"))
(partial-recall . (:name "tag" :face whale-line-contrast :fallback "PR")))
(lsp . (:name "plug" :face whale-line-contrast))
(debug . (:name "bug" :face whale-line-urgent)))
"Icon specifications.
This is an alist of (ICON-SYMBOL . SPECS). The ICON-SYMBOL is the
symbol passed to `whale-line-iconify' to retrieve the associated
specs. SPECS is a plist with the following keys.
FUNCTION is a function to use instead of constructing it using
NAME. If PASS-ARGS is t, all icon specs are passed to the
function.
NAME is the name of the icon in the font. The remaining keys are
optional.
Expand All @@ -63,9 +67,6 @@ FACE is the face to use for the icon.
FONT is a symbol, namely one of the possible suffixes of
`all-the-icons-insert-*'. It defaults to `faicon'.
FALLBACK is a string to use when the icon can't be used or is
disabled.
PARENT is a symbol of another icon. This allows disabling an icon
through its parent.
Expand All @@ -86,18 +87,21 @@ All remaining keys and their values will be passed to

(defun whale-line-iconify--from-specs (specs)
"Get icon from SPECS."
(let* ((font (or (plist-get specs :font) 'faicon))
(fun (intern (concat "all-the-icons-" (symbol-name font))))
(icon (plist-get specs :name))
(remainder (whale-line-iconify--pure-specs specs))
(defaults (whale-line-iconify--default-specs specs remainder)))
(if-let* ((fun (plist-get specs :function)))
(apply fun (when (plist-get specs :pass-args)
(whale-line-iconify--pure-specs specs)))
(let* ((font (or (plist-get specs :font) 'faicon))
(fun (intern (concat "all-the-icons-" (symbol-name font))))
(icon (plist-get specs :name))
(remainder (whale-line-iconify--pure-specs specs))
(defaults (whale-line-iconify--default-specs specs remainder)))

(apply fun (append (list icon) remainder defaults))))
(apply fun (append (list icon) remainder defaults)))))

(defun whale-line-iconify--pure-specs (specs)
"Return icon only specs from SPECS."
(cl-loop for (key . val) in (cl--plist-to-alist specs)
unless (memq key '(:name :fallback :font :no-defaults :parent))
unless (memq key '(:name :function :font :no-defaults :parent :pass-args))
nconc (list key val)))

(defun whale-line-iconify--default-specs (specs existing)
Expand Down Expand Up @@ -129,20 +133,13 @@ Only properties not in EXISTING are added."
"Get the icon for NAME.
If optional argument FACE is passed, it will be used instead of
the value in the retrieved specs.
If icon can't or shouldn't be displayed, any existing fallback is
returned."
(if-let* ((specs (copy-tree (cdr-safe (assoc name whale-line-iconify-specs))))
(specs (if face (plist-put specs :face face) specs))
(parent (or (plist-get specs :parent) name))
((whale-line-iconify--use-for-p parent)))

(whale-line-iconify--from-specs specs)

(when-let ((fallback (plist-get specs :fallback)))
the value in the retrieved specs."
(when-let* ((specs (copy-tree (cdr-safe (assoc name whale-line-iconify-specs))))
(specs (if face (plist-put specs :face face) specs))
(parent (or (plist-get specs :parent) name))
((whale-line-iconify--use-for-p parent)))

(propertize fallback 'face (plist-get specs :face)))))
(whale-line-iconify--from-specs specs)))

(provide 'whale-line-iconify)

Expand Down
Loading

0 comments on commit 96aed58

Please sign in to comment.