自定义 doom mode line 添加一个图标显示任务进度

自定义 doom mode line 添加一个图标显示任务进度

效果

加了一个图标来显示当前工程的综合和下载进度

实现

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
  (defvar my-download-status 'downloading
    "当前下载状态,可选值:none/downloading/success/failure.")

  ;; 定义不同状态下的图标和颜色
  ;; 你可以使用 all-the-icons 包中的图标,也可用emoji或者unicode符号。
  ;; 这里仅以简单的符号为例:

  (defvar my-download-status-icon-alist
    '(
      ;; (none . "⬜")
      (none . " ")
      ;; (downloading . " ⬇ ")
      ;; (success . " ✔ ")
      ;; (failure . " ✘ ")
      (synthesis . "  ")  ;; 
      (synthesis-pass . "  ")  ;; 
      (synthesis-fail . "  ")  ;; 
      (quartus-cdb . " cdb... " )
      (quartus-asm . " asm... " )
      (quartus-fit . " fit... " )
      (quartus-sta . " sta... " )
      ;; (downloading . "[Downloading]")
      ;; (success . "[Pass]")
      ;; (failure . "[Fail]")

      (downloading . "  ")
      (success . "  ")
      (failure . "  ")
      ))

  (defun my/toggle-model-line-display ()
    (interactive)
    (if my-mode-line-valid
        (setq my-mode-line-valid nil)
      (setq my-mode-line-valid t)))

  (defun my/active-model-line-display ()
    (setq my-mode-line-valid t))

  (defvar my-mode-line-valid t
    " 在mode line 中显示图标是否激活, 激活 => 正常显示颜色, 非激活 => 显示灰色 ")

  ;; 定义一个变量来存储定时器对象
  (defvar my/toggle-model-line-display-timer nil
    "定时器,用于每秒调用 `my/toggle-model-line-display` 函数。")

  ;; 启动定时调用的函数
  (defun my/start-toggle-model-line-display ()
    "每秒调用一次 `my/toggle-model-line-display` 函数。"
    (interactive)
    (unless my/toggle-model-line-display-timer
      (setq my/toggle-model-line-display-timer
            (run-at-time t 1 #'my/toggle-model-line-display))))

  (defun my/stop-toggle-model-line-display ()
    "停止每秒调用 `my/toggle-model-line-display` 函数。"
    (interactive)
    (when my/toggle-model-line-display-timer
      (cancel-timer my/toggle-model-line-display-timer)
      (setq my/toggle-model-line-display-timer nil))
    (my/active-model-line-display))

  (use-package! doom-modeline
    :config
    (defun my-download-status-set (status)
      "设置当前下载状态."
      (setq my-download-status status)
      ;; 更新mode-line显示
      (force-mode-line-update))

    (defun my-download-status-string ()
      "根据下载状态返回带颜色的图标字符串."
      (let* ((icon (cdr (assoc my-download-status my-download-status-icon-alist)))
             (face (pcase my-download-status
                     ('success '(:foreground "green"))
                     ('failure '(:foreground "#af0000"))
                     ('downloading '(:foreground "yellow"))
                     ('synthesis-pass '(:foreground "green"))
                     ('synthesis-fail '(:foreground "#af0000"))
                     ('synthesis '(:foreground "yellow"))
                     ('quartus-cdb '(:foreground "yellow"))
                     ('quartus-asm '(:foreground "yellow"))
                     ('quartus-fit '(:foreground "yellow"))
                     ('quartus-sta '(:foreground "yellow"))
                     (_ '(:foreground "gray")))))
        (if my-mode-line-valid
            (propertize icon 'face face)
          (propertize icon 'face '(:foreground "gray")))
        ))

    (doom-modeline-def-segment my-verilog-segment
      "Show my-test info in green."
      (my-download-status-string))

    (doom-modeline-def-modeline 'my-model-line
      '(bar matches buffer-info remote-host buffer-position parrot selection-info)
      '(my-verilog-segment misc-info minor-modes input-method buffer-encoding major-mode process vcs check ))

    (add-hook 'doom-modeline-mode-hook
              (lambda ()
                (doom-modeline-set-modeline 'my-model-line 'default))))

用法

调用 my-download-status-set 修改图标, 例如: (my-download-status-set 'synthesis)
调用 (my/start-toggle-model-line-display) 实现图标闪烁
调用 (my/stop-toggle-model-line-display) 停止闪烁

Licensed under CC BY-NC-SA 4.0