Skip to content

Instantly share code, notes, and snippets.

@saitoha
Created February 3, 2014 12:35
Show Gist options
  • Save saitoha/8783115 to your computer and use it in GitHub Desktop.
Save saitoha/8783115 to your computer and use it in GitHub Desktop.
A test script for ECMA-48 terminals.
#!/usr/bin/env python
import sys, time
def doTest(tests):
for test, comment in tests:
sys.stdout.write(test + "\n")
chars = test.split(" ")
sys.stdout.write("1: ")
for char in chars:
if char == "WAIT":
sys.stdout.flush()
time.sleep(0.5)
sys.stdout.flush()
else:
char = char.replace("CAN", "\x0e")
char = char.replace("SUB", "\x1a")
char = char.replace("ESC", "\x1b")
char = char.replace("SP", " ")
sys.stdout.write(char)
sys.stdout.flush()
sys.stdout.write("\x1b[m\n")
sys.stdout.flush()
sys.stdout.write("2: %s\x1b[m\n\n" % comment)
sys.stdout.flush()
sys.stdout.write("\n")
hr = "----------------------------------------------------"
print hr
print "Test1: various prefix & intermediate bytes"
print hr
tests1 = [
[ "ESC [ m a b c ESC [ m",
"abc" ],
[ "ESC [ 3 1 m a b c",
"\033[31mabc" ],
[ "ESC [ 3 1 ; 1 m a b c",
"\033[31;1mabc" ],
[ "ESC [ > 3 1 m a b c",
"abc" ],
[ "ESC [ ? 3 1 m a b c",
"abc" ],
[ "ESC [ > > < ? 3 1 m a b c",
"abc" ],
[ "ESC [ > > < ? 3 1 SP m a b c",
"abc" ],
[ "ESC [ 3 1 ; 4 4 m a b c",
"\033[31m\033[44mabc" ],
[ "ESC [ 3 1 ; > 4 4 m a b c",
"abc" ],
[ "ESC [ 3 1 ; ; 4 4 m a b c",
"\033[44mabc" ],
[ "ESC [ 3 1 : 4 4 m a b c",
"abc" ],
[ "ESC [ 3 1 : 4 4 > < SP / m a b c",
"abc" ],
[ "ESC [ > > SUB < ? 3 1 SP m a b c",
"<?31 mabc" ],
[ "ESC [ > > ESC [ 3 1 m a b c",
"\033[31mabc" ],
]
doTest(tests1)
print hr
print "Test2: continuous parsing"
print hr
tests2 = [
[ "ESC [ WAIT 3 1 m a b c",
"\033[31mabc" ],
[ "ESC WAIT [ WAIT 3 WAIT 1 WAIT m WAIT \xe3 WAIT \x81 WAIT \x82",
"\033[31m\xe3\x81\x82" ],
[ "ESC WAIT [ 3 1 WAIT m a b c",
"\033[31mabc" ],
[ "ESC [ > > WAIT < ? 3 1 SP m a b c",
"abc" ],
[ "ESC [ > > WAIT < ? 3 1 WAIT SP WAIT m a b c",
"abc" ],
[ "ESC [ 3 WAIT 1 m a b c",
"\033[31mabc" ],
[ "ESC [ 3 1 % WAIT $ m a b c",
"abc" ],
]
doTest(tests2)
print hr
print "Test3: long parameters"
print hr
tests3 = [
[ "ESC [ ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; m",
"" ],
[ "ESC [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; 10 ; 13 ; 14 ; 15 ; 16 ; 17 ; 18 ; 19 ; 20 m",
"" ],
[ "ESC [ 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 ; m",
"" ],
]
doTest(tests3)
print hr
print "Test4: packet overflow tolerance"
print hr
sys.stdout.write("1:");
for x in xrange(0, 100000):
sys.stdout.write("\x1b[;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;m")
sys.stdout.write("\n");
sys.stdout.write("2:\033[m\n\n");
sys.stdout.flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment