eris/terminal.py

83 lines
1.9 KiB
Python
Raw Normal View History

2015-12-14 18:03:11 +00:00
2016-01-01 17:06:28 +00:00
# Copyright (C) 2015-2016 Andrew Hamilton. All rights reserved.
2015-12-14 18:03:11 +00:00
# Licensed under the Artistic License 2.0.
import contextlib
import sys
_PREFIX = "\x1b"
2015-12-14 18:03:11 +00:00
normal = _PREFIX + "(B\x1b[m" # sgr0 "[0m" ?
bold = _PREFIX + "[1m" # bold
italic = _PREFIX + "[3m" # sitm
standout = _PREFIX + "[7m" # smso
underline = _PREFIX + "[4m" # smul
enter_fullscreen = _PREFIX + "[?1049h" # smcup
exit_fullscreen = _PREFIX + "[?1049l" # rmcup
hide_cursor = _PREFIX + "[?25l" # civis
normal_cursor = _PREFIX + "[?25l\x1b[?25h" # cnorm
clear = _PREFIX + "[H\x1b[2J" # clear
save = _PREFIX + "7" # sc
restore = _PREFIX + "8" # rc
2015-12-14 18:03:11 +00:00
_FG_CODES = ["30", "31", "32", "33", "34", "35", "36", "37",
"90", "91", "92", "93", "94", "95", "96", "97"]
2015-12-14 18:03:11 +00:00
def fg_color(color_number): # setaf
return ("\x1b[38;5;%im" % color_number if color_number > 15
else "\x1b[%sm" % _FG_CODES[color_number])
2015-12-14 18:03:11 +00:00
_BG_CODES = ["40", "41", "42", "43", "44", "45", "46", "47",
"100", "101", "102", "103", "104", "105", "106", "107"]
2015-12-14 18:03:11 +00:00
def bg_color(color_number): # setab
return ("\x1b[48;5;%im" % color_number if color_number > 15
else "\x1b[%sm" % _BG_CODES[color_number])
2015-12-14 18:03:11 +00:00
def fg_rgb_color(rgb):
return "\x1b[38;2;%i;%i;%im" % rgb
def bg_rgb_color(rgb):
return "\x1b[48;2;%i;%i;%im" % rgb
def move(x, y): # cup
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)