Skip to content

Instantly share code, notes, and snippets.

@alanduan
Created April 11, 2016 23:03
Show Gist options
  • Save alanduan/cc83e9eb69f5b517308c4157a11193ef to your computer and use it in GitHub Desktop.
Save alanduan/cc83e9eb69f5b517308c4157a11193ef to your computer and use it in GitHub Desktop.
from itertools import product
class ConsoleDisplay(object):
def __init__(self, win, rows, cols):
win.resize(rows, cols)
self._win = win
self.rows = rows
self.cols = cols
def refresh(self, vram):
for y, x in product(xrange(self.rows), xrange(self.cols)):
self.set_char(y, x, vram[y * self.cols + x])
self._win.refresh()
def set_char(self, y, x, char):
if char == '\x00':
char = '\x20'
self._win.insstr(y, x, char)
import os
import curses
import mmap
import time
import logging
logging.basicConfig(filename='console.log', level=logging.DEBUG)
def run():
def func(stdscr, rows, cols, ram_file_path, refresh_interval):
try:
console = ConsoleDisplay(stdscr, rows, cols)
with open(ram_file_path, 'wb') as f:
f.seek(rows * cols - 1)
f.write('\x00')
with open(ram_file_path, 'rb') as f:
vram = mmap.mmap(fileno=f.fileno(), length=rows * cols, prot=mmap.PROT_READ)
while True:
console.refresh(vram)
# without sleep, it can loop for 730/sec on my mac book pro 2014
time.sleep(refresh_interval)
logging.info('count: %d' % count)
except:
logging.exception('Exception:')
rows, cols = 25, 80
console_ram_file = 'console.ram'
refresh_interval = 0.1
curses.wrapper(func, rows, cols, console_ram_file, refresh_interval)
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment