move make_screen into a method

This commit is contained in:
Michael Hudson-Doyle 2019-12-20 12:46:43 +13:00
parent 21b7556dc1
commit 039b5313c4
1 changed files with 31 additions and 32 deletions

View File

@ -149,37 +149,6 @@ def make_palette(colors, styles, ascii):
return urwid_palette
def make_screen(colors, is_linux_tty, ascii_mode):
"""Return a screen to be passed to MainLoop.
colors is a list of exactly 8 tuples (name, (r, g, b)), the same as
passed to make_palette.
"""
# On the linux console, we overwrite the first 8 colors to be those
# defined by colors. Otherwise, we return a screen that uses ISO
# 8613-3ish codes to display the colors.
if len(colors) != 8:
raise Exception(
"make_screen must be passed a list of exactly 8 colors")
if is_linux_tty:
# Perhaps we ought to return a screen subclass that does this
# ioctl-ing in .start() and undoes it in .stop() but well.
curpal = bytearray(16*3)
fcntl.ioctl(sys.stdout.fileno(), GIO_CMAP, curpal)
for i in range(8):
for j in range(3):
curpal[i*3+j] = colors[i][1][j]
fcntl.ioctl(sys.stdout.fileno(), PIO_CMAP, curpal)
return urwid.raw_display.Screen()
elif ascii_mode:
return urwid.raw_display.Screen()
else:
_urwid_name_to_rgb = {}
for i, n in enumerate(urwid_8_names):
_urwid_name_to_rgb[n] = colors[i][1]
return TwentyFourBitScreen(_urwid_name_to_rgb)
def extend_dec_special_charmap():
urwid.escape.DEC_SPECIAL_CHARMAP.update({
ord('\N{BLACK RIGHT-POINTING SMALL TRIANGLE}'): '>',
@ -614,9 +583,39 @@ class Application:
controller_index = i
return controller_index
def make_screen(self):
"""Return a screen to be passed to MainLoop.
colors is a list of exactly 8 tuples (name, (r, g, b)), the same as
passed to make_palette.
"""
# On the linux console, we overwrite the first 8 colors to be those
# defined by colors. Otherwise, we return a screen that uses ISO
# 8613-3ish codes to display the colors.
if len(self.COLORS) != 8:
raise Exception(
"make_screen must be passed a list of exactly 8 colors")
if self.is_linux_tty:
# Perhaps we ought to return a screen subclass that does this
# ioctl-ing in .start() and undoes it in .stop() but well.
curpal = bytearray(16*3)
fcntl.ioctl(sys.stdout.fileno(), GIO_CMAP, curpal)
for i in range(8):
for j in range(3):
curpal[i*3+j] = self.COLORS[i][1][j]
fcntl.ioctl(sys.stdout.fileno(), PIO_CMAP, curpal)
return urwid.raw_display.Screen()
elif self.opts.ascii:
return urwid.raw_display.Screen()
else:
_urwid_name_to_rgb = {}
for i, n in enumerate(urwid_8_names):
_urwid_name_to_rgb[n] = self.COLORS[i][1]
return TwentyFourBitScreen(_urwid_name_to_rgb)
def run(self):
log.debug("Application.run")
screen = make_screen(self.COLORS, self.is_linux_tty, self.opts.ascii)
screen = self.make_screen()
self.urwid_loop = urwid.MainLoop(
self.ui, palette=self.color_palette, screen=screen,