Skip to content

Commit

Permalink
feat(segments): buffer-identification now prepends path
Browse files Browse the repository at this point in the history
Adds new custom variable
`whale-line-segments-buffer-identification-path-segments` and sets it
to 1. This is the number of preceding path segments that are included
in the segment.

If the old behavior is desired, this can be set to 0.
  • Loading branch information
Walheimat committed Mar 10, 2024
1 parent b63abf6 commit e100675
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ the change.
])
(whale-line-segments-animation-speed 0.4)

;; Buffer identification.
(whale-line-segments-buffer-identification-path-segments 1)

;; Org.
(whale-line-segments-org-separator ">")
(whale-line-segments-org-ellispis "…")
Expand Down
25 changes: 23 additions & 2 deletions test/whale-line-segments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,38 @@
(bydi-was-called-with run-hooks 'whale-line-segments-buffer-identification-hook))))

(ert-deftest buffer-identification ()
(should (equal '((:propertize (:eval (propertized-buffer-identification "%b"))
(should (equal '((:propertize (:eval (whale-line-segments--buffer-identification--path-segments))
face whale-line-shadow)
(:propertize (:eval (propertized-buffer-identification "%b"))
face (mode-line-buffer-id nil)))
(whale-line-segments--buffer-identification)))

(let ((whale-line-segments--buffer-identification--additional-help "help"))

(should (equal '((:propertize (:eval (propertized-buffer-identification "%b"))
(should (equal '((:propertize (:eval (whale-line-segments--buffer-identification--path-segments))
face whale-line-shadow)
(:propertize (:eval (propertized-buffer-identification "%b"))
face (mode-line-buffer-id nil)
help-echo "help"))
(whale-line-segments--buffer-identification)))))

(ert-deftest buffer-identification--path-segments ()
:tags '(segments buffer)

(bydi ((:mock buffer-file-name :return "/test/one/two/three.el"))

(let ((whale-line-segments-buffer-identification-path-segments 1))

(should (string= "two/" (whale-line-segments--buffer-identification--path-segments)))

(setq whale-line-segments-buffer-identification-path-segments 12)

(should (string= "/test/one/two/" (whale-line-segments--buffer-identification--path-segments)))

(setq whale-line-segments-buffer-identification-path-segments 0)

(should-not (whale-line-segments--buffer-identification--path-segments)))))

(ert-deftest buffer-status--using-icons ()
(let ((modified nil)
(buffer-file-name "some-name"))
Expand Down
26 changes: 24 additions & 2 deletions whale-line-segments.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ to 2, only the 3rd level is elided."
:group 'whale-line-segments
:type 'integer)

(defcustom whale-line-segments-buffer-identification-path-segments 1
"The amount of path elements to show before buffer identification.
This can be disabled by setting this to 0."
:group 'whale-line-segments
:type 'integer)

;;;; Utility

(defun whale-line-segments--decorate (_symbol &rest _args)
Expand Down Expand Up @@ -99,12 +106,27 @@ See `whale-line-iconify' for a way to do this."
(run-hooks 'whale-line-segments-buffer-identification-hook))

(defun whale-line-segments--buffer-identification ()
"Get the buffer identification."
`((:propertize (:eval (propertized-buffer-identification "%b"))
"Get the buffer identification.
This pre-pends the path to the buffer if so configured."
`((:propertize (:eval (whale-line-segments--buffer-identification--path-segments))
face whale-line-shadow)
(:propertize (:eval (propertized-buffer-identification "%b"))
face ,(list 'mode-line-buffer-id whale-line-segments--buffer-identification--additional-face)
,@(and whale-line-segments--buffer-identification--additional-help
(list 'help-echo whale-line-segments--buffer-identification--additional-help)))))

(defun whale-line-segments--buffer-identification--path-segments ()
"Get preceding path segments."
(when (> whale-line-segments-buffer-identification-path-segments 0)

(let* ((file (buffer-file-name (current-buffer)))
(path (file-name-split file))
(count (min (1- (length path)) whale-line-segments-buffer-identification-path-segments))
(segments (seq-take (cdr (reverse path)) count)))

(concat (string-join (reverse segments) "/") "/"))))

(whale-line-create-stateful-segment buffer-identification
:getter whale-line-segments--buffer-identification
:hooks (find-file-hook after-save-hook clone-indirect-buffer-hook kill-buffer-hook whale-line-segments-buffer-identification-hook)
Expand Down

0 comments on commit e100675

Please sign in to comment.