Skip to content

Commit

Permalink
refac(org segment): make max-length calculation dynamic
Browse files Browse the repository at this point in the history
Calculation is now linked to the available space.

This removes `whale-lien-segments-org-max-heading-length`.
  • Loading branch information
Walheimat committed Sep 30, 2023
1 parent 47d0f8f commit d5a4b9d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed

- `dir-locals.el` is no longer tracked.
- Custom variable `whale-line-segments-org-max-length`.

### Changed

Expand Down
1 change: 0 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ will need to call =whale-line-rebuild=.
(whale-line-segments-org-ellispis "…")
(whale-line-segments-org-elision "*")
(whale-line-segments-org-max-count 2)
(whale-line-segments-org-max-heading-length 12)

;; Specs for used icons.
(whale-line-iconify-specs '((project . (:name "package" :font octicon :face whale-line-emphasis))
Expand Down
23 changes: 21 additions & 2 deletions test/whale-line-segments-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,30 @@
(ert-deftest org--maybe-truncate--skips ()
(should (string= "testing" (whale-line-segments--org--maybe-truncate "testing" 'success 7))))

(ert-deftest org--maybe-truncate--obeys-min ()
(bydi ((:mock whale-line--space :return -123))
(ert-deftest org--max-length--obeys-min ()
(bydi ((:mock whale-line--space :return -123)
(:mock window-font-width :return 10))
(should (eq whale-line-segments--org--min-length
(whale-line-segments--org--max-length)))))

(ert-deftest org--max-length--obeys-max ()
(let ((whale-line-segments-org-max-count 3)
(whale-line-segments--org--max-length 12)
(whale-line-segments--org--min-length 4))

(bydi ((:mock whale-line--space :return 40)
(:mock window-font-width :return 1))
(should (eq 12 (whale-line-segments--org--max-length))))))

(ert-deftest org--max-length--reduces-max ()
(let ((whale-line-segments-org-max-count 3)
(whale-line-segments--org--max-length 12)
(whale-line-segments--org--min-length 3))

(bydi ((:mock whale-line--space :return 32)
(:mock window-font-width :return 1))
(should (eq 10 (whale-line-segments--org--max-length))))))

(ert-deftest org--get-next-heading ()
(let ((org-level-faces '(red green blue orange)))

Expand Down
18 changes: 7 additions & 11 deletions whale-line-segments.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ to 2, only the 3rd level is elided."
:group 'whale-line-segments
:type 'integer)

(defcustom whale-line-segments-org-max-heading-length 12
"The max length of a heading after which it will be truncated."
:group 'whale-line-segments
:type 'integer)

;;; -- Segments

(declare-function image-mode-window-get "ext:image-mode.el")
Expand Down Expand Up @@ -479,6 +474,7 @@ Returns nil if not checking or if no errors were found."
(declare-function org-up-heading-safe "ext:org.el")

(defvar wls--org--min-length 4)
(defvar wls--org--max-length 24)

(defun wls--org--maybe-truncate (heading face max-len)
"Maybe truncate HEADING depending on MAX-LEN.
Expand All @@ -503,13 +499,13 @@ Use FACE for the ellipsis glyph."

(defun wls--org--max-length ()
"Check if we're in a low space environment."
(let ((space (whale-line--space)))
(let* ((space (max 1 (whale-line--space)))
(width (/ space (window-font-width))))

(if (> space 0)
(max wls--org--min-length
(/ wls-org-max-heading-length
(max (/ 300 space) 1)))
wls--org--min-length)))
(if (> width (* wls-org-max-count wls--org--max-length))
wls--org--max-length
(min wls--org--max-length
(max wls--org--min-length (/ width wls-org-max-count))))))

(defun wls--org--collect-headings ()
"Collect headings until it's no longer safe."
Expand Down

0 comments on commit d5a4b9d

Please sign in to comment.