我有一个使用ANSI转义颜色代码格式化文本的日志文件。模式是fundamental
。有其他已回答的问题涉及此问题,但我不确定如何将其应用于此模式或任何其他模式。我知道解决方案与以某种方式配置ansi-color
有关。
我有一个使用ANSI转义颜色代码格式化文本的日志文件。模式是fundamental
。有其他已回答的问题涉及此问题,但我不确定如何将其应用于此模式或任何其他模式。我知道解决方案与以某种方式配置ansi-color
有关。
(require 'ansi-color)
(defun display-ansi-colors ()
(interactive)
(ansi-color-apply-on-region (point-min) (point-max)))
然后您可以通过M-x执行display-ansi-colors
,通过自己选择的键绑定或通过某些编程条件(也许您的日志文件具有与某些正则表达式匹配的扩展名或名称)来执行。
如果您想在只读缓冲区(日志文件、grep结果)中执行此操作,可以使用inhibit-read-only
,因此函数将如下所示:
(defun display-ansi-colors ()
(interactive)
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max))))
.log
文件上开启 ansi-color 显示(请根据你的文件扩展名进行选择):(add-to-list 'auto-mode-alist '("\\.log\\'" . display-ansi-colors))
- Son of the Wai-Panansi-color-apply-on-region
函数接受第三个参数"preserve-sequences"。因此,我们可以改进@juanleon的答案。 (interactive)
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max) t)))```
- undefined用户自定义函数:
(defun my-ansi-color (&optional beg end)
"Interpret ANSI color esacape sequence by colorifying cotent.
Operate on selected region on whole buffer."
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(ansi-color-apply-on-region beg end))
对于使用comint/compilation的缓冲区,请使用filter过滤器:
(ignore-errors
(require 'ansi-color)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
(add-to-list 'load-path "path/to/your/tty-format.el/")
(require 'tty-format)
;; M-x display-ansi-colors to explicitly decode ANSI color escape sequences
(defun display-ansi-colors ()
(interactive)
(format-decode-buffer 'ansi-colors))
;; decode ANSI color escape sequences for *.txt or README files
(add-hook 'find-file-hooks 'tty-format-guess)
;; decode ANSI color escape sequences for .log files
(add-to-list 'auto-mode-alist '("\\.log\\'" . display-ansi-colors))
tty-format基于ansi-color.el,后者只在较新版本的emacs中原生地提供。
ansi-color-apply-on-region
的性能较慢。这里提供了一种解决方案,可以对当前区域进行着色,并且可以在只读缓冲区中工作。(require 'ansi-color)
(defun ansi-color-region ()
"Color the ANSI escape sequences in the acitve region.
Sequences start with an escape \033 (typically shown as \"^[\")
and end with \"m\", e.g. this is two sequences
^[[46;1mTEXT^[[0m
where the first sequence says to diplay TEXT as bold with
a cyan background and the second sequence turns it off.
This strips the ANSI escape sequences and if the buffer is saved,
the sequences will be lost."
(interactive)
(if (not (region-active-p))
(message "ansi-color-region: region is not active"))
(if buffer-read-only
;; read-only buffers may be pointing a read-only file system, so don't mark the buffer as
;; modified. If the buffer where to become modified, a warning will be generated when emacs
;; tries to autosave.
(let ((inhibit-read-only t)
(modified (buffer-modified-p)))
(ansi-color-apply-on-region (region-beginning) (region-end))
(set-buffer-modified-p modified))
(ansi-color-apply-on-region (region-beginning) (region-end))))
ansi-color-apply-on-region
会从缓冲区中删除 ANSI 转义序列字符,因此当您保存时,它们将丢失。我想知道是否有一种方法可以隐藏这些字符而不是剥离它们?(require ansi-color)
(emacs 28.2)的小模式。该小模式懒惰地运行`ansi-color-apply-on-region',即仅处理缓冲区中可见的部分。因此,即使日志文件很大,它也不会冻结Emacs。(defun ansi-color-after-scroll (window start)
"Used by ansi-color-mode minor mode"
(ansi-color-apply-on-region start (window-end window t) t))
(define-minor-mode ansi-color-mode
"A very primitive minor mode to view log files containing ANSI color codes.
Pros: this minor mode runs `ansi-color-apply-on-region' lazily,
i.e. only the visible part of the buffer. Hence, it does NOT
freeze Emacs even if the log file is huge.
Cons: a) when the minor code is toggled off, it does not undo
what has already been ansi colorized. b) assumes the buffer
content etc. does not change. c) jumping to random places within
the buffer may incur incorrect/incomplete colorization.
How to install: put this code into your init.el, then evaluate it or
restart Emacs for the code to take effect.
How to use: in the log buffer of need run `M-x ansi-color-mode'.
Alternatively, feel free to enable this minor mode via mode hooks
so that you needn't enable it manually.
-- lgfang
"
:global nil
:lighter ""
(if ansi-color-mode
(progn
(ansi-color-apply-on-region (window-start) (window-end) t)
(add-hook 'window-scroll-functions 'ansi-color-after-scroll 80 t))
(remove-hook 'window-scroll-functions 'ansi-color-after-scroll t)))
emacs
时存在的限制/复杂性,你可能想尝试一种常见的替代方案来处理大型日志文件:less -R /path/to/file.log
。 - Jeff Ward