跳转到主要内容

GRBL的丰富串行控制台客户端

项目描述

Latest version released on PyPi Build status BSD 3-Clause

ansimarkup是一个类似于XML的标记,用于生成彩色终端文本。

from ansimarkup import ansiprint as print

print("<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>")

安装

可以从pypi安装ansimarkup的最新稳定版本

$ pip install ansimarkup

用法

基本

from ansimarkup import parse, 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

用户定义的标签

可以通过创建一个新的 AnsiMarkup 实例来定义自定义标签或现有标签的覆盖

from ansimarkup import AnsiMarkup, parse

user_tags = {
    # Add a new tag (e.g. we want <info> to expand to "<bold><green>").
    "info": parse("<b><g>")

    # The ansi escape sequence can be used directly.
    "info": "e\x1b[32m\x1b[1m",

    # Tag names may also be callables.
    "err":  lambda: parse("<r>")

    # Colors may also be given convenient tag names.
    "orange": parse("<fg #d78700>"),

    # User-defined tags always take precedence over existing tags.
    "bold": parse("<dim>")
}

am = AnsiMarkup(tags=user_tags)

am.parse("<info>bold green</info>")
am.ansiprint("<err>red</err>")

# Calling the instance is equivalent to calling its parse method.
am("<b>bold</b>") == am.parse("<b>bold</b>")

其他特性

可以通过将 tag_sep 参数传递给 AnsiMarkup 来更改默认的标签分隔符

from ansimarkup import AnsiMarkup

am = AnsiMarkup(tag_sep="{}")
am.parse("{b}{r}bold red{/b}{/r}")

可以使用 strip() 方法删除标记标签

from ansimarkup import AnsiMarkup

am = AnsiMarkup()
am.strip("<b><r>bold red</b></r>")

命令行

ansimarkup也可以用作命令行脚本。这相当于将所有参数传递给 ansiprint()

$ python -m ansimarkup "<b>bold</b>" "<red>red</red>"

日志格式化程序

ansimarkup还附带标准库 logging 模块的格式化程序。它可以用来

import logging
from ansimarkup.logformatter import AnsiMarkupFormatter

log = logging.getLogger()
hdl = logging.StreamHandler()
fmt = AnsiMarkupFormatter()
hdl.setFormatter(fmt)
log.addHandler(hdl)

log.info("<b>bold text</b>")

Windows

ansimarkup内部使用colorama库,这意味着在首先运行之后,Windows支持ansi转义序列

import colorama
colorama.init()

有关Windows支持的更多信息,请参阅colorama文档的“用法”部分。

性能

虽然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 替换为更高效的东西(例如有序的 MultiDict)。

类似库

  • pastel:为您的终端带来颜色

  • plumbum.colors:Python 中类似 shell 脚本程序的小型但功能丰富的库

  • colr:易于使用的终端颜色,具有可链接的方法

许可

Ansimarkup 在修订版 BSD 许可证的条款下发布。

项目详情


下载文件

下载适合您平台文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源分发

grblcom-0.0.0.tar.gz (8.2 kB 查看哈希值)

上传时间 源代码

构建分发

grblcom-0.0.0-py2.py3-none-any.whl (11.2 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者