fsxNet Wiki

BBS Development & Resources

User Tools

Site Tools


programming:pycrt

PyCrt

This is a basic unit, that contains functions for console/terminal manipulation, with ANSI codes. With it, you can move the cursor, clear the screen, clear to the end of the line, place the cursor in a specific location and also ANSI colors.

pycrt.py
import time
import os
import sys
 
class colors:
    #Colors class:
    #reset all colors with colors.reset
    #two subclasses fg for foreground and bg for background.
    #use as colors.subclass.colorname.
    #i.e. colors.fg.red or colors.bg.green
    #also, the generic bold, disable, underline, reverse, strikethrough,
    #and invisible work with the main class
    #i.e. colors.bold
 
    reset='\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    class fg:
        black='\033[30m'
        red='\033[31m'
        green='\033[32m'
        orange='\033[33m'
        blue='\033[34m'
        purple='\033[35m'
        cyan='\033[36m'
        lightgrey='\033[37m'
        darkgrey='\033[90m'
        lightred='\033[91m'
        lightgreen='\033[92m'
        yellow='\033[93m'
        lightblue='\033[94m'
        pink='\033[95m'
        lightcyan='\033[96m'
    class bg:
        black='\033[40m'
        red='\033[41m'
        green='\033[42m'
        orange='\033[43m'
        blue='\033[44m'
        purple='\033[45m'
        cyan='\033[46m'
        lightgrey='\033[47m'
 
 
NextLine = '\033[{n}E'
PrevLine = '\033[{n}F'
 
def up(n):
    sys.stdout.write('\033['+str(n)+'A')
    sys.stdout.flush()
 
def down(n):
    sys.stdout.write('\033['+str(n)+'B')
    sys.stdout.flush() 
 
def left(n):
    sys.stdout.write('\033['+str(n)+'D')
    sys.stdout.flush() 
 
def right(n):
    sys.stdout.write('\033['+str(n)+'C')
    sys.stdout.flush() 
 
def clrscr():
    sys.stdout.write('\033[2J')
#   n=0 clears from cursor until end of screen,
#   n=1 clears from cursor to beginning of screen
#   n=2 clears entire screen
    sys.stdout.flush()
 
def clreol():
    sys.stdout.write('\033[0K')
#   n=0 clears from cursor to end of line
    sys.stdout.flush()
 
def clrsol():
    sys.stdout.write('\033[1K')
#   n=1 clears from cursor to start of line
    sys.stdout.flush()
 
 
def clrline():
    sys.stdout.write('\033[2K')
#   n=2 clears entire line
    sys.stdout.flush()    
 
def ansi_on():
    sys.stdout.write('\033(U\033[0m')
    sys.stdout.flush()
 
def gotox(i):
    sys.stdout.write('\033['+str(i)+'G')
    sys.stdout.flush()
 
def gotoxy(x,y):
    sys.stdout.write('\033['+str(y)+';'+str(x)+'H')
    sys.stdout.flush()
 
def clear():
    os.system('cls' if os.name == 'nt' else 'clear')
programming/pycrt.txt · Last modified: 2018/03/29 01:58 (external edit)