From e52d08aa0ebb9351dd3afce5cbe82fcfb3746ef1 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Mon, 4 Mar 2024 20:12:29 +0800 Subject: 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. --- dashboard.el | 61 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'dashboard.el') 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)))) -- cgit v1.2.3-18-g5258