彩印
colour-text的Python项目详细描述
彩色文本
彩色文本和彩色打印命令
colour-print
是使打印彩色文本的命令
在Unix、MacOS和Windows上更容易操作。它使用ColourText
给课文上色。
该命令使用<>colour-name text<>
标记。
$ colour-print "<>info Info:<> this is an <>em informational<> message" $ colour-print "<>error Error: This is an error message<>"
如果有更多参数,则将第一个参数视为格式字符串。
$ colour-print "<>info Info:<> Home folder is %s""$HOME"
类颜色文本
ColourText
类将带有颜色标记的字符串转换为
一种适合在支持颜色的终端上打印的表格
文本。这包括MacOS、Windows上的大多数终端模拟器
和Unix。
要给一段文字上色,先用标记,再用颜色 命名一个空格和以标记结尾的彩色文本。
fromcolour_textimportColourTextct=ColourText()ct.initTerminal()print(ct.convert("The next section is in green: <>green example<>."))
要将标记包含为文本,请使用两个相邻的标记。
fromcolour_textimportColourTextct=ColourText()ct.initTerminal()print(ct("A <>red literal marker<> <><> in the string"))
colouttext可以与gettext一起用于国际化应用程序。
fromcolour_textimportColourTextct=ColourText()ct.initTerminal()message="<>red Error: cannot open file %s<>"i18n_message=_(message)coloured_i18n_message=ct(i18n_message)formatted_message=coloured_i18n_message%(file_name,)print(formatted_message)# or in one lineprintct(_("<>red Error: cannot open file %s<>"))%(file_name,))
类颜色文本
__init__( marker='<>' )
marker
是用于标记颜色部分的字符串 默认为tilda(<>
)。initTerminal()
确保终端可以显示彩色文本。
必须在windows上调用,并且可以在macos和unix系统上安全地调用。
define( name, colour_def )
定义一种颜色
name
,用于标记部分。 ^ {CD11}}是现有颜色名称的列表。 或者一个名字。内置的前景色名称是:
bold, black, brown, green, yellow, blue, magenta, cyan, gray, red, lightred, lightgreen, lightyellow, lightblue, lightmagenta, lightcyan and white.
内置的背景色名称是:
bg-black, bg-brown, bg-green, bg-yellow, bg-blue, bg-magenta, bg-cyan, bg-gray and bg-white.
例如,将名称
info
添加为绿色文本,将error
添加为 在白色背景上显示红色文本:
ct=ColourText()ct.define('info','green')ct.define('error',('red','bg-white'))ct("Error messages are <>error shown like this<>")
convert( colour_text )
解释颜色文本字符串中的颜色标记 并返回适合在终端上打印的字符串。
__call__( colour_text )
以简洁的方式调用
convert( colour_text )
。