grbl的富串行控制台客户端
grblcom的Python项目详细描述
ansimarkup是一种类似xml的标记,用于生成彩色终端文本。
fromansimarkupimportansiprintasprintprint("<b>bold text</b>"))print("<red>red text</red>","<red,green>red text on a green background</red,green>")print("<fg #ffaf00>orange text</fg #ffaf00>")
安装
最新稳定版本的ansimarkup可以从pypi安装:
$ pip install ansimarkup
用法
基本
fromansimarkupimportparse,ansiprint# parse() converts the tags to the corresponding ansi escape sequence.parse("<b>bold</b> <d>dim</d>")# ansiprint() works exactly like print(), but first runs parse() on all arguments.ansiprint("<b>bold</b>","<d>dim</d>")ansiprint("<b>bold</b>","<d>dim</d>",sep=":",file=sys.stderr)
颜色和样式
# Colors may be specified in one of several ways.parse("<red>red foreground</red>")parse("<RED>red background</RED>")parse("<fg red>red foreground</fg red>")parse("<bg red>red background</bg red>")# Xterm, hex and rgb colors are accepted by the <fg> and <bg> tags.parse("<fg 86>aquamarine foreground</fg 86>")parse("<bg #00005f>dark blue background</bg #00005f>")parse("<fg 0,95,0>dark green foreground</fg 0,95,0>")# Tags may be nested.parse("<r><Y>red text on a yellow foreground</Y></r>")# The above may be more concisely written as:parse("<r,y>red text on a yellow background</r,y>")# This shorthand also supports style tags.parse("<b,r,y>bold red text on a yellow background</b,r,y>")parse("<b,r,>bold red text</b,r,>")parse("<b,,y>bold regular text on a yellow background</b,,y>")# Unrecognized tags are left as-is.parse("<b><element1></element1></b>")
有关标记标记的列表,请参阅tags.py。
其他功能
通过将tag_sep参数传递给 AnsiMarkup:
fromansimarkupimportAnsiMarkupam=AnsiMarkup(tag_sep="{}")am.parse("{b}{r}bold red{/b}{/r}")
可以使用strip()方法删除标记:
fromansimarkupimportAnsiMarkupam=AnsiMarkup()am.strip("<b><r>bold red</b></r>")
命令行
ansimarkup也可以用作命令行脚本。这就好像一切 参数被传递给ansiprint():
$ python -m ansimarkup "<b>bold</b>" "<red>red</red>"
日志格式化程序
ansimarkup还为标准库提供了格式化程序日志记录 模块。它可以用作:
importloggingfromansimarkup.logformatterimportAnsiMarkupFormatterlog=logging.getLogger()hdl=logging.StreamHandler()fmt=AnsiMarkupFormatter()hdl.setFormatter(fmt)log.addHandler(hdl)log.info("<b>bold text</b>")
性能
虽然ansimarkup的重点是方便性,但它确实尝试继续处理 最少。benchmark.py脚本尝试对不同的ansi进行基准测试 转义代码库:
Benchmark 1: <r><b>red bold</b></r> colorama 0.2998 μs termcolor 3.2339 μs colr 3.6483 μs ansimarkup 6.8679 μs pastel 28.8538 μs plumbum 53.5004 μs Benchmark 2: <r><b>red bold</b>red</r><b>bold</b> colorama 0.8269 μs termcolor 8.9296 μs ansimarkup 9.3099 μs colr 9.6244 μs pastel 62.2018 μs plumbum 120.8048 μs
限制
Ansimarkup是Colorama的一个简单包装。它几乎不妨碍 验证标记字符串的格式是否正确。这是一个有意识的决定 以保持简单和快速为目标。
不平衡的嵌套,例如在下面的示例中,将产生不正确的 输出:
<r><Y>1</r>2</Y>
待办事项
- 许多角落的箱子仍有待修理。
- 更精细的测试。当前的测试套件主要包括 “幸福之路”。
- 用更多内容替换sub_end中的tag_list.index。 有效的(例如,像一个有序的多重听写)。
类似库
- pastel:给终端带来颜色
- plumbum.colors:python中类似shell脚本的程序的小型但功能丰富的库
- colr:简单的终端颜色,具有可链接的方法
许可证
ansimarkup是根据Revised BSD License的条款发布的。