From 0b3ddd8304a12b7666809e249ead161ebe99161f Mon Sep 17 00:00:00 2001 From: Walheimat Date: Mon, 14 Oct 2024 18:55:45 +0200 Subject: [PATCH] improve(segments,project): customizable width before truncation Adds new custom variable `whale-line-segments-project-width` that defaults to 10. If the name of the project exceeds this numer, it is truncated using `truncate-string-to-width`. The old behavior can be restored by setting this to `nil`. --- test/whale-line-segments-test.el | 12 +++++++++++- whale-line-segments.el | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/whale-line-segments-test.el b/test/whale-line-segments-test.el index c130a70..20668df 100644 --- a/test/whale-line-segments-test.el +++ b/test/whale-line-segments-test.el @@ -685,7 +685,8 @@ (should (string= "Project (/home/test/project/)\nmouse-1: Open root" (whale-line-segments--project--help))))) (ert-deftest project--get--for-project () - (let ((whale-line-segments--project--map nil)) + (let ((whale-line-segments--project--map nil) + (whale-line-segments-project-width nil)) (bydi ((:always whale-line-segments--project--display-for-buffer-p) (:always project-current) @@ -699,6 +700,15 @@ mouse-face whale-line-highlight help-echo "help" local-map nil)) + (whale-line-segments--project))) + + (setq whale-line-segments-project-width 3) + + (should (equal `("*" " " (:propertize ,(concat "pr" (truncate-string-ellipsis)) + face whale-line-emphasis + mouse-face whale-line-highlight + help-echo "help" + local-map nil)) (whale-line-segments--project)))))) (ert-deftest tab-bar () diff --git a/whale-line-segments.el b/whale-line-segments.el index 532f0e5..c8bf2ae 100644 --- a/whale-line-segments.el +++ b/whale-line-segments.el @@ -88,6 +88,14 @@ In project buffers segments are calculated relative to the project root." :group 'whale-line-segments :type 'string) +(defcustom whale-line-segments-project-width 10 + "The width of the project name. + +If this is a number, a name exceeding it will be truncated." + :group 'whale-line-segments + :type '(choice (integer :tag "The (padded) width") + (const :tag "Full name" nil))) + ;;;; Utility (defun whale-line-segments--decorate (_symbol &rest _args) @@ -845,11 +853,15 @@ Only consider Dired buffers and file buffers." (when-let* ((candidate (whale-line-segments--project--display-for-buffer-p)) (project (project-current)) (name (project-name project)) + (truncated (if (and (numberp whale-line-segments-project-width) + (> (length name) whale-line-segments-project-width)) + (truncate-string-to-width name whale-line-segments-project-width 0 nil t) + name)) (help (whale-line-segments--project--help))) `(,@(when-let ((decorated (whale-line-segments--decorate 'project))) (list decorated (whale-line--spacer))) - (:propertize ,name + (:propertize ,truncated face whale-line-emphasis mouse-face whale-line-highlight help-echo ,help