In [1]:
from RichConsole import *
import IPython.display

RichStr consist of pieces of strings and RichStrs


In [2]:
n=RichStr("I am ", "normal")

__repr__esentation of a rich string shows a "flat" representation of a RichStr - a sequence of styles and strings where style applies to everything after it. This is how terminal works. This representation is useful for debugging.


In [3]:
n


Out[3]:
RichStr([Sheet({'Back': Back:reset, 'Fore': Fore:reset, 'Blink': Blink:reset, 'Style': Style:reset}), 'I am ', Sheet({'Back': Back:reset, 'Fore': Fore:reset, 'Blink': Blink:reset, 'Style': Style:reset}), 'normal', Sheet({'Back': Back:reset, 'Blink': Blink:reset, 'Style': Style:reset, 'Fore': Fore:reset})])

To apply a style or a stylesheet you use sheet named argument of RichStr


In [4]:
r=RichStr("RED", sheet=groups["Fore"]["red"])

You can also use dot notation


In [5]:
r=RichStr("RED", sheet=groups.Fore.red)

In [6]:
str(r)


Out[6]:
'\x1b[31mRED\x1b[39m'

__str__ is overloaded, so you can print. Note that the red is not pure red, it is because here it is indexed, indexed colors depend on terminal palete


In [7]:
print(r)


RED

There is a quick and dirty conversion to HTML, but don't use it, it is too unfinished dirty. There are some methods to get CSS rules for some styles.


In [8]:
print(r.toHTML())
IPython.display.display_html(r.toHTML(), raw=True)


<span style='color:#770000'>RED</span>
RED

In [9]:
pureRed=RGBColor("PureRed", 0xFF, bg=True)
prs=RichStr("Pure red", sheet=pureRed)
print(repr(str(prs)))
print(prs)


'\x1b[48;2;255;0;0mPure red\x1b[49m'
Pure red

you can create stylesheets from styles


In [10]:
lightGoldenrod1=RGBColor("lightGoldenrod1", 0xff, 0xff, 0x5f, True)
blackOnGold=Sheet({
	"Back":lightGoldenrod1, # requires 3rd-party libraries
	"Fore":groups.Fore.black
})

g=RichStr(r, " on GOLD", sheet=blackOnGold)

In [11]:
str(g)


Out[11]:
'\x1b[48;2;255;255;95;31mRED\x1b[30m on GOLD\x1b[49;39m'

In [12]:
print(g)


RED on GOLD

In [13]:
print(g.toHTML())


<span style='background-color:#ffff5f;color:#000000'><span style='color:#770000'>RED</span> on GOLD</span>

In [14]:
IPython.display.display_html(g.toHTML(), raw=True)


RED on GOLD

In [15]:
g.sheetRepr()


Out[15]:
[Sheet({'Back': Back:lightGoldenrod1, 'Fore': Fore:red, 'Blink': Blink:reset, 'Style': Style:reset}),
 'RED',
 Sheet({'Back': Back:lightGoldenrod1, 'Blink': Blink:reset, 'Fore': Fore:black, 'Style': Style:reset}),
 ' on GOLD',
 Sheet({'Back': Back:reset, 'Blink': Blink:reset, 'Style': Style:reset, 'Fore': Fore:reset})]

with RichStr.optimizedCodeRepr you can get optimized code sequence in a machine-readable form


In [16]:
g.optimizedCodeRepr()


Out[16]:
[ControlCodes((48, 2, 255, 255, 95, 31)),
 'RED',
 Fore:black,
 ' on GOLD',
 ControlCodes((49, 39))]