Skip to content

Instantly share code, notes, and snippets.

@josephernest
Last active October 4, 2022 01:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephernest/de1069bb17a95db7d60e to your computer and use it in GitHub Desktop.
Save josephernest/de1069bb17a95db7d60e to your computer and use it in GitHub Desktop.
"""
zeroterm is a light weight terminal allowing both:
* lines written one after another (normal terminal/console behaviour)
* fixed position text
Note: Requires an ANSI terminal. For Windows 7, please download https://github.com/downloads/adoxa/ansicon/ansi160.zip and run ansicon.exe -i to install it.
"""
from sys import stdout
import time
class zeroterm:
def __init__(self, nrow=24, ncol=50): # nrow, ncol determines the size of the scrolling (=normal terminal behaviour) part of the screen
stdout.write("\x1b[2J") # clear screen
self.nrow = nrow
self.ncol = ncol
self.buf = []
def write(self, s, x=None, y=None): # if no x,y specified, normal console behaviour
if x is not None and y is not None: # if x,y specified, fixed text position
self.movepos(x,y)
print s
else:
if len(self.buf) < self.nrow:
self.buf.append(s)
else:
self.buf[:-1] = self.buf[1:]
self.buf[-1] = s
for i, r in enumerate(self.buf):
self.movepos(i+1,0)
print r[:self.ncol].ljust(self.ncol)
def movepos(self, row, col):
stdout.write("\x1b[%d;%dH" % (row, col))
if __name__ == '__main__':
# An exemple
t = zeroterm()
t.write('zeroterm', 1, 60)
for i in range(1000):
t.write(time.strftime("%H:%M:%S"), 3, 60)
t.write("Hello %i" % i)
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment