以下控制字符的含义是什么:
回程
换行
表格饲料
newline control-characters ansi-escape carriage-return linefeed
在大多数终端上(当然,除了Windows的cmd.exe之外),可以使用\033ANSI转义序列对输出进行着色.
现在我正在寻找所有支持的颜色和选项列表(如明亮和闪烁).
由于支持它们的终端之间可能存在一些差异:我主要对xterm兼容终端支持的序列感兴趣.
我有一些产生颜色的脚本,我需要摆脱这个.
#!/bin/bash
exec > >(tee log) # redirect the output to a file but keep it on stdout
exec 2>&1
./somescript
Run Code Online (Sandbox Code Playgroud)
输出是(在日志文件中):
java (pid 12321) is running...@[60G[@[0;32m OK @[0;39m]
Run Code Online (Sandbox Code Playgroud)
我不知道怎么把ESC字符放在这里,所以我把@它放在了它的位置.
我将脚本更改为:
#!/bin/bash
exec > >(tee log) # redirect the output to a file but keep it on stdout
exec 2>&1
./somescript | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
Run Code Online (Sandbox Code Playgroud)
但现在它给了我(在日志文件中):
java (pid 12321) is running...@[60G[ OK ]
Run Code Online (Sandbox Code Playgroud)
我怎么能删除这个' @[60G?
也许有一种方法可以完全禁用整个脚本的着色?
我正在寻找一个代码库,通过普通标签或CSS将ANSI转义序列转换为HTML颜色.例如,可以转换它的东西:
ESC[00mESC[01;34mbinESC[00m ESC[01;34mcodeESC[00m ESC[01;31mdropbox-lnx.x86-0.6.404.tar.gzESC[00m ESC[00mfooESC[00m
进入:
<span style="color:blue">bin</span>
<span style="color:blue">code</span>
<span style="color:red">dropbox-lnx.x86-0.6.404.tar.gz</span>
foo
Run Code Online (Sandbox Code Playgroud)
将休息转换为<br/>是不必要的,它只是我不知道的转义码.我可以自己一起破解它,但我可能会错过一些重要的东西,如下划线或搞乱背景颜色如何工作.我宁愿坐在别人的代码之上.
是否存在这样的工具(命令行linux)或库(perl,python或ruby)?
这是我的字符串:
'ls\r\n\x1b[00m\x1b[01;31mexamplefile.zip\x1b[00m\r\n\x1b[01;31m'
Run Code Online (Sandbox Code Playgroud)
我正在使用代码从SSH命令检索输出,我希望我的字符串只包含'examplefile.zip'
我可以用什么来删除额外的转义序列?
我正在构建一个轻量级的ncurses库.到目前为止,它与VT100兼容终端的效果非常好,但是win32控制台无法将\033代码识别为转义序列的开头:
# include <stdio.h>
# include "term.h"
int main(void) {
puts(BOLD COLOR(FG, RED) "Bold text" NOT_BOLD " is cool!" CLEAR);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

为了加载ANSI.SYS驱动程序并识别ANSI/VT100转义序列,需要在C代码级别上做什么?
我已经做了一些事情来破坏OS X(10.5.7)终端中的Bash Shell提示符.
这是我配置的PS1:
PS1='\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '
Run Code Online (Sandbox Code Playgroud)
据我所知,我有正确的颜色命令逃逸.然而,当我在命令历史中向上和向下滚动时,如果历史命令包裹在多行上,我经常会遇到换行问题.
我将提示简化为以下内容:
PS1='\[\e[1m\]\h:\w\$ \[\e[0m\]'
PS2='> '
Run Code Online (Sandbox Code Playgroud)
我仍然看到类似的东西:
localhost:~/Library/Application Support/Firefox/Profiles/knpmxpup.Defau
lt/extensions/{1A2D0EC4-75F5-4c91-89C4-3656F6E44B68}$ expocd \{1A2D0EC4-7
5F5-4c91-89C4-3656F6E export PS1="\[
\e[1;32m\]\h\[\e[0m\]: cd Library/Appl
ication\ Support/
我也试过\033而不是\e.我刚刚将PS2包含在那里以获取信息,我没有从安装默认值中更改它.如果我完全删除颜色代码,那么一切正常,任何想法?
有没有人在编译模式Emacs中添加对ansi-color的支持?如果是这样,颜色编写程序必须检查哪个属性/属性,以确保其活动终端支持ANSI-escape着色.
为了检测控制台,正确sys.stderr还是sys.stdout,我正在做以下测试:
if hasattr(sys.stderr, "isatty") and sys.stderr.isatty():
if platform.system()=='Windows':
# win code (ANSI not supported but there are alternatives)
else:
# use ANSI escapes
else:
# no colors, usually this is when you redirect the output to a file
Run Code Online (Sandbox Code Playgroud)
现在,通过IDE(如PyCharm)运行此Python代码时,问题变得更加复杂.最近PyCharm添加了对ANSI的支持,但第一次测试失败:它具有isatty属性但设置为False.
我想修改逻辑,以便正确检测输出是否支持ANSI着色.一个要求是在任何情况下我都不应该在输出重定向到文件时输出一些东西(对于控制台,它是可以接受的).
通过https://gist.github.com/1316877添加了更复杂的ANSI测试脚本
我知道在bash终端中一种可靠的改变颜色的方法是使用ANSI转义序列.例如:
echo -e "\033[0;31mbrown text\033[0;00m"
Run Code Online (Sandbox Code Playgroud)
应该输出
brown text (in brown)
Run Code Online (Sandbox Code Playgroud)
有没有办法使用ANSI的特定RGB集输出颜色?说我要亮红色:
echo -e "**\033[255:0:0m**red text\033[0;00m"
Run Code Online (Sandbox Code Playgroud)
这种事情存在吗?
我只是想使用标准的bash.