Add safety check for terminals without cursor support.

This commit is contained in:
Michael Lazar
2017-01-06 22:47:29 -08:00
parent 0767c8fcab
commit e9cd4c4458
2 changed files with 20 additions and 6 deletions

View File

@@ -57,10 +57,14 @@ def curses_session():
try: try:
curses.start_color() curses.start_color()
except: except:
_logger.warning('Curses failed to initialize color support')
pass pass
# Hide the blinking cursor # Hide the blinking cursor
curses.curs_set(0) try:
curses.curs_set(0)
except:
_logger.warning('Curses failed to initialize the cursor mode')
# Assign the terminal's default (background) color to code -1 # Assign the terminal's default (background) color to code -1
curses.use_default_colors() curses.use_default_colors()

View File

@@ -26,7 +26,6 @@ from . import exceptions
from . import mime_parsers from . import mime_parsers
from .objects import LoadScreen, Color from .objects import LoadScreen, Color
try: try:
# Added in python 3.4+ # Added in python 3.4+
from html import unescape from html import unescape
@@ -122,6 +121,17 @@ class Terminal(object):
""" """
return curses.flash() return curses.flash()
@staticmethod
def curs_set(val):
"""
Change the cursor visibility, may fail for some terminals with limited
cursor support.
"""
try:
curses.curs_set(val)
except:
pass
@staticmethod @staticmethod
def addch(window, y, x, ch, attr): def addch(window, y, x, ch, attr):
""" """
@@ -628,7 +638,7 @@ class Terminal(object):
window.clear() window.clear()
# Set cursor mode to 1 because 2 doesn't display on some terminals # Set cursor mode to 1 because 2 doesn't display on some terminals
curses.curs_set(1) self.curs_set(1)
# Keep insert_mode off to avoid the recursion error described here # Keep insert_mode off to avoid the recursion error described here
# http://bugs.python.org/issue13051 # http://bugs.python.org/issue13051
@@ -656,7 +666,7 @@ class Terminal(object):
except exceptions.EscapeInterrupt: except exceptions.EscapeInterrupt:
out = None out = None
curses.curs_set(0) self.curs_set(0)
return self.strip_textpad(out) return self.strip_textpad(out)
def prompt_input(self, prompt, key=False): def prompt_input(self, prompt, key=False):
@@ -687,13 +697,13 @@ class Terminal(object):
input_win.refresh() input_win.refresh()
if key: if key:
curses.curs_set(1) self.curs_set(1)
ch = self.getch() ch = self.getch()
# We can't convert the character to unicode, because it may return # We can't convert the character to unicode, because it may return
# Invalid values for keys that don't map to unicode characters, # Invalid values for keys that don't map to unicode characters,
# e.g. F1 # e.g. F1
text = ch if ch != self.ESCAPE else None text = ch if ch != self.ESCAPE else None
curses.curs_set(0) self.curs_set(0)
else: else:
text = self.text_input(input_win) text = self.text_input(input_win)