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]:
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]:
__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)
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)
In [9]:
pureRed=RGBColor("PureRed", 0xFF, bg=True)
prs=RichStr("Pure red", sheet=pureRed)
print(repr(str(prs)))
print(prs)
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]:
In [12]:
print(g)
In [13]:
print(g.toHTML())
In [14]:
IPython.display.display_html(g.toHTML(), raw=True)
In [15]:
g.sheetRepr()
Out[15]:
with RichStr.optimizedCodeRepr you can get optimized code sequence in a machine-readable form
In [16]:
g.optimizedCodeRepr()
Out[16]: