2015-12-14 18:03:11 +00:00
|
|
|
|
2018-01-12 15:00:39 +10:00
|
|
|
# Copyright (C) 2015-2018 Andrew Hamilton. All rights reserved.
|
2015-12-14 18:03:11 +00:00
|
|
|
# Licensed under the Artistic License 2.0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
2016-12-03 23:43:03 +01:00
|
|
|
ESC = "\x1b"
|
2018-01-12 15:02:28 +10:00
|
|
|
normal = ESC + "(B\x1b[m"
|
|
|
|
|
bold = ESC + "[1m"
|
|
|
|
|
italic = ESC + "[3m"
|
|
|
|
|
standout = ESC + "[7m"
|
|
|
|
|
underline = ESC + "[4m"
|
|
|
|
|
enter_fullscreen = ESC + "[?1049h"
|
|
|
|
|
exit_fullscreen = ESC + "[?1049l"
|
|
|
|
|
hide_cursor = ESC + "[?25l"
|
|
|
|
|
normal_cursor = ESC + "[?25l\x1b[?25h"
|
|
|
|
|
clear = ESC + "[H\x1b[2J"
|
|
|
|
|
save = ESC + "7"
|
|
|
|
|
restore = ESC + "8"
|
2015-12-14 18:03:11 +00:00
|
|
|
|
|
|
|
|
|
2017-02-08 11:46:28 +01:00
|
|
|
def color(color_number, is_foreground):
|
|
|
|
|
return "\x1b[%s;5;%im" % ("38" if is_foreground else "48", color_number)
|
2015-12-14 18:03:11 +00:00
|
|
|
|
|
|
|
|
|
2017-02-08 11:46:28 +01:00
|
|
|
def rgb_color(rgb, is_foreground):
|
|
|
|
|
return "\x1b[%s;2;" % ("38" if is_foreground else "48") + "%i;%i;%im" % rgb
|
2015-12-14 18:03:11 +00:00
|
|
|
|
|
|
|
|
|
2018-01-12 15:02:28 +10:00
|
|
|
def move(x, y):
|
2016-11-11 22:02:47 +01:00
|
|
|
return "\x1b[%i;%iH" % (y + 1, x + 1)
|
2015-12-14 18:03:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def fullscreen():
|
2016-12-03 13:24:41 +01:00
|
|
|
sys.stdout.write(enter_fullscreen)
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
sys.stdout.write(exit_fullscreen)
|
2015-12-14 18:03:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def hidden_cursor():
|
|
|
|
|
sys.stdout.write(hide_cursor)
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
sys.stdout.write(normal_cursor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def console_title(title):
|
|
|
|
|
sys.stdout.write(save)
|
|
|
|
|
sys.stdout.write("\033]0;%s\007" % title)
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
sys.stdout.write(restore)
|