summaryrefslogtreecommitdiff
path: root/dashboard.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2024-03-04 20:12:29 +0800
committerJSDurand <mmemmew@gmail.com>2024-03-04 20:12:29 +0800
commite52d08aa0ebb9351dd3afce5cbe82fcfb3746ef1 (patch)
treec5364e45a63a7f80dc30c8b90f743109591ac404 /dashboard.el
parent5ca627fe4fa9685090ceea961a4dde339c4245e0 (diff)
dashboard: refactor
* dashboard.el (dashboard-center-string): Now use a dedicated function for the purpose of centering strings. This was originally done by the function `center-string-in-width` in text-conf, but that function behaved weirdly for some reason. (dashboard): Adapt the function for correct centering effect.
Diffstat (limited to 'dashboard.el')
-rw-r--r--dashboard.el61
1 files changed, 40 insertions, 21 deletions
diff --git a/dashboard.el b/dashboard.el
index 1abc53d..56bb616 100644
--- a/dashboard.el
+++ b/dashboard.el
@@ -1,5 +1,29 @@
;;; dashboard.el --- My daashboard -*- lexical-binding: t; -*-
+(defun dashboard-center-string (str)
+ "Return a centered string."
+ (let ((current-window-width (1- (window-width nil t)))
+ (str-width (string-pixel-width str)))
+ (cond
+ ((display-graphic-p)
+ (concat
+ (propertize
+ (string 32)
+ 'display
+ (list
+ 'space
+ :width
+ (list (floor (- current-window-width str-width) 2))))
+ str))
+ (t
+ (concat
+ (make-string
+ (round
+ (- current-window-width str-width)
+ (* 2 (string-pixel-width (string 32))))
+ 32)
+ str)))))
+
;;;###autoload
(defvar dashboard-buffer-name "durand"
"The name of the buffer for the dashboard.")
@@ -42,37 +66,32 @@ If FORCE is non-nil, re-gerenate the dashboard buffer."
(erase-buffer)
(cond
((display-graphic-p)
- (let* ((image
- (create-image
- (expand-file-name
- "cat.png"
- user-emacs-directory)))
- (str (propertize
- " "
- 'display image
- 'rear-nonsticky '(display)))
- (str-width (string-pixel-width str)))
+ (let ((image
+ (create-image
+ (expand-file-name
+ "cat.png"
+ user-emacs-directory))))
(insert
(make-string 3 10)
- (propertize
- (string 32)
- 'display
- (list
- 'space
- :width
- (list (floor (- (window-body-width nil t) str-width) 2))))
- str)))
+ (dashboard-center-string
+ (propertize
+ " "
+ 'display image
+ 'rear-nonsticky '(display))))))
((mapc (function
(lambda (str)
(insert
- (center-string-in-width str (- (window-body-width) 13))
+ (dashboard-center-string str)
"\n")))
dashboard-cat-text)))
(newline 5)
- (insert (center-string-in-width "Dashboard" (window-body-width)))
+ (insert
+ (dashboard-center-string "Dashboard"))
(set-buffer-modified-p nil)
(read-only-mode 1)
- (set 'mode-line-format '("%e" (:eval (modeline-format-dashboard))))
+ (set
+ 'mode-line-format
+ '("%e" (:eval (modeline-format-dashboard))))
(set 'default-directory user-emacs-directory)
(view-mode 1)
(dashboard-mode))))