跳转到主要内容

textwrap,但更擅长处理ANSI颜色和样式

项目描述

travisci PyPI Package latest release Supported versions Supported implementations Wheel packaging support Test line coverage

ansiwrap 包装文本,就像标准的 textwrap 模块一样。但它也正确地包装了包含ANSI控制序列的文本,这些序列可以给文本上色或设置样式。

textwrap 被那些控制代码的原始字符串长度欺骗时,ansiwrap 不会;它明白,无论这些代码如何影响颜色和显示样式,它们都没有逻辑长度。

API与 textwrapwrapfillshorten 函数相匹配。例如

from __future__ import print_function
from colors import *     # ansicolors on PyPI
from ansiwrap import *

s = ' '.join([red('this string'),
              blue('is going on a bit long'),
              green('and may need to be'),
              color('shortened a bit', fg='purple')])

print('-- original string --')
print(s)
print('-- now filled --')
print(fill(s, 20))
print('-- now shortened / truncated --')
print(shorten(s, 20, placeholder='...'))

它还导出了一些其他函数

  • ansilen(给出字符串的有效长度,忽略ANSI控制代码)

  • ansi_terminate_lines(将控制代码传播到字符串/行列表中,并终止每一行。)

  • strip_color(从字符串中删除ANSI控制代码)

请参阅附带的 demo.py

https://content.screencast.com/users/jonathaneunice/folders/Jing/media/8db64be2-01cc-4da4-b46a-789c53c63b44/00000569.png

项目详情


下载文件

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

源代码发行版

ansiwrap-0.8.4.zip (17.7 kB 查看哈希值)

上传时间 源代码

构建发行版

ansiwrap-0.8.4-py2.py3-none-any.whl (8.5 kB 查看哈希值)

上传时间 Python 2 Python 3

支持