Style

Fast styling for friendly CLIs.
Note

Styled outputs don’t show in Quarto documentation. Please use a notebook editor to correctly view this page.


source

StyleCode

 StyleCode (name, code, typ)

An escape sequence for styling terminal text.

The primary building block of the S API.

print(str(StyleCode('blue', 34, 'fg')) + 'hello' + str(StyleCode('default', 39, 'fg')) + ' world')
hello world

source

Style

 Style (codes=None)

A minimal terminal text styler.

The main way to use it is via the exported S object.

Exported source
S = Style()

We start with an empty style:

S
<Style: none>

Define a new style by chaining attributes:

s = S.blue.bold.underline
s
<Style: blue bold underline>

You can see a full list of available styles with auto-complete by typing S . Tab.

Apply a style by calling it with a string:

s('hello world')
'\x1b[34m\x1b[1m\x1b[4mhello world\x1b[22m\x1b[24m\x1b[39m'

That’s a raw string with the underlying escape sequences that tell the terminal how to format text. To see the styled version we have to print it:

print(s('hello world'))
hello world

You can also nest styles:

print(S.bold(S.blue('key') + ' = value ') + S.light_gray(' ' + S.underline('# With a comment')) + ' and unstyled text')
key = value  # With a comment and unstyled text
print(S.blue('this '+S.bold('is')+' a test'))
this is a test

source

demo

 demo ()

Demonstrate all available styles and their codes.

demo()
 30    black           
 31    red             
 32    green           
 33    yellow          
 34    blue            
 35    magenta         
 36    cyan            
 37    light_gray      
 39    default         
 90    dark_gray       
 91    light_red       
 92    light_green     
 93    light_yellow    
 94    light_blue      
 95    light_magenta   
 96    light_cyan      
 97    white           
 40    black_bg        
 41    red_bg          
 42    green_bg        
 43    yellow_bg       
 44    blue_bg         
 45    magenta_bg      
 46    cyan_bg         
 47    light_gray_bg   
 49    default_bg      
100    dark_gray_bg    
101    light_red_bg    
102    light_green_bg  
103    light_yellow_bg 
104    light_blue_bg   
105    light_magenta_bg
106    light_cyan_bg   
107    white_bg        
  1    bold            
  2    dim             
  3    italic          
  4    underline       
  5    blink           
  7    invert          
  8    hidden          
  9    strikethrough   
 22    reset_bold      
 22    reset_dim       
 23    reset_italic    
 24    reset_underline 
 25    reset_blink     
 27    reset_invert    
 28    reset_hidden    
 29    reset_strikethrough
  0    reset