轻松打造您的终端!

styled的Python项目详细描述


PyPI versionPyPI - Python VersionCoverage StatusBuild Status

简介

欢迎使用styled,这是一个简单的python包,可以轻松地向终端编写漂亮的文本。 styled背后的主要创新是完全简单的用户界面,它不需要用户知道 除了样式名之外的任何东西。幕后styled处理要保留的所有内容 您的样式是一致的和冗余的,并在您出现格式错误时通知您。

styled是由于在使用其他混淆了 用户空间设计空间。用户应该可以成为user用户,而隐藏 一个简单的用户界面后面的实现,它方便了用户的任务。这就是我一直想做的。如果我有 没能做到这一点请告诉我。我相信我们能一起想出更好的办法。

开始

要从PyPI获取它,请使用

pip install styled

最好在虚拟环境中进行。

# anaconda/miniconda
conda create -n styled python
source activate styled
pip install styled

# virtualenv
virtualenv /path/to/env/styled -p /path/to/python
source /path/to/envs/styled/bin/activate
pip install styled

使用styled很容易。在python控制台中试试这个。

>>>fromstyledimportStyled>>>s=Styled("We are [[ 'bold'|bold ]] men!")>>>print(s)Weareboldmen!

可以在构造函数中直接执行字符串格式化

>>>s=Styled("There were up to [[ '{}'|bold:fg-red ]] people who handed over themselves to the \[[ '{police}'|fg-black:bg-red:bold ]].",24,police='policia')>>>print(s)Therewereupto24peoplewhohandedoverthemselvestothepolicia.

您必须用[[ ... ]]限定要设置样式的文本,然后确保以下3(3)条件成立:

  • 用管道(|)将文本与样式分离,
  • 用一对单('...')或双("...")引号引用文本部分,然后
  • 用冒号分隔每个样式(:

有三种类型的样式:

  • 文本样式化,例如boldblinkunderline等。
  • 前景色,例如fg-red
  • 背景色,例如bg-blue

如果要设置扩展文本的样式,可以使用特殊的附加样式标记(no-end),指示 尽可能长时间地继续造型。如果您希望设置长文本的样式,这将非常有用。

>>>s=Styled("There were up to [[ '{}'|bold:fg-red:no-end ]] people who handed over themselves to the \[[ '{police}'|fg-black:bg-red:bold ]].",24,police='policia')>>>print(s)Therewereupto24peoplewhohandedoverthemselvestothepolicia.

上面的示例将包含所有文本,直到下一个标记受到红色前景样式的影响。你也可以 使用yes-end作为样式手动强制执行结束标记。默认情况下,所有样式标记都将在 要设置样式的文本。因此,例如

>>># an example of a terminating end marker>>>s=Styled("There were up to [[ '{}'|bold:fg-red:no-end ]] people who handed over themselves [[ ''|yes-end ]] to the \[[ '{police}'|fg-black:bg-red:bold ]].",24,police='policia')>>>print(s)Therewereupto24peoplewhohandedoverthemselvestothepolicia.

将在单词“selfs”的结尾处挂起红色前景。

你只能有一个前景和一个背景色。忽略这会产生一个StyleError

>>>fromstyledimportStyled>>>s=Styled("There were up to [[ '{}'|bold:fg-red:fg-blue ]] people who handed over themselves to the [[ '{police}'|fg-black:bg-red:bold ]].",24,police='policia')Traceback(mostrecentcalllast):File"/Users/pkorir/miniconda2/envs/styled/lib/python2.7/site-packages/IPython/core/interactiveshell.py",line2878,inrun_codeexec(code_obj,self.user_global_ns,self.user_ns)File"<ipython-input-3-0993b680f88b>",line1,in<module>s=Styled("There were up to [[ '{}'|bold:fg-red:fg-blue ]] people who handed over themselves to the [[ '{police}'|fg-black:bg-red:bold ]].",24,police='policia')File"/Users/pkorir/PycharmProjects/styled/styled/styled.py",line55,in__init__self._validate(self._tokens)File"/Users/pkorir/PycharmProjects/styled/styled/styled.py",line156,in_validateraiseStyleError(u"Multiple foreground styles for text '{}': {}".format(text,', '.join(styles)))StyleError:Multipleforegroundstylesfortext'24':bold,fg-red,fg-blue

输入无效的style也会引发StyleError

>>>s=Styled("This just can't just [[ 'go'|underline ]] on forever! ")Traceback(mostrecentcalllast):File"/Users/pkorir/miniconda2/envs/styled/lib/python2.7/site-packages/IPython/core/interactiveshell.py",line2878,inrun_codeexec(code_obj,self.user_global_ns,self.user_ns)File"<ipython-input-4-528d6d2ac4f4>",line1,in<module>s=Styled("This just can't just [[ 'go'|underline ]] on forever! ")File"/Users/pkorir/PycharmProjects/styled/styled/styled.py",line59,in__init__self._styled=self._transform(self._plain,self._cleaned_tokens)File"/Users/pkorir/PycharmProjects/styled/styled/styled.py",line99,in_transformstyled+=plain[i:start]+self.transform(token)File"/Users/pkorir/PycharmProjects/styled/styled/styled.py",line87,intransformraiseStyleError(u"Unknown style '{}'".format(style_))StyleError:Unknownstyle'underline'

(如果你想知道的话,应该是underlined而不是underline。)

把我们带到样式表。(顺便说一下,这些是从https://gitlab.com/dslackw/colored借来的。)

styles
bold
dim
underlined
blink
reverse
hidden
reset
res_bold
res_dim
res_underlined
res_blink
res_reverse
res_hidden
colours
black
red
green
yellow
blue
magenta
cyan
light_gray
dark_gray
light_red
light_green
light_yellow
light_blue
light_magenta
light_cyan
white
grey_0
navy_blue
dark_blue
blue_3a
blue_3b
blue_1
dark_green
deep_sky_blue_4a
deep_sky_blue_4b
deep_sky_blue_4c
dodger_blue_3
dodger_blue_2
green_4
spring_green_4
turquoise_4
deep_sky_blue_3a
deep_sky_blue_3b
dodger_blue_1
green_3a
spring_green_3a
dark_cyan
light_sea_green
deep_sky_blue_2
deep_sky_blue_1
green_3b
spring_green_3b
spring_green_2a
cyan_3
dark_turquoise
turquoise_2
green_1
spring_green_2b
spring_green_1
medium_spring_green
cyan_2
cyan_1
dark_red_1
deep_pink_4a
purple_4a
purple_4b
purple_3
blue_violet
orange_4a
grey_37
medium_purple_4
slate_blue_3a
slate_blue_3b
royal_blue_1
chartreuse_4
dark_sea_green_4a
pale_turquoise_4
steel_blue
steel_blue_3
cornflower_blue
chartreuse_3a
dark_sea_green_4b
cadet_blue_2
cadet_blue_1
sky_blue_3
steel_blue_1a
chartreuse_3b
pale_green_3a
sea_green_3
aquamarine_3
medium_turquoise
steel_blue_1b
chartreuse_2a
sea_green_2
sea_green_1a
sea_green_1b
aquamarine_1a
dark_slate_gray_2
dark_red_2
deep_pink_4b
dark_magenta_1
dark_magenta_2
dark_violet_1a
purple_1a
orange_4b
light_pink_4
plum_4
medium_purple_3a
medium_purple_3b
slate_blue_1
yellow_4a
wheat_4
grey_53
light_slate_grey
medium_purple
light_slate_blue
yellow_4b
dark_olive_green_3a
dark_green_sea
light_sky_blue_3a
light_sky_blue_3b
sky_blue_2
chartreuse_2b
dark_olive_green_3b
pale_green_3b
dark_sea_green_3a
dark_slate_gray_3
sky_blue_1
chartreuse_1
light_green_2
light_green_3
pale_green_1a
aquamarine_1b
dark_slate_gray_1
red_3a
deep_pink_4c
medium_violet_red
magenta_3a
dark_violet_1b
purple_1b
dark_orange_3a
indian_red_1a
hot_pink_3a
medium_orchid_3
medium_orchid
medium_purple_2a
dark_goldenrod
light_salmon_3a
rosy_brown
grey_63
medium_purple_2b
medium_purple_1
gold_3a
dark_khaki
navajo_white_3
grey_69
light_steel_blue_3
light_steel_blue
yellow_3a
dark_olive_green_3
dark_sea_green_3b
dark_sea_green_2
light_cyan_3
light_sky_blue_1
green_yellow
dark_olive_green_2
pale_green_1b
dark_sea_green_5b
dark_sea_green_5a
pale_turquoise_1
red_3b
deep_pink_3a
deep_pink_3b
magenta_3b
magenta_3c
magenta_2a
dark_orange_3b
indian_red_1b
hot_pink_3b
hot_pink_2
orchid
medium_orchid_1a
orange_3
light_salmon_3b
light_pink_3
pink_3
plum_3
violet
gold_3b
light_goldenrod_3
tan
misty_rose_3
thistle_3
plum_2
yellow_3b
khaki_3
light_goldenrod_2a
light_yellow_3
grey_84
light_steel_blue_1
yellow_2
dark_olive_green_1a
dark_olive_green_1b
dark_sea_green_1
honeydew_2
light_cyan_1
red_1
deep_pink_2
deep_pink_1a
deep_pink_1b
magenta_2b
magenta_1
orange_red_1
indian_red_1c
indian_red_1d
hot_pink_1a
hot_pink_1b
medium_orchid_1b
dark_orange
salmon_1
light_coral
pale_violet_red_1
orchid_2
orchid_1
orange_1
sandy_brown
light_salmon_1
light_pink_1
pink_1
plum_1
gold_1
light_goldenrod_2b
light_goldenrod_2c
navajo_white_1
misty_rose1
thistle_1
yellow_1
light_goldenrod_1
khaki_1
wheat_1
cornsilk_1
grey_100
grey_3
grey_7
grey_11
grey_15
grey_19
grey_23
grey_27
grey_30
grey_35
grey_39
grey_42
grey_46
grey_50
grey_54
grey_58
grey_62
grey_66
grey_70
grey_74
grey_78
grey_82
grey_85
grey_89
grey_93

要查看调色板,请在python shell中运行以下命令

>>>fromstyledimportStyled,COLOURS>>>forcn,cvinCOLOURS.iteritems():...printStyled("Foreground: [[ '{:>30}'|fg-{} ]]\tBackground: [[ '{:>30}'|bg-{} ]]".format(cn,cn,cn,cn))

串联

连接普通字符串和Styled字符串会生成Styled字符串,它是字符串的子类。 不过,在内部,一切都是unicode字符串。

>>>s=Styled("This just can't just [[ 'go'|underlined ]] on forever! ")>>>u="She shouted back!">>>print(type(s+u))<class'styled.styled.Styled'>>>>print(type(u+s))<class'styled.styled.Styled'>>>>s+="Gloria!">>>print(type(s))<class'styled.styled.Styled'>

验证

生成输出的过程包括一些验证,以检查样式是否正确。目前, 只检查多个前色和背景色。这将根据需要扩大。

清洁方式

除了验证之外,还清理样式。清理可以确保最终的样式集是非冗余的。

>>>s=Styled("It takes enormouse [[ 'courage'|bold:bold:bold ]] to admit that you're wrong (Hehehehehehe!)")>>>s._tokens[(19,49,u'courage',[u'bold',u'bold',u'bold'])]>>>s._cleaned_tokens[(19,49,u'courage',[u'bold'])]

抱负?

当我长大后,我想拥有自己的python字符串声明,就像这样:

# hey! I'm a styled strings=s"You have to [[ 'believe'|fg-red ]] it to [[ 'see'|fg-green ]] it!"

特别感谢

致以下人员

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

推荐PyPI第三方库


热门话题
面向java语句的方法与面向表达式的方法   密码学如何在java中为json对象签名?   是否可以通过java程序知道给定卡夫卡消费群体的消费偏移量?   打印字符时出现java未知问号   java为JFrame设置背景色   在ubuntu中检查java版本时linux权限被拒绝   如何用java创建xml模式   java无法在远程服务器上运行Vaadin应用程序   java智能垃圾收集?   java如何在SpringMVC中设置缓存头?   在unix计算机上运行java应用程序a:>签名以输入内容   Java、Apache Commons配置XML属性   使用ArrayList调用Java未经检查的方法   在文本文件中查找并替换单词(Java GUI)   java Android Studio无法检测到JDK7或更新版本   java从socket的有效负载获取事件消息   安卓中java调用子类方法   java如何通过点击超链接来运行jar文件(Firefox)