/ termlib / attr.py
attr.py
 1  class Attr:
 2      '''
 3      Terminal output attributes.
 4      '''
 5      def fg(r, g, b):
 6          return f'\x1b[38;2;{r};{g};{b}m'
 7  
 8      def bg(r, g, b):
 9          return f'\x1b[48;2;{r};{g};{b}m'
10  
11      def fg_hex(color):
12          if color[0] == '#':
13              color = color[1:]
14          assert(len(color) == 6)
15          return Attr.fg(int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16))
16  
17      def bg_hex(color):
18          if color[0] == '#':
19              color = color[1:]
20          assert(len(color) == 6)
21          return Attr.bg(int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16))
22  
23      def close(attr):
24          # Close an attribute (restore terminal to "neutral")
25          return Attr.RESET
26  
27      RESET = '\x1b[0m'
28      BOLD = '\033[1m'
29      CLEAR = '\x1b[H\x1b[J\x1b[3J'
30      UNDERLINE = '\x1b[4m'
31      REVERSE = '\033[7m'