From d5a4b9d6a631b8d58c0c17e4ae16258f3e0ee06b Mon Sep 17 00:00:00 2001 From: Walheimat Date: Sat, 30 Sep 2023 14:00:53 +0200 Subject: [PATCH] refac(org segment): make max-length calculation dynamic Calculation is now linked to the available space. This removes `whale-lien-segments-org-max-heading-length`. --- CHANGELOG.md | 1 + README.org | 1 - test/whale-line-segments-test.el | 23 +++++++++++++++++++++-- whale-line-segments.el | 18 +++++++----------- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c01398..16906c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.org b/README.org index 1124966..bb845d3 100644 --- a/README.org +++ b/README.org @@ -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)) diff --git a/test/whale-line-segments-test.el b/test/whale-line-segments-test.el index 4df0d2f..bf6140b 100644 --- a/test/whale-line-segments-test.el +++ b/test/whale-line-segments-test.el @@ -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))) diff --git a/whale-line-segments.el b/whale-line-segments.el index c7039cd..9c9815b 100644 --- a/whale-line-segments.el +++ b/whale-line-segments.el @@ -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") @@ -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. @@ -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."