elisp:在回显区域输出彩色消息

时间:2018-06-20 23:33:44

标签: emacs echo message spacemacs

以下是我的shell命令的输出

Result [31mERROR[0m Invalid Transition "Backlog" from "In Progress", Available: Ready for Code Review, Stop Development

我正在尝试以这种方式在回显区域中输出它:

(setq result (shell-command-to-string (
            concat "j transition \"" state "\" " jira-ticket " --noedit")))
  (message "Result %s" result))

但它不会打印红色的“ ERROR”,

是否可以在emacs中使用?

,如果不可能的话-如何自动剥离此类字母?

系统信息:计算机:
  • 操作系统:gnu / linux
  • Emacs:26.1
  • Spacemacs:0.200.13
  • Spacemacs分支:主服务器(修订版c7a103a7)
  • 图形显示:t
  • 发行:spacemacs
  • 编辑风格:emacs
  • 完成情况:掌舵
  • 层: elisp (gnus php yaml themes-megapack html javascript helm (auto-completion :variables auto-completion-enable-snippets-in-popup t) emacs-lisp (git :variables git-magit-status-fullscreen t magit-save-repository-buffers 'dontask magit-refs-show-commit-count 'all magit-revision-show-gravatars nil) markdown org (shell :variables shell-default-height 30 shell-default-term-shell "/usr/bin/zsh" shell-default-position 'bottom) (syntax-checking :variables flycheck-select-checker 'javascript-eslint) (version-control :variables version-control-diff-tool 'diff-hl version-control-global-margin t) evil-commentary chrome python django dmitry)
  • 系统配置功能:XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND DBUS GSETTINGS NOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 MODULES THREADS XWIDGETS LCMS2

1 个答案:

答案 0 :(得分:3)

n.b。我假设您 正在获取正确的ansi转义码,并且转义字符在发布问题的过程中丢失了。


这是从alter-text-property代码派生的解决方案。

(defun copy-font-lock-face-to-face (from to &optional object)
  "Programmatically copy `font-lock-face' text-properties as `face' properties.

Optional third argument OBJECT specifies the string or buffer to operate on."
  ;; Derived from `alter-text-property'.
  (let ((begin from)
        end val)
    (while (setq val (get-text-property begin 'font-lock-face object)
                 end (text-property-not-all begin to 'font-lock-face val object))
      (put-text-property begin end 'face val object)
      (setq begin end))
    (if (< begin to)
        (put-text-property begin to 'face val object))))

(defun message-with-ansi-color (format-string &rest args)
  "A variant of `message' which supports ansi color escape codes."
  (require 'ansi-color)
  (let* ((text (ansi-color-apply (apply #'format format-string args))))
    (copy-font-lock-face-to-face 0 (length text) text)
    (message "%s" text)))

(message-with-ansi-color "%s" "Result \e[31mERROR\e[0m Invalid Transition")

原始答案如下...


我曾期望ansi-color-apply能做到这一点:

(require 'ansi-color)
(message "%s" (ansi-color-apply "Result \e[31mERROR\e[0m Invalid Transition \"Backlog\" from \"In Progress\", Available: Ready for Code Review, Stop Development"))

但是看起来message会处理 1 face文本属性,而不是font-lock-face文本属性,并且ansi-color-apply会生成后者(如果您修改该函数的定义,将font-lock-face更改为face,然后您将获得彩色消息。

如果您想麻烦的话,可以操纵结果字符串来转换这些文本属性,从我的阅读来看,这样做是完全安全的:

  

‘font-lock-face’
      此属性指定“字体”模式应应用于基础文本的“ face”属性的值。

我认为字体锁定代码中可以为您做些什么,但是我不知道那是什么-希望有更多熟悉的人可以填补这一空白。

剥离字符串中的转义码,可以使用ansi-color-filter-apply代替ansi-color-apply


1 仅用于回声区域输出;法律列表指出,文本属性未应用于*Messages*缓冲区中。