彩印

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"

screenshot of help output

类颜色文本

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 )

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java CXF和授权   java在网站中部署使用swing创建的表单   java为什么getHeaderField()返回一个字符串,其中getHeaderFields()返回HttpUrlConnection中的Map<String,List<String>>   java如何检测恶意数据包?   webview中的java网页为空   java SWT图像资源,用于将我的所有图像存储在一个位置   java计算数组的最大长度,使平均值小于给定值   java“发件人电话号码无效”和美国号码   将Swing组件作为内容的自定义Java工具提示不会显示   在并发HashMap中重新灰化期间的java检索   Java 7和Tomcat 7.0.64 ClassFormatException:常量池中的字节标记无效   使用JUnit的java assertNull因NullPointerException失败   java内存中的文件是否与文件系统中的文件大小相同?   循环内实例化的类型的java注入依赖项