This release comes with a lot of changes, including a complete rewrite of the `Base` class and a rework of the Customization API. From now on you can mute/unmute styles as well.
**Highlights**
* Rework of Customization API
* Rework of the Base class.
* New Rule type for assigning rules to attributes.
* New Render enum storing render-function names.
* New way of assigning renderers and special call functions in the register-class definition.
* Ability to replace/add render-functions for register-objects
* Ability to mute/unmute register-objects.
Render-functions moved
The default render functions moved from `sty.renderer` to `sty.renderfunc`.
Changes within the customization API
Before `Beta 7` new rules were assigned like this:
python
from sty import fg
Direct attribute assignment
fg.orange = ('rgb', (255, 150, 50))
Attribute assignment via method
fg.set('my_color', 'eightbit', 51)
Since `Beta 7` you do it like this:
python
from sty import fg, Rule, Render
Direct attribute assignment
fg.orange = Rule(Render.rgb_fg, 255, 150, 50)
Attribute assignment via method
fg.set_rule('my_color', Rule(Render.eightbit_fg, 51))
You can see there are a couple of changes.
`set` method renamed to `set_rule`
`fg.set(..` is now `fg.set_rule(...`
The `Rule` type
We now use the `Rule` type (see example above) to set new styling/formatting rules for attributes. This makes for a cleaner `Base` class and a more structured API.
**The `Render` enum**
There is a new `Render` enum (see example above), which provides the renderer names. That means you can select a renderer via the `Render` enum:
python
Rule(Render.rgb_fg, 10, 20, 30)
The old style of selecting render functions by string still works as well:
python
Rule('rgb_fg', 10, 20, 30)
New register-class definition
Before `Beta 7` you could extend the default register classes like this:
python
from sty.register import FgRegister
Extend default Fg register.
class MyFgRegister(FgRegister):
Set a custom render-function
def custom_rgb_fg(self, *args):
return my_custom_rgb_fg_func(*args)
Set render-function for rgb call.
def _rgb_call(self, *args):
return my_custom_rgb_fg_func(*args)
black = ('sgr', 31)
red = ('sgr', 34)
orange = ('custom_rgb_fg', (255, 128, 0))
...
In `Beta 7` you do it like this:
python
import sty
from sty import FgRegister, Rule, Render
Extend the enum with the name of your new render-funtion.
class Render(sty.Render):
custom_rgb_fg = 'custom_rgb_fg'
Extend default Fg register.
class MyFgRegister(FgRegister):
Set a custom render-function
def __init__(self):
super().__init__() Call super to apply render-functions from parent FgRegister.
self.set_renderer(Render.custom_rgb_fg, my_custom_rgb_fg)
Set render-function for rgb call.
rgb_call = Rule(Render.rgb_fg)
black = Rule(Render.sgr, 31)
red = Rule(Render.sgr, 34)
orange = Rule(Render.custom_rgb_bg, 255, 128, 0)
...
As you see in the example above, they way we assign render-functions and the way we set render-functions for the special call methods has changed.
I suggest adding the name of the new renderer to the `Render` enum as shown above.
The new `set_renderer` method
You can now add/change render-functions for each register-object:
python
from sty import fg, Render
def my_custom_renderfunc():
...
fg.set_renderer(Render.rgb_fg, my_custom_renderfunc)
Muting / Silencing / Disabling formatting
This is now possible.
The `mute` and `unmute` methods
Sometimes its useful to disable the formatting for a register-object. You can do so by invoking the `mute` and `unmute` methods:
python
a = fg.red + 'This text is red.' + fg.rs
fg.mute()
b = fg.red + 'This text is NOT red.' + fg.rs
fg.unmute()
c = fg.red + 'This text is red.' + fg.rs
The `mute` and `unmute` batch functions
If you want to mute multiple register-objects at the same time you can use the `mute` and `unmute` functions that you find in `sty.mute`, `sty.unmute`:
python
from sty import fg, bg, ef, rs, mute, unmute
a1 = fg.red + 'This text is red.' + fg.rs
a2 = bg.red + 'This bg is red.' + bg.rs
a3 = ef.italic + 'This text is italic' + ef.rs
mute(fg, bg, ef, rs)
b1 = fg.red + 'This text is NOT red.' + fg.rs
b2 = bg.red + 'This bg is NOT red.' + bg.rs
b3 = ef.italic + 'This text is NOT italic' + ef.rs
unmute(fg, bg, ef, rs)
c1 = fg.red + 'This text is red.' + fg.rs
c2 = bg.red + 'This bg is red.' + bg.rs
c3 = ef.italic + 'This text is italic' + ef.rs