added cursor support, examples and bugfixes

This commit is contained in:
filip 2021-03-09 23:26:32 +01:00
parent 9f817ff112
commit 8ddd20203d
9 changed files with 120 additions and 1 deletions

View File

@ -1,10 +1,79 @@
def bell():
print("\u0007")
def cursor_position(**coordinates):
return_string = "\u001b["
try:
return_string += coordinates["row"]
except:
pass
return_string += ";"
try:
return_string += coordinates["column"]
except:
pass
return_string += "H"
return return_string
set_cursor = cursor_position
cup = cursor_position
def move_cursor(direction, *cells):
return_string = "\u001b["
try:
return_string += cells[0]
except:
pass
a = {
"up": "A",
"down": "B",
"forward": "C",
"backward": "D"
}
return_string += a[direction]
return return_string
def erase_in_display(direction):
return_string = "\u001b["
a = {
"forward": "0",
"backward": "1",
"both": "2",
"history": "3"
}
try:
return_string += a[direction]
except:
return_string += str(direction)
return_string += "J"
return return_string
erase_in_display_from_cursor = erase_in_display
ed = erase_in_display
def erase_in_line(direction):
return_string = "\u001b["
a = {
"forward": "0",
"backward": "1",
"both": "2"
}
try:
return_string += a[direction]
except:
return_string += direction
return_string += "K"
return return_string
erase_in_line_from_cursor = erase_in_line
el = erase_in_line
def color(mode, ground, *args):
return_string = "\u001b["
if mode == "bright":
return_string += "1;"
mode = "normal"
if ground == "fg":
return_string += "3"
@ -41,15 +110,27 @@ def color(mode, ground, *args):
bell = "\u0007"
backspace = "\u0008"
escape = "\u001b"
esc = escape
csi = escape + "["
reset = csi + "0m"
normal = reset
reset_formatting = reset
bold = csi + "1m"
faint = csi +"2m"
underline = csi + "4m"
blink = csi + "5m"
invert = csi + "7m"
reverse_video = invert
strike = csi + "9m"
crossed_out = strike
normal_intensity = csi + "22m"
#no_italic = csi + "23m" #not vidley supported
no_blink = csi + "25m"

2
examples/a.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,8 @@
import ansi_escape
a = 0
while a < 10:
a += 1
print("Hello World!!")
a = input()
print(ansi_escape.move_cursor("up", str(a)) + ansi_escape.el("both"))

2
examples/erase.py Normal file
View File

@ -0,0 +1,2 @@
import ansi_escape, os
print(ansi_escape.erase_from_cursor("2"))

7
examples/move_cursor.py Normal file
View File

@ -0,0 +1,7 @@
import keyboard, ansi_escape
print(ansi_escape.erase_from_cursor("both"))
keyboard.add_hotkey('up', ansi_escape.cursor, args=('up'))
keyboard.add_hotkey('down', ansi_escape.cursor, args=('down'))
keyboard.add_hotkey('left', ansi_escape.cursor, args=('backward'))
keyboard.add_hotkey('right', ansi_escape.cursor, args=('forward'))
keyboard.wait()

9
examples/palette.py Normal file
View File

@ -0,0 +1,9 @@
import ansi_escape
color = 0
print(ansi_escape.escape + "]0;256 Color Terminal Palette" + ansi_escape.bell)
while color <= 255:
color = str(color)
print(ansi_escape.color("256", "bg", color) + " " + " " * ( len(color) - 2 ) + color + " ", end="")
color = int(color)
color += 1
input()

View File

@ -0,0 +1,4 @@
import ansi_escape, random
while 1:
print(ansi_escape.set_cursor(row=str(random.randint(1,20)), column=str(random.randint(1,20))) + ansi_escape.color("normal", "bg", "red") + " " + ansi_escape.reset)
input()

View File

@ -6,4 +6,8 @@ while a < 256:
a += 1
print(ansi_escape.escape + "]0;Demo" + ansi_escape.escape + "\\")
input()
print("hello" + ansi_escape.move_cursor("up", "2"))
input()
print(ansi_escape.erase_from_cursor("forward"))
input()

View File

@ -4,4 +4,6 @@ Features:
SGR (Select Graphic Rendition)
SGR foreground and background color in 4-bit color, 8-bit color and 24-bit color
SGR text formating like; underline, overline and bold text
Easy way to use the bell character
Easy way to use the bell character
Move cursor
Clear parts of screen or line