具有 ANSI 代码渲染支持 Windows 的文本编辑器/查看器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2271785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:07:31  来源:igfitidea点击:

Text editor/viewer with ANSI codes rendering support for Windows

windowseditorsyntax-highlightingansi

提问by SF.

I need some tool to display text containing ANSI codes correctly on Windows. No full support needed, but at least coloring/bold is a must.

我需要一些工具来在 Windows 上正确显示包含 ANSI 代码的文本。不需要完全支持,但至少必须着色/粗体。

Reason: My logger/debug module produce nicely rendered rich output with important sections colored using ANSI codes. This helps a lot when debugging on the serial terminal, but if I dump the debug to a file or copy-paste it into a text editor on Windows (interactive remote debug is not always viable), at best all the ANSI codes are stripped, at worst they are rendered as junk characters obscuring the real data. Rudimentary editing capabilities would be appreciated to be able to pick out specific parts, annotate, and so on.

原因:我的记录器/调试模块产生了很好呈现的丰富输出,重要部分使用 ANSI 代码着色。这在串行终端上调试时有很大帮助,但是如果我将调试转储到文件或将其复制粘贴到 Windows 上的文本编辑器中(交互式远程调试并不总是可行),至多所有 ANSI 代码都被剥离,在最坏的情况下,它们会被渲染为模糊真实数据的垃圾字符。能够挑选出特定部分、注释等的基本编辑功能将不胜感激。

采纳答案by Andras Vass

You might have some more luck with ASCII/ANSI utilities, like the ones listed here: List of ASCII/ANSI/NFO utilities

您可能对 ASCII/ANSI 实用程序有更多的运气,例如此处列出的那些: ASCII/ANSI/NFO 实用程序列表

**Note: some files on this page might be outdated, you might find newer versions of these utilities on their respective homepages.*

**注意:此页面上的某些文件可能已过时,您可能会在其各自的主页上找到这些实用程序的较新版本。*

For example, the latest version of NFOPad can be found here.

例如,可以在此处找到最新版本的 NFOPad

回答by Jeffson

If you're primarily interested in viewing the file instead of editing it, Ansifilter will convert it to HTML, which you can then view and at least search in your browser, or RTF if wordpad would be good enough (hard to imagine). Huh, looks like there's a notepad++ plugin version on the download page, too, so that might be perfect if it allows you to load into notepad++.
http://www.andre-simon.de/doku/ansifilter/ansifilter.html

如果您主要对查看文件而不是编辑文件感兴趣,Ansifilter 会将其转换为 HTML,然后您可以查看并至少在浏览器中搜索,或者如果写字板足够好(很难想象),则可以使用 RTF 格式。嗯,看起来下载页面上也有一个记事本++插件版本,所以如果它允许您加载到记事本++中,那可能是完美的。
http://www.andre-simon.de/doku/ansifilter/ansifilter.html

There's also a different plugin for vim which colors text according to ANSI codes.
http://www.vim.org/scripts/script.php?script_id=302

还有一个不同的 vim 插件,它根据 ANSI 代码为文本着色。
http://www.vim.org/scripts/script.php?script_id=302

However, while it highlights the text in the correct color, it leaves the ANSI codes themselves in there (in a faded, near-background color) which probably will mess up any alignment formatting in the file, as well as making it harder to move around the file (lots of "empty space" to wade the cursor through, searching for a word won't match if there's an ansi code in the middle of it, etc.). There's a patch it can take advantage of to hide the codes too, but that would require patching and then recompiling vim itself from source.

然而,虽然它以正确的颜色突出显示文本,但它会将 ANSI 代码本身留在那里(以褪色的、接近背景的颜色),这可能会弄乱文件中的任何对齐格式,并使其更难移动在文件周围(有很多“空白”可以让光标通过,如果中间有 ansi 代码,搜索单词将不匹配,等等)。它也可以利用一个补丁来隐藏代码,但这需要打补丁,然后从源代码重新编译 vim 本身。

Yeah, suggesting vim is pretty unhelpful if you aren't a vim user already, it has too huge of a learning curve, I know. But it might be useful to the vim users out there.

是的,如果您还不是 vim 用户,建议 vim 非常无用,我知道它的学习曲线太大了。但它可能对那里的 vim 用户有用。

回答by autronix

I've been looking for a solution to display the ANSI colors as well (for program debug output readability) and stumbled upon Sublime Text (paid software with trial http://www.sublimetext.com/) with a the ANSIescape package (https://github.com/aziz/SublimeANSIor installed through the package control).

我一直在寻找一个解决方案,以显示ANSI颜色以及(对于程序调试输出可读性)和基于文本崇高(付费软件试用跌跌撞撞http://www.sublimetext.com/)用的ANSIescape包(HTTPS ://github.com/aziz/SublimeANSI或通过包控件安装)。

It supports coloring and the bold escape is recognized but not displayed, although a special color can be assigned to it in the settings file. Also worth noting that this plugin shows text in read-only mode, and needs to be turned off if editing is necessary.

它支持着色,虽然可以在设置文件中为其分配特殊颜色,但可以识别但不显示粗体转义符。另外值得注意的是,这个插件以只读模式显示文本,如果需要编辑,需要关闭。

Here is the screenshot provided on the github, and I have personally tried it and verified it works:

下面是github上提供的截图,本人亲自试过,验证有效:

enter image description here

在此处输入图片说明

回答by guo

The open-source editor Atomhas the package language-ansi-styles. It supports all kinds of formatting except ;r;g;b.

开源编辑器Atom具有包language-ansi-styles。它支持除;r;g;b.

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Trevor

If you just want to view then the terminal program "Tera Term" can do this. Just click "File" -> "Replay Log" and select your file containing the ANSI codes.

如果您只是想查看,那么终端程序“Tera Term”可以做到这一点。只需单击“文件”->“重播日志”并选择包含 ANSI 代码的文件。

You can download Tera Term here: http://logmett.com/index.php?/download/tera-term-477-freeware.html

你可以在这里下载 Tera Term:http: //logmett.com/index.php?/download/tera-term-477-freeware.html

回答by sdaau

I know it won't be of much help - but I was looking for the exact same thing on linux; was just trying to view some log outputs that had bash ANSI color codes inside. Unfortunately, those ANSI color codes were spread across several lines - meaning 'cat'-ing the file and piping into 'less -R', 'most' and similar tools, would simply display the starting line where the color originated, but not the subsequent lines that should've been colored.

我知道它不会有太大帮助 - 但我一直在 linux 上寻找完全相同的东西;只是想查看一些里面有 bash ANSI 颜色代码的日志输出。不幸的是,那些 ANSI 颜色代码分布在多行中——这意味着“cat”文件并管道到“less -R”、“most”和类似工具,只会显示颜色来源的起始行,而不是应该已经着色的后续行。

Funnily enough, I thought usual Linux tools like 'nano', 'gedit', 'vim' and whatnot would have capabilities for ANSI color codes in a text file, but it's very modest out there with info on ANSI color in text files in these editors. I've only found info on ANSI color for the test editor 'joe':

有趣的是,我认为像“nano”、“gedit”、“vim”等通常的 Linux 工具在文本文件中具有 ANSI 颜色代码的功能,但是在这些文本文件中提供有关 ANSI 颜色的信息的情况非常有限编辑。我只找到了有关测试编辑器“joe”的 ANSI 颜色信息:

Cheap ANSI Color! - http://tldp.org/LDP/LG/issue01to08/articles.html#ansi

便宜的 ANSI 颜色!- http://tldp.org/LDP/LG/issue01to08/articles.html#ansi

but couldn't get the recommendations there to work (also couldn't get 'emacs' to work either, at least not by directly reading a text file with ANSI color characters inside).

但无法让建议在那里工作(也无法让“emacs”工作,至少不是通过直接读取内部带有 ANSI 颜色字符的文本文件)。

The good thing - it seems what you need, if you need ANSI color in text, is to look for ASCII art / NFO utilities as recommended above - and the one that I finally found, and was working for me, was tetradraw(via www.linux.org/apps/AppId_42.html ; can be sudo apt-get installed in Ubuntu ... actually, tetradraw is the name of the drawing/editor part - however there is a separate viewer that also works with ANSI color codes, tetraview).

好事 - 如果您需要文本中的 ANSI 颜色,似乎您需要的是按照上面推荐的方式查找 ASCII art / NFO 实用程序 - 而我最终找到并对我来说有效的实用程序是tetradraw(通过 www .linux.org/apps/AppId_42.html ; 可以在 Ubuntu 中安装 sudo apt-get ......实际上, tetradraw 是绘图/编辑器部分的名称 - 但是有一个单独的查看器也适用于 ANSI 颜色代码,四视图)。

Well, who would have thought, that you need to track down an ASCII art utility, in order to read log files :)

好吧,谁会想到,您需要追踪一个 ASCII 艺术实用程序,以便读取日志文件:)

Anyways, hope this may somehow help in the further search of ANSI color text editors for Windows, too.. Cheers!

无论如何,希望这也能以某种方式帮助进一步搜索适用于 Windows 的 ANSI 彩色文本编辑器..干杯!

回答by ens

In Emacs, just eval the following before opening your .nfo file:

在 Emacs 中,只需在打开 .nfo 文件之前评估以下内容:

(add-to-list 'auto-coding-alist '("\\.nfo\\'" . cp437-dos))

(add-to-list 'auto-coding-alist '("\\.nfo\\'" . cp437-dos))

回答by Sopalajo de Arrierez

I have been a while testing multiple programs on the URL refered by Andras Vass with no results(they don't show colors, or they keep showing ANSI codes as a mess of characters).

我一直在测试 Andras Vass 引用的 URL 上的多个程序,但没有结果(它们不显示颜色,或者它们一直将 ANSI 代码显示为一堆字符)。

Tired of searching I have finally found ANSIFilter(not the NotePad++ plugin refered by Jeffson), the only that works for me. I have added it to Windows context menu, so I can now easily open my ANSI text files.

厌倦了搜索,我终于找到了ANSIFilter(不是 Jeffson 提到的 NotePad++ 插件),这是唯一对我有用的。我已将它添加到 Windows 上下文菜单中,因此我现在可以轻松打开我的 ANSI 文本文件。

回答by Patrick

If the debug logging of your application goes via 1 class/function, you could try to split the output so that:

如果您的应用程序的调试日志通过 1 个类/函数进行,您可以尝试拆分输出,以便:

  • ANSI-like logging is shown on the terminal/console
  • HTML-like logging is written to file
  • 类似 ANSI 的日志记录显示在终端/控制台上
  • 类似 HTML 的日志被写入文件

For your application all logging goes to this class, and this class splits the output to terminal/console and file.

对于您的应用程序,所有日志记录都转到此类,此类将输出拆分为终端/控制台和文件。

Make a 'standard' in your logging class for specifying colors and boldness (e.g. predefined codes like Ctrl-A means red, Ctrl-B means bold, ..., or specific methods in the logging class for setting the color and boldness, or maybe even the ANSI-codes), and translate this in your central logging class to:

在您的日志类中制定一个“标准”来指定颜色和粗体(例如,预定义的代码,如 Ctrl-A 表示红色,Ctrl-B 表示粗体,...,或日志类中用于设置颜色和粗体的特定方法,或甚至可能是 ANSI 代码),并将其在您的中央日志类中转换为:

  • the correct ANSI codes on terminal
  • the correct HTML codes in file
  • 终端上正确的 ANSI 代码
  • 文件中正确的 HTML 代码

Alternatively, I think that instead of HTML you also could use rich-text, but I don't know all the possibilities of rich text so you may have to look this up.

或者,我认为您也可以使用富文本代替 HTML,但我不知道富文本的所有可能性,因此您可能需要查找此内容。

回答by Zsolt Botykai

I would be surprised if emacs can't do that. At least with the embeded shell. There are:

如果 emacs 不能做到这一点,我会感到惊讶。至少有嵌入式外壳。有:

Update: as it had been pointed, they are just term output colorizers. But if you can edit the shellbuffer contents in emacs too, eg. cat file&& colorize.

更新:正如所指出的,它们只是术语输出着色器。但是,如果您也可以在 emacs 中编辑shell缓冲区内容,例如。cat file&& 着色。

But wait a minute, I had just found these:

但是等一下,我刚刚发现了这些: