2 Star 10 Fork 1

shede333 / SWTermColor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

SWTermColor

支持在终端打印彩色文案,效果如最下面截图;

封装了 termcolor库,更方便使用了; 同时,也借鉴了 termcolor2库的一些思想;

支持 python2 + python3;

注意:不同的终端软件、不同Shell,的输出效果会有些许的不同。

Install


pip install SWTermColor

Example Modules:

文本颜色,都是以 "c_" 开头的方法;
背景色,都是以 "b_" 开头的方法;
文字特性(加粗等),都是以 "a_" 开头的方法;


from swtermcolor import SWTermColor
# 或者
from swtermcolor import ColorPrint
# 或者
from swtermcolor import PrintColor

# 注意:
# SWTermColor == ColorPrint == PrintColor, 三者都是等价的类名,根据喜好选用
SWTermColor().c_red().p("hello, SWTermColor")  # p 即 print的缩写
PrintColor().c_red().p("hello, PrintColor")
ColorPrint().c_red().p("hello, ColorPrint")


# 常用的几种打印方法:
SWTermColor("hello, sw red").c_red().p()  # 先设置文本,再设置效果
SWTermColor().c_red().p("hello, sw red")  # 先设置效果,再设置文本
print(SWTermColor("hello, sw red").c_red())  # 使用print方法打印
# 默认以", "为分隔符,打印所有文本
SWTermColor().c_red().p("hello", "sw", "red")
# 以" * "为分隔符,打印所有文本
SWTermColor().c_red().p("hello", "sw", "red", sep=" * ")


print(SWTermColor("hello, sw red").c_blue().c_red())
print(SWTermColor("hello, sw red bg_grey").c_red().b_grey())
SWTermColor("hello, sw red bg_blue").c_red().b_blue().p()
# 背景色:蓝色 覆盖了 白色,同时会打印一行覆盖警告
print(SWTermColor("hello, sw red bg_blue").c_red().b_white().b_blue())
print(SWTermColor("hello, sw red bg_blue bold").c_red().b_blue().a_bold())
print(SWTermColor("hello, sw red bg_blue bold dark").c_red().b_blue().a_bold().a_dark())
print(SWTermColor("hello, sw red blue bold reverse(反转)").c_red().b_blue().a_bold().a_reverse())


# 文本 与 样式 分离
text = "hello, sw red bg_blue bold 文本 与 样式 分离"  # 文本
effect = SWTermColor(prefix_text="提示文案前缀:").c_red().b_blue().a_bold()  # 特效
print(effect)  # 输出空行,因为没有文本信息
effect.p("看打印效果")
print(effect(text))  # 正常使用方法
print(effect(text, "color临时改为grey", color="grey"))  # 临时改变文本颜色
print(effect(text, "bg_color临时改为grey", bg_color="grey"))  # 临时改变文本背景色
print(effect(text, "attrs临时改为reverse", attrs=["reverse"]))  # 临时改变文本效果

# 输出彩色的:1 + 2 = 3
effect._prefix_text = None  # 删除 提示文案
print(effect("彩色文本") + " + 普通文本")  # 与普通str 相加
print("普通文本 + " + effect("彩色文本"))  # 与普通str 相加
print(effect(" 1") + effect(" + ", bg_color="cyan") + effect("2") + effect(" = ", bg_color="cyan") + effect("3 "))

截图(ScreenShot)

image1

致谢

待完成的功能

  1. 内置一些彩色打印模板,类似警告、错误等(✔: \u2714, ✘: \u2718);
  2. 集成 colorama项目的功能
MIT License Copyright (c) 2019 shede333 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

CLI终端输出彩色的文本, simple termcolor wrapper 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/shede333/SWTermColor.git
git@gitee.com:shede333/SWTermColor.git
shede333
SWTermColor
SWTermColor
master

搜索帮助