Console attribute and special drawing characters and functions accessor. Use GetConsoleAttr() to get a global ConsoleAttr object shared by all callers. Use ConsoleAttr() for abstracting multiple consoles. If _out is not associated with a console, or if the console properties cannot be dete
| 207 | |
| 208 | |
| 209 | class ConsoleAttr(object): |
| 210 | """Console attribute and special drawing characters and functions accessor. |
| 211 | |
| 212 | Use GetConsoleAttr() to get a global ConsoleAttr object shared by all callers. |
| 213 | Use ConsoleAttr() for abstracting multiple consoles. |
| 214 | |
| 215 | If _out is not associated with a console, or if the console properties cannot |
| 216 | be determined, the default behavior is ASCII art with no attributes. |
| 217 | |
| 218 | Attributes: |
| 219 | _ANSI_COLOR: The ANSI color control sequence dict. |
| 220 | _ANSI_COLOR_RESET: The ANSI color reset control sequence string. |
| 221 | _csi: The ANSI Control Sequence indicator string, '' if not supported. |
| 222 | _encoding: The character encoding. |
| 223 | ascii: ASCII art. This is the default. |
| 224 | utf8: UTF-8 unicode. |
| 225 | win: Windows code page 437. |
| 226 | _font_bold: The ANSI bold font embellishment code string. |
| 227 | _font_italic: The ANSI italic font embellishment code string. |
| 228 | _get_raw_key: A function that reads one keypress from stdin with no echo. |
| 229 | _out: The console output file stream. |
| 230 | _term: TERM environment variable value. |
| 231 | _term_size: The terminal (x, y) dimensions in characters. |
| 232 | """ |
| 233 | |
| 234 | _CONSOLE_ATTR_STATE = None |
| 235 | |
| 236 | _ANSI_COLOR = { |
| 237 | 'red': '31;1m', |
| 238 | 'yellow': '33;1m', |
| 239 | 'green': '32m', |
| 240 | 'blue': '34;1m' |
| 241 | } |
| 242 | _ANSI_COLOR_RESET = '39;0m' |
| 243 | |
| 244 | _BULLETS_UNICODE = ('▪', '◆', '▸', '▫', '◇', '▹') |
| 245 | _BULLETS_WINDOWS = ('■', '≡', '∞', 'Φ', '·') # cp437 compatible unicode |
| 246 | _BULLETS_ASCII = ('o', '*', '+', '-') |
| 247 | |
| 248 | def __init__(self, encoding=None, suppress_output=False): |
| 249 | """Constructor. |
| 250 | |
| 251 | Args: |
| 252 | encoding: Encoding override. |
| 253 | ascii -- ASCII art. This is the default. |
| 254 | utf8 -- UTF-8 unicode. |
| 255 | win -- Windows code page 437. |
| 256 | suppress_output: True to create a ConsoleAttr that doesn't want to output |
| 257 | anything. |
| 258 | """ |
| 259 | # Normalize the encoding name. |
| 260 | if not encoding: |
| 261 | encoding = self._GetConsoleEncoding() |
| 262 | elif encoding == 'win': |
| 263 | encoding = 'cp437' |
| 264 | self._encoding = encoding or 'ascii' |
| 265 | self._term = '' if suppress_output else os.getenv('TERM', '').lower() |
| 266 |