Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from .. import Example
from sty.rendertype import *
print("\n\nCUSTOMIZING\n" + "=" * 80)
# ===== Start =====
Example("customizing the register-objects: assignment")
from sty import fg, ef, rs, Style, RgbFg, Sgr
fg.my_red = Style(RgbFg(255, 0, 0))
a = fg.my_red + 'This text has red fg.' + fg.rs
print(a)
# ===== End =====
# ===== Start =====
Example("customizing the register-objects: compose")
from sty import fg, ef, rs, Style, RgbFg, Sgr
fg.blue_bold = Style(RgbFg(0, 100, 200), Sgr(1))
fg.green_italic = Style(fg.green, ef.i)
a = fg.blue_bold + 'This text has bold blue fg.' + rs.all
b = fg.green_italic + 'This text has italic green fg' + rs.all
def __init__(self):
super().__init__()
self.set_renderfunc(Sgr, renderfunc.sgr)
self.set_renderfunc(EightbitFg, renderfunc.eightbit_fg)
self.set_renderfunc(RgbFg, renderfunc.rgb_fg)
self.set_eightbit_call(EightbitFg)
self.set_rgb_call(RgbFg)
self.red = Style(Sgr(31))
self.green = Style(Sgr(32))
self.rs = Style(Sgr(39))
self.yellow = Style(RgbFg(250, 250, 70))
# ...
# ===== Start =====
Example("gettings started: sty all the strings")
from sty import fg, bg, ef, rs
foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs
# Add custom colors:
from sty import Style, RgbFg
fg.orange = Style(RgbFg(255, 150, 50))
buf = fg.orange + 'Yay, Im orange.' + fg.rs
print(foo, bar, baz, qux, qui, buf, sep='\n')
# ===== End =====
# ===== Start =====
Example("gettings started: select dynamically")
a = ef('italic') + 'Italic text.' + rs.italic
b = fg('blue') + 'Blue text.' + rs.fg
c = bg(randint(0, 254)) + 'Random colored bg' + rs.bg
print(a, b, c, sep='\n')
# ===== End =====
# ===== Start =====
def __init__(self):
super().__init__()
# Add custom style attributes.
self.purple = Style(Sgr(35))
self.blue = Style(Sgr(34))
self.orange = Style(RgbFg(255, 128, 0))
# ...
from sty import fg, RgbFg
fg.set_style('my_red', RgbFg(255, 0, 0))
a = fg.my_red + 'This text has red fg.' + fg.rs
print(a)
# ===== End =====
# ===== Start =====
Example("customizing the register-objects: render_types")
from sty import fg, Sgr, RgbFg, EightbitFg
fg.set_style('my_sgr_red', Sgr(31))
fg.set_style('my_eightbit_red', EightbitFg(196))
fg.set_style('my_rgb_red', RgbFg(255, 0, 0))
a = fg.my_sgr_red + 'This text has a red fg.' + fg.rs
b = fg.my_eightbit_red + 'This text has a red fg.' + fg.rs
c = fg.my_rgb_red + 'This text has a red fg.' + fg.rs
print(a, b, c, sep='\n')
# ===== End =====
# ===== Start =====
Example("customizing the register-objects: get_style")
from sty import ef, fg, rs
# Make fg.green_i render a green and itelic foreground.
fg.set_style('green_i', fg.get_style('green'), ef.get_style('italic'))
# Make rs.green_i reset green and itelic foregrounds.
def __init__(self):
super().__init__()
self.set_renderfunc(Sgr, renderfunc.sgr)
self.set_renderfunc(EightbitFg, renderfunc.eightbit_fg)
self.set_renderfunc(RgbFg, renderfunc.rgb_fg)
self.set_eightbit_call(EightbitFg)
self.set_rgb_call(RgbFg)
self.red = Style(Sgr(31))
self.green = Style(Sgr(32))
self.rs = Style(Sgr(39))
self.yellow = Style(RgbFg(250, 250, 70))
# ...
fg = fg_obj.as_namedtuple()
a = fg.yellow + "I have a yellow fg." + fg.rs
print(a)
# ===== End =====
# ===== Start =====
Example("copy")
from sty import fg as fg_a, RgbFg, FgRegister
fg_b = fg_a
fg_c = fg_a.copy()
fg_a.set_style('orange', RgbFg(240, 100, 100))
a = fg_a.orange + "I have an orange fg." + fg.rs
b = fg_b.orange + "I have an orange fg too." + fg.rs
print(a, b, sep="\n")
try:
msg = "But I have no orange fg, because I'm an earlier copy."
fg_c.orange + msg + fg.rs
except AttributeError:
print(msg)