Formatting and code style enhancements have been made to enhance the user experience.
This commit is contained in:
parent
86280a4b55
commit
9167877b29
@ -11,16 +11,16 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|||||||
# c.py: ANSI Escape Sequence colors
|
# c.py: ANSI Escape Sequence colors
|
||||||
|
|
||||||
# 'constants' -- feel free to change at runtime ;P
|
# 'constants' -- feel free to change at runtime ;P
|
||||||
RESET = "\033[0m"
|
RESET = "\x1b[0m"
|
||||||
|
|
||||||
def esc(c):
|
def esc(c):
|
||||||
return f"\033[{c}m"# + str(c) + "m"
|
return f"\x1b[{c}m"# + str(c) + "m"
|
||||||
|
|
||||||
|
|
||||||
def clear(line=True):
|
def clear(line=True):
|
||||||
if line:
|
if line:
|
||||||
return "\033[K"
|
return "\x1b[2K"
|
||||||
return "\033[2J"
|
return "\x1b[2J"
|
||||||
|
|
||||||
# ANSI Colors
|
# ANSI Colors
|
||||||
def c(c, bg=0, i=0):
|
def c(c, bg=0, i=0):
|
||||||
|
26
src/ui.py
26
src/ui.py
@ -12,11 +12,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
ANSI_S = "\033[s"
|
ANSI_S = "\x1b[s"
|
||||||
ANSI_U = "\033[u"
|
ANSI_U = "\x1b[u"
|
||||||
|
|
||||||
CLEAR = "\033[2J"
|
CLEAR = "\x1b[2J"
|
||||||
|
|
||||||
|
SHOW_CURSOR = "\x1b[12?25h"
|
||||||
|
HIDE_CURSOR = "\x1b[8m"
|
||||||
|
|
||||||
# directions
|
# directions
|
||||||
UP = NORTH = 'A'
|
UP = NORTH = 'A'
|
||||||
@ -30,12 +32,12 @@ size = [0,0]
|
|||||||
def init(width, height, x=0, y=0):
|
def init(width, height, x=0, y=0):
|
||||||
size[0], size[1] = width, height
|
size[0], size[1] = width, height
|
||||||
for i in range(y,y+height):
|
for i in range(y,y+height):
|
||||||
uprint(x,i," "*width)
|
uprint(x,i," "*width + "\n")
|
||||||
uprint(x,height+y,f"{m(0, -height)}{ANSI_S}")
|
uprint(x,height+y,f"{m(0, -height)}{ANSI_S}{HIDE_CURSOR}")
|
||||||
clear()
|
clear()
|
||||||
|
|
||||||
def clear(x = 0, y = 0, behavior = 0):
|
def clear(x = 0, y = 0, behavior = 0):
|
||||||
uprint(x, y, f"\x1b[{behavior}J")
|
uprint(x, y, f"\x1b[{behavior}J{SHOW_CURSOR}\n")
|
||||||
|
|
||||||
# ANSI Escape Sequence Generators:
|
# ANSI Escape Sequence Generators:
|
||||||
# unless otherwise specified, return strings containing ANSI escape characters
|
# unless otherwise specified, return strings containing ANSI escape characters
|
||||||
@ -50,26 +52,28 @@ def m(x = 0, y = 0):
|
|||||||
# Move the cursor relatively on the screen in a given direction
|
# Move the cursor relatively on the screen in a given direction
|
||||||
def m_dir(dir, steps=1):
|
def m_dir(dir, steps=1):
|
||||||
if ord('A') <= ord(dir) <= ord('D') :
|
if ord('A') <= ord(dir) <= ord('D') :
|
||||||
return f"\033[{steps}{dir}"
|
return f"\x1b[{steps}{dir}"
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
# Position the cursor absolutely on the screen (0-index)
|
# Position the cursor absolutely on the screen (0-index)
|
||||||
def m_abs(x, y):
|
def m_abs(x, y):
|
||||||
return f"\033[{y+1};{x+1}H"
|
return f"\x1b[{y+1};{x+1}H"
|
||||||
|
|
||||||
# print/input wrappers
|
# print/input wrappers
|
||||||
# uprint: call print at a given x,y position, relative to the cursor
|
# uprint: call print at a given x,y position, relative to the cursor
|
||||||
def uprint(x, y, *args, **kwargs):
|
def uprint(x, y, *args, **kwargs):
|
||||||
print(f"{ANSI_S}{m(x, y)}", end='')
|
print(f"{ANSI_S}{m(x, y)}", end='')
|
||||||
|
sys.stdout.flush()
|
||||||
print(*args, **kwargs, end='')
|
print(*args, **kwargs, end='')
|
||||||
print(ANSI_U, end='')
|
print(f"{ANSI_U}", end='')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
# uinput: call input at a given x,y position, relative to the cursor
|
# uinput: call input at a given x,y position, relative to the cursor
|
||||||
# returns: string containing user input
|
# returns: string containing user input
|
||||||
def uinput(x, y, *args, **kwargs):
|
def uinput(x, y, *args, **kwargs):
|
||||||
print(f"{ANSI_S}{m(x, y)}", end='')
|
print(f"{ANSI_S}{SHOW_CURSOR}{SHOW_CURSOR}{SHOW_CURSOR}{SHOW_CURSOR}{m(x, y)}", end='')
|
||||||
|
sys.stdout.flush()
|
||||||
r = input(*args, **kwargs)
|
r = input(*args, **kwargs)
|
||||||
print(ANSI_U, end='')
|
print(f"{HIDE_CURSOR}{ANSI_U}", end='')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
return r
|
return r
|
||||||
|
Loading…
Reference in New Issue
Block a user