def fullscreen_winch_with_input(): """ Monitors user input as well as screen size and acknowledges changes to both. """ print('this should be just off-screen') w = FullscreenWindow(sys.stdout) def sigwinch_handler(signum, frame): print('sigwinch! Changed from %r to %r' % ((rows, columns), (w.height, w.width))) signal.signal(signal.SIGWINCH, sigwinch_handler) with w: with Cbreak(sys.stdin): for e in input.Input(): rows, columns = w.height, w.width a = [ fmtstr( (('.%sx%s.%r.' % (rows, columns, e)) * rows)[:columns]) for row in range(rows) ] w.render_to_terminal(a) if e == u'<ESC>': break
def fullscreen_winch_with_input(): print('this should be just off-screen') w = FullscreenWindow(sys.stdout) def sigwinch_handler(signum, frame): print('sigwinch! Changed from %r to %r' % ((rows, columns), (w.height, w.width))) signal.signal(signal.SIGWINCH, sigwinch_handler) with w: with Cbreak(sys.stdin): for e in input.Input(): rows, columns = w.height, w.width a = [fmtstr((('.%sx%s.%r.' % (rows, columns, e)) * rows)[:columns]) for row in range(rows)] w.render_to_terminal(a)
def main(): MAX_FPS = 20 time_per_frame = lambda: 1. / MAX_FPS with FullscreenWindow() as window: with Input() as input_generator: snake = Snake(window.height, window.width) while True: c = None t0 = time.time() while True: t = time.time() temp_c = input_generator.send( max(0, t - (t0 + time_per_frame()))) if temp_c == '<ESC>': return elif temp_c == '+': MAX_FPS += 1 elif temp_c == '-': MAX_FPS = max(1, MAX_FPS - 1) elif temp_c is not None: c = temp_c # save this keypress to be used on next tick if time_per_frame() < t - t0: break if snake.tick(c): return window.render_to_terminal(snake.render())
def run_ui(): with FullscreenWindow() as window: with Input() as input_generator: root = OverlayLayout(window) #g1 = Frame(root) #menu3 = HFill(g1, height=5) g2 = Frame(root, VStackLayout(), border=True, opaque=False, title="Hallo World") a = VFill(g2, width=20) b = VFill(g2, width=None) c = VFill(g2, width=30) root.render() for c in input_generator: if c == u'<ESC>': break if c == u't': b.tangible = not b.tangible if c == u'v': b.visible = not b.visible root.render()
def main(): """Sets speed for snake and begins game upon receiving input from user""" MAX_FPS = 4 time_per_frame = lambda: 1. / MAX_FPS input("Press enter to start") with FullscreenWindow() as window: with Input() as input_generator: snake = Snake(window.height, window.width) while True: c = None t0 = time.time() while True: t = time.time() temp_c = input_generator.send(max(0, t - (t0 + time_per_frame()))) if temp_c == '<ESC>': return elif temp_c == '+': MAX_FPS += 1 elif temp_c == '-': MAX_FPS = max(1, MAX_FPS - 1) elif temp_c is not None: c = temp_c # save this keypress to be used on next tick if time_per_frame() < t - t0: break if snake.tick(c): return window.render_to_terminal(snake.render())
class Starfield(): def __init__(self): self.screen = FullscreenWindow() self.array = FSArray(self.screen.height, self.screen.width) def listen(self): with Input() as input_generator: for c in input_generator: if c == '<ESC>': break elif c == '<SPACE>': self.populate() def populate(self): numstars = int((self.screen.height * self.screen.width) / 10) for i in range(numstars): num = random.choice(range(2)) if num == 1: self.make_star() else: self.make_fancy_star() sleep(1) def color(self): return random.choice([cyan, blue, dark, green, magenta, gray, red,yellow]) def random_row_and_column(self): dimensions = [self.screen.height, self.screen.width] return list(map(lambda x: random.choice(range(x)), dimensions)) def place_with_random_color(self, row, column, character): color = self.color() if row < self.array.height and column < self.array.width: self.array[row, column] = [color('*')] self.screen.render_to_terminal(self.array) def make_fancy_star(self): row, column = self.random_row_and_column() self.place_with_random_color(row, column+1, '*') self.place_with_random_color(row+1, column, '*') self.place_with_random_color(row+1, column+1, ':') self.place_with_random_color(row+1, column+2, '*') self.place_with_random_color(row+2, column+1, '*') def make_star(self): row, column = self.random_row_and_column() self.place_with_random_color(row, column, '*')
def main(host, port): # Create a socket client = socket.socket() # Connect client.connect((host, port)) # Set socket not to block? client.setblocking(False) # Create connection object conn = Connection(client) # Store keypresses keypresses = [] with FullscreenWindow() as window: # Input() is from curtsies with Input() as input_generator: while True: # red is stuff at bottom, blue status at top, green 'window' with output # red at bottom a = FSArray(10, 80) in_text = ''.join(keypresses)[:80] a[9:10, 0:len(in_text)] = [red(in_text)] # render does the page in green for i, line in zip(reversed(range(2, 7)), reversed(conn.render())): a[i:i + 1, 0:len(line)] = [line] # Top line text = 'connected to %s:%d' % (host if len(host) < 50 else host[:50] + '...', port) a[0:1, 0:len(text)] = [blue(text)] window.render_to_terminal(a) ready_to_read, _, _ = select.select([conn, input_generator], [], []) for r in ready_to_read: if r is conn: r.on_read() else: e = input_generator.send(0) if e == '<ESC>': return elif e == '<Ctrl-j>': keypresses.append('\n') client.send( (''.join(keypresses)).encode('latin-1')) keypresses = [] elif e == '<SPACE>': keypresses.append(' ') elif e in ('<DELETE>', '<BACKSPACE>'): keypresses = keypresses[:-1] elif e is not None: keypresses.append(e)
def main(): with FullscreenWindow(sys.stdout) as window: with Input(sys.stdin) as input_generator: world = World(width=window.width, height=window.height) window.render_to_terminal(world.get_array()) for c in input_generator: msg = world.process_event(c) if msg: break window.render_to_terminal(world.get_array()) print(msg)
def main(): with FullscreenWindow() as window: print('Press escape to exit') with Input() as input_generator: a = FSArray(window.height, window.width) for c in input_generator: if c == '<ESC>': break elif c == '<SPACE>': a = FSArray(window.height, window.width) else: row = random.choice(range(window.height)) column = random.choice(range(window.width - len(repr(c)))) a[row, column:column + len(repr(c))] = [repr(c)] window.render_to_terminal(a)
def main(): counter = FrameCounter() with FullscreenWindow() as window: print('Press escape to exit') game = SnakeGame(window.height, window.width) with Input() as input_generator: c = None last_c = '<DOWN>' for framenum in itertools.count(0): t0 = time.time() while True: t = time.time() temp_c = input_generator.send( max(0, t - (t0 + time_per_frame))) if temp_c is not None: c = temp_c if c is None: pass elif c == '<ESC>': return elif c == '<UP>' and last_c != '<DOWN>': game.direction = (-1, 0) last_c = '<UP>' elif c == '<DOWN>' and last_c != '<UP>': game.direction = (1, 0) last_c = '<DOWN>' elif c == '<LEFT>' and last_c != '<RIGHT>': game.direction = (0, -1) last_c = '<LEFT>' elif c == '<RIGHT>' and last_c != '<LEFT>': game.direction = (0, 1) last_c = '<RIGHT>' c = None if time_per_frame < t - t0: break fps = 'FPS: %.1f' % counter.fps() a = game.render(isDead=False) # insert death boolean a[0:1, 0:len(fps)] = [fps] game.move() window.render_to_terminal(a) counter.frame()
def main(host, port): client = socket.socket() client.connect((host, port)) client.setblocking(False) conn = Connection(client) keypresses = [] with FullscreenWindow() as window: with Input() as input_generator: while True: a = FSArray(10, 80) in_text = ''.join(keypresses)[:80] a[9:10, 0:len(in_text)] = [red(in_text)] for i, line in zip(reversed(range(2, 7)), reversed(conn.render())): a[i:i + 1, 0:len(line)] = [line] text = 'connected to %s:%d' % (host if len(host) < 50 else host[:50] + '...', port) a[0:1, 0:len(text)] = [blue(text)] window.render_to_terminal(a) ready_to_read, _, _ = select.select([conn, input_generator], [], []) for r in ready_to_read: if r is conn: r.on_read() else: e = input_generator.send(0) if e == '<ESC>': return elif e == '<Ctrl-j>': keypresses.append('\n') client.send( (''.join(keypresses)).encode('latin-1')) keypresses = [] elif e == '<SPACE>': keypresses.append(' ') elif e in ('<DELETE>', '<BACKSPACE>'): keypresses = keypresses[:-1] elif e is not None: keypresses.append(e)
def main(): counter = FrameCounter() with FullscreenWindow() as window: print('Press escape to exit') with Input() as input_generator: a = FSArray(window.height, window.width) c = None for framenum in itertools.count(0): t0 = time.time() while True: t = time.time() temp_c = input_generator.send( max(0, t - (t0 + time_per_frame))) if temp_c is not None: c = temp_c if c is None: pass elif c == '<ESC>': return elif c == '<SPACE>': a = FSArray(window.height, window.width) else: row = random.choice(range(window.height)) column = random.choice(range(window.width - len(c))) a[row:row + 1, column:column + len(c)] = [c] c = None if time_per_frame < t - t0: break row = random.choice(range(window.height)) column = random.choice(range(window.width)) a[row:row + 1, column:column + 1] = [random.choice(".,-'`~")] fps = 'FPS: %.1f' % counter.fps() a[0:1, 0:len(fps)] = [fps] window.render_to_terminal(a) counter.frame()
def display(grid=None, stop=False, pause=0): if not grid: return m = ["-" * 30] for r in grid: mm = "|" for e in r: mm = mm + ' ' + (str(e) if e else ' ') + ' ' mm += '|' m.append(mm) m.append("-" * 30) if stop: m.append("Hit any key to see more") with Input() as input: with FullscreenWindow() as win: win.render_to_terminal(fsarray(m)) if stop: c = input.next() if not pause: time.sleep(pause)
def main(argv): pool = dict((name[:-5], play) for name, play in globals().items() if name.endswith('_play')) faceoff = [human_play, max_play] try: if len(argv) == 1: pass elif len(argv) == 2: faceoff[1] = pool[argv[1]] elif len(argv) == 3: faceoff = [pool[argv[1]], pool[argv[2]]] else: raise KeyError except KeyError: print("Usage: %s [player] [player]" % argv[0]) print("where a player is one of:", ', '.join(sorted(pool))) return 1 else: with Input() as i: with FullscreenWindow() as w: tictactoe(w, i, *faceoff) return 0
def main(): with Input() as input: with FullscreenWindow() as window: b = Board() while True: window.render_to_terminal(b.display()) if b.turn == 9 or b.winner(): c = input.next() # hit any key sys.exit() while True: c = input.next() if c == '': sys.exit() try: if int(c) in range(9): b = b.move(int(c)) except ValueError: continue window.render_to_terminal(b.display()) break if b.turn == 9 or b.winner(): c = input.next() # hit any key sys.exit() b = ai(b, 'o')
elif c == "a": a = [fmtstr(c*columns) for _ in range(rows)] elif c == "s": a = [fmtstr(c*columns) for _ in range(rows-1)] elif c == "d": a = [fmtstr(c*columns) for _ in range(rows+1)] elif c == "f": a = [fmtstr(c*columns) for _ in range(rows-2)] elif c == "q": a = [fmtstr(c*columns) for _ in range(1)] elif c == "w": a = [fmtstr(c*columns) for _ in range(1)] elif c == "e": a = [fmtstr(c*columns) for _ in range(1)] elif c == "c": w.write(w.t.move(w.t.height-1, 0)) w.scroll_down() elif isinstance(c, events.WindowChangeEvent): a = w.array_from_text("window just changed to %d rows and %d columns" % (c.rows, c.columns)) elif c == '\x0c': # ctrl-L [w.write('\n') for _ in range(rows)] continue else: a = w.array_from_text("unknown command") w.render_to_terminal(a) if __name__ == '__main__': logging.basicConfig(filename='display.log',level=logging.DEBUG) array_size_test(FullscreenWindow(sys.stdout))
def main() -> None: with FullscreenWindow() as window: game = Game(window) game.play()
from __future__ import unicode_literals # convenient for Python 2 import random from curtsies import FullscreenWindow, Input, FSArray from curtsies.fmtfuncs import red, bold, green, on_blue, yellow print(yellow('this prints normally, not to the alternate screen')) with FullscreenWindow() as window: with Input() as input_generator: msg = red(on_blue(bold('Press escape to exit'))) a = FSArray(window.height, window.width) a[0:1, 0:msg.width] = [msg] window.render_to_terminal(a) for c in input_generator: if c == '<ESC>': break elif c == '<SPACE>': a = FSArray(window.height, window.width) else: s = repr(c) row = random.choice(range(window.height)) column = random.choice(range(window.width-len(s))) color = random.choice([red, green, on_blue, yellow]) a[row, column:column+len(s)] = [color(s)] window.render_to_terminal(a)
def main(): with FullscreenWindow(sys.stdout) as window: do_introduction(window) mainloop(window)
def loop(self): if self.history_file: with open(self.history_file, 'a+') as f: f.seek(0) self.history = f.read().split('\n') + self.history self.new_history = len(self.history) self.wait_completion = False self.line = 0 self.column = 0 with FullscreenWindow() as window: with Input() as input_generator: self.a = FSArray(window.height, window.width) intro_lines = self.intro.split('\n') for i, line in enumerate(intro_lines): self.write_xy(i, 0, line) self.write_xy(self.line + 1, 0, self.prompt) window.render_to_terminal(self.a) # intro and prompt text = '' begin_line = self.line begin_column = self.column in_history = False for c in input_generator: if self.wait_completion: self.wait_completion = False # print(c) # continue if c == '<Ctrl-j>': if in_history: text = self.history[history_index] in_history = False if self.read_until in [CR, NL]: self.history.append(text) self.print_result(self.evaluate(text)) self.write_xy(self.line + 1, 0, self.prompt) elif self.complete_on in [CR, NL]: self.offer_completions(text) else: text += '\n' #tokens.append(token) self.write_xy(self.line + 1, 0, self.prompt) begin_line = self.line begin_column = self.column elif c == '<UP>': if not in_history: history_index = len(self.history) - 1 in_history = True self.write_xy( begin_line, begin_column, self.highlight_text(self.history[history_index])) if history_index > 0: history_index -= 1 elif c == '<DOWN>': if in_history: history_index += 1 if history_index > len(self.history) - 1: in_history = False self.write_xy(begin_line, begin_column, self.highlight_text(text)) else: self.write_xy( begin_line, begin_column, self.highlight_text( self.history[history_index])) elif c == '<LEFT>': if self.column > len(self.prompt): self.column -= 1 elif c == '<RIGHT>': if self.column < len(self.prompt) + len(text): self.column += 1 elif c == '<BACKSPACE>': if self.column > len(self.prompt): text = text[:-1] self.write_xy(begin_line, begin_column, self.highlight_text(text)) elif c == '<Ctrl-D>': if self.history_file: with open(self.history_file, 'a+') as f: f.write('\n'.join( self.history[self.new_history:]) + '\n') exit(0) elif c in ['<SPACE>', '<TAB>'] or len(c) == 1: if in_history: text = self.history[history_index] in_history = False if self.complete_on == c: self.offer_completions(text + c) elif self.read_until == c: self.history.append(text) self.print_result(self.evaluate(text)) self.write_xy(self.line + 1, 0, self.prompt) begin_line = self.line begin_column = self.column else: text += c if len(c) == 1 else { '<SPACE>': ' ', '<TAB>': '\t' }[c] if c[0].isalnum(): self.write_xy(self.line, self.column, c) else: self.write_xy(begin_line, begin_column, self.highlight_text(text)) window.render_to_terminal(self.a)
def __init__(self): self.screen = FullscreenWindow() self.array = FSArray(self.screen.height, self.screen.width)