def get_input(): buf = [] while True: msg = ''.join(buf) matches = display_suggestion(msg) W.gotoxy(0, C.height - 1) W.clreol() W.textcolor(W.GREEN) W.cputs(game.state.current_node.command_prompt + '$ '), W.textcolor(W.LIGHTGREEN) W.cputs(msg) if matches and len(matches) == 1: #show the argument help text match = matches[0] x = W.wherex() W.textcolor(W.DARKGREY) splitted = match.arguments.split() splitted = splitted[max(0, len(msg.split())-2):] W.cputs(' ' + ' '.join(splitted)) W.textcolor(W.LIGHTGREEN) W.gotoxy(x, C.height - 1) else: match = None #Read input W.setcursortype(1) (chn, chs) = W.getch() W.setcursortype(0) #figure out if we're done if chs == '\r': #enter, exit break if chn == 8: #backspace if len(buf): buf.pop() else: MessageBeep() continue if chn == 3: log.debug('took a ctrl-c') game.fire('specialkey', 'ctrlc') break if chn == 0 or chn == 224: #special keys come in two parts (chn2, _) = W.getch() if chn2 in W.__keydict: game.fire('specialkey', W.__keydict[chn2]) continue if len(buf) >= C.width: #way too long now break if chs not in string.printable: #dont care continue buf.append(chs) return buf, match
def do_prompt(): command_line, match = get_input() game.fire('command', command_line, match)