Skip to content

Commit

Permalink
fix str width
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 17, 2024
1 parent faa775f commit 4fa8190
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
emacs-version:
- 28.2
- 29.3
experimental: [false]
include:
Expand Down
2 changes: 1 addition & 1 deletion Eask
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(source 'jcs-elpa)
(source 'melpa)

(depends-on "emacs" "27.1")
(depends-on "emacs" "29.1")
(depends-on "elenv")
(depends-on "moody")
(depends-on "minions")
Expand Down
23 changes: 6 additions & 17 deletions jcs-modeline.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-emacs/jcs-modeline
;; Version: 0.1.1
;; Package-Requires: ((emacs "28.1") (moody "0.7.1") (minions "0.3.7") (elenv "0.1.0") (nerd-icons "0.0.1") (reveal-in-folder "0.1.2"))
;; Package-Requires: ((emacs "29.1") (moody "0.7.1") (minions "0.3.7") (elenv "0.1.0") (nerd-icons "0.0.1") (reveal-in-folder "0.1.2"))
;; Keywords: faces mode-line

;; This file is not part of GNU Emacs.
Expand Down Expand Up @@ -84,9 +84,6 @@
;; (@* "Externals" )
;;

(declare-function string-pixel-width "subr-x.el") ; TODO: remove this after 29.1
(declare-function shr-string-pixel-width "shr.el") ; TODO: remove this after 29.1

(defvar buffer-undo-tree)
(defvar undo-tree-visualizer-buffer-name)
(defvar undo-tree-visualizer-parent-buffer)
Expand Down Expand Up @@ -171,18 +168,10 @@
(puthash char result jcs-modeline--char-displayable-cache)
(and result str-or-char)))

;; TODO: Use function `string-pixel-width' after 29.1
(defun jcs-modeline--string-pixel-width (str)
"Return the width of STR in pixels."
(if (fboundp #'string-pixel-width)
(string-pixel-width str)
(require 'shr)
(shr-string-pixel-width str)))

(defun jcs-modeline--str-len (str)
(defun jcs-modeline--str-width (str)
"Calculate STR in pixel width."
(let ((width (frame-char-width))
(len (jcs-modeline--string-pixel-width str)))
(len (string-pixel-width str)))
(+ (/ len width)
(if (zerop (% len width)) 0 1)))) ; add one if exceeed

Expand Down Expand Up @@ -235,7 +224,7 @@ Position argument ARG0."
(let* ((item (nth (if is-left left-index right-index)
(if is-left jcs-modeline-left right-list)))
(format (format-mode-line item))
(width (jcs-modeline--str-len format))
(width (jcs-modeline--str-width format))
(new-width (+ current-width width)))
;; Can the new item added to the list?
(when (<= new-width (window-width)) ; can be displayed properly!
Expand All @@ -262,8 +251,8 @@ Position argument ARG0."

(defun jcs-modeline-render (left right)
"Render mode line with LEFT and RIGHT alignment."
(let* ((len-left (jcs-modeline--str-len (format-mode-line left)))
(len-right (jcs-modeline--str-len (format-mode-line right)))
(let* ((len-left (jcs-modeline--str-width (format-mode-line left)))
(len-right (jcs-modeline--str-width (format-mode-line right)))
(available-width (- (window-width) (+ len-left len-right)))
(available-width (+ available-width (jcs-modeline--adjust-pad))))
(append left
Expand Down

0 comments on commit 4fa8190

Please sign in to comment.