Fixed prompt sometimes not clearing.

This commit is contained in:
Michael Lazar
2015-12-08 00:28:55 -08:00
parent 8820ecdafe
commit 9d2a6af826
2 changed files with 28 additions and 12 deletions

View File

@@ -411,12 +411,19 @@ class Terminal(object):
n_rows, n_cols = self.stdscr.getmaxyx()
attr = curses.A_BOLD | Color.CYAN
prompt = self.clean(prompt, n_cols - 1)
window = self.stdscr.derwin(
1, n_cols - len(prompt), n_rows - 1, len(prompt))
window.attrset(attr)
self.add_line(self.stdscr, prompt, n_rows-1, 0, attr)
self.stdscr.refresh()
prompt = self.clean(prompt, n_cols-1)
# Create a new window to draw the text at the bottom of the screen,
# so we can erase it when we're done.
prompt_win = curses.newwin(1, len(prompt)+1, n_rows-1, 0)
self.add_line(prompt_win, prompt, attr=attr)
prompt_win.refresh()
# Create a separate window for text input
input_win = curses.newwin(1, n_cols-len(prompt), n_rows-1, len(prompt))
input_win.attrset(attr)
input_win.refresh()
if key:
curses.curs_set(1)
ch = self.getch()
@@ -426,7 +433,15 @@ class Terminal(object):
text = ch if ch != self.ESCAPE else None
curses.curs_set(0)
else:
text = self.text_input(window)
text = self.text_input(input_win)
prompt_win.clear()
input_win.clear()
del prompt_win
del input_win
self.stdscr.touchwin()
self.stdscr.refresh()
return text
def prompt_y_or_n(self, prompt):

View File

@@ -218,7 +218,7 @@ def test_prompt_input(terminal, stdscr, ascii):
assert isinstance(terminal.prompt_input('hi'), six.text_type)
attr = Color.CYAN | curses.A_BOLD
stdscr.addstr.assert_called_with(39, 0, 'hi'.encode('ascii'), attr)
stdscr.subwin.addstr.assert_called_with(0, 0, 'hi'.encode('ascii'), attr)
assert window.nlines == 1
assert window.ncols == 78
@@ -236,25 +236,26 @@ def test_prompt_y_or_n(terminal, stdscr):
stdscr.getch.side_effect = [ord('y'), ord('N'), terminal.ESCAPE, ord('a')]
attr = Color.CYAN | curses.A_BOLD
text = 'hi'.encode('ascii')
# Press 'y'
assert terminal.prompt_y_or_n('hi')
stdscr.addstr.assert_called_with(39, 0, 'hi'.encode('ascii'), attr)
stdscr.subwin.addstr.assert_called_with(0, 0, text, attr)
assert not curses.flash.called
# Press 'N'
assert not terminal.prompt_y_or_n('hi')
stdscr.addstr.assert_called_with(39, 0, 'hi'.encode('ascii'), attr)
stdscr.subwin.addstr.assert_called_with(0, 0, text, attr)
assert not curses.flash.called
# Press Esc
assert not terminal.prompt_y_or_n('hi')
stdscr.addstr.assert_called_with(39, 0, 'hi'.encode('ascii'), attr)
stdscr.subwin.addstr.assert_called_with(0, 0, text, attr)
assert not curses.flash.called
# Press an invalid key
assert not terminal.prompt_y_or_n('hi')
stdscr.addstr.assert_called_with(39, 0, 'hi'.encode('ascii'), attr)
stdscr.subwin.addstr.assert_called_with(0, 0, text, attr)
assert curses.flash.called