Skip to content

Instantly share code, notes, and snippets.

@Sasszem
Created July 17, 2018 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sasszem/edf7961fc296f6ba19949d49e6b6fea9 to your computer and use it in GitHub Desktop.
Save Sasszem/edf7961fc296f6ba19949d49e6b6fea9 to your computer and use it in GitHub Desktop.
A terminal control python module, like ComputerCraft's. Based on colorama
"""
Low-level terminal API based on colorama
Uses ASCII escape sequences
"""
import colorama
from colorama.ansi import clear_screen, set_title
from shutil import get_terminal_size
class Term(object):
"Terminal object."
_cursorpos=[0, 0]
_textcolor='RESET'
_bgcolor='RESET'
_pos = lambda self, y, x: '\x1b[%d;%dH' % (y, x)
_clr=clear_screen()
_size=get_terminal_size((80, 24))
def write(self, text):
"Blit text to cursor position, using the textcolor and bgcolor"
print(self._pos(self._cursorpos[0], self._cursorpos[1])+getattr(colorama.Fore, self._textcolor)+getattr(colorama.Back, self._bgcolor)+text+colorama.Style.RESET_ALL)
def clear(self):
"Clear the terminal with bgcolor"
print(getattr(colorama.Back, self._bgcolor)+self._clr)
def reset(self):
"Reset the terminal, moves cursor to (0,0)"
self._cursorpos=[0, 0]
self._textcolor='RESET'
self._bgcolor='RESET'
def getCursorPos(self):
"Get the current cursor position"
return self._cursorpos
def setCursorPos(self, pos):
"Set cursor position"
self._cursorpos=pos
cursorPos=property(getCursorPos, setCursorPos)
cursorPos.__doc__="The current position of the cursor"
def getTextColor(self):
"Get the current textcolor"
return self._textcolor
def setTextColor(self, c):
"Set the color of the text"
self._textcolor=c
textColor=property(getTextColor, setTextColor)
textColor.__doc__="""The color of the text"""
def getBackgroundColor(self):
"Get the background color"
return self._bgcolor
def setBackgroundColor(self, bg):
"Set the color of the background"
self._bgcolor=bg
backgroundColor=property(getBackgroundColor, setBackgroundColor)
backgroundColor.__doc__="The background color"
def _title(self):
"""The title of the terminal window"""
return self._title
def _set_title(self, t):
self._title=t
print(set_title(t))
title=property(_title, _set_title)
colorama.init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment