Skip to content

Commit

Permalink
Truncate long lines (#25)
Browse files Browse the repository at this point in the history
* feat: add option sideline-truncate

* simplify and add option sideline-truncate-min-available-space-ratio

* sideline-truncate defaults to nil

* Add sideline-truncate-suffix
  • Loading branch information
Azkae authored Nov 3, 2024
1 parent 0994d4d commit 4287929
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions sideline.el
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
:type 'boolean
:group 'sideline)

(defcustom sideline-truncate nil
"Truncate sideline if the line width are wider than the window width."
:type 'boolean
:group 'sideline)

(defcustom sideline-truncate-min-available-space-ratio 0.5
"Minimum available space to allow truncation."
:type 'number
:group 'sideline)

(defcustom sideline-truncate-suffix "..."
"Truncation suffix."
:type 'string
:group 'sideline)

(defface sideline-default
'((((background light)) :foreground "DarkOrange")
(t :foreground "yellow"))
Expand Down Expand Up @@ -405,7 +420,7 @@ calculate to the right side."
(setq str-len (+ str-len opposing-str-len))
;; Start the calculation!
(when-let* ((win-width (sideline--render-data :win-width))
((or sideline-force-display-if-exceeds
((or sideline-force-display-if-exceeds sideline-truncate
(<= str-len win-width)))
(column-start (sideline--render-data :hscroll))
(pos-end (max (sideline--line-width) column-start)))
Expand All @@ -419,7 +434,10 @@ calculate to the right side."
(t
(let ((column-end (+ column-start win-width)))
(cond ((or sideline-force-display-if-exceeds
(<= str-len (- column-end pos-end)))
(<= str-len (- column-end pos-end))
(and sideline-truncate
(< (* win-width sideline-truncate-min-available-space-ratio)
(- column-end pos-end))))
(cons column-end pos-end))))))))

(defun sideline--find-line (str-len on-left &optional direction exceeded)
Expand Down Expand Up @@ -584,6 +602,20 @@ FACE, NAME, ON-LEFT, and ORDER for details."
(space :width 0))
`cursor t))
title)))

(when sideline-truncate
(let* ((win-width (sideline--render-data :win-width))
(used-space (- pos-start occ-pt))
(available-space (1+ (- win-width used-space)))
(suffix nil))
(when (and sideline-truncate-suffix
(> available-space (length sideline-truncate-suffix)))
(setq suffix (copy-sequence sideline-truncate-suffix))
(set-text-properties 0 (length suffix)
(text-properties-at (1- (length str)) str)
suffix))
(setq str (truncate-string-to-width str available-space 0 nil suffix))))

;; Create overlay
(let* ((len-str (length str))
(empty-ln (= pos-start pos-end))
Expand Down

0 comments on commit 4287929

Please sign in to comment.