-
Notifications
You must be signed in to change notification settings - Fork 1
/
TermEmulator.py
executable file
·974 lines (817 loc) · 32.3 KB
/
TermEmulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# TermEmulator - Emulator for VT100 terminal programs
# Copyright (C) 2008 Siva Chandran P
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Siva Chandran P
# siva.chandran.p@gmail.com
"""
Emulator for VT100 terminal programs.
This module provides terminal emulation for VT100 terminal programs. It handles
V100 special characters and most important escape sequences. It also handles
graphics rendition which specifies text style(i.e. bold, italics), foreground color
and background color. The handled escape sequences are CUU, CUD, CUF, CUB, CHA,
CUP, DECCKM, ED, EL, VPA and SGR.
"""
import sys
import os
import pty
import select
from array import *
CURSOR_UP = 1
CURSOR_DOWN = 2
CURSOR_RIGHT = 3
CURSOR_LEFT = 4
class V102Terminal:
__ASCII_NUL = 0 # Null
__ASCII_BEL = 7 # Bell
__ASCII_BS = 8 # Backspace
__ASCII_HT = 9 # Horizontal Tab
__ASCII_LF = 10 # Line Feed
__ASCII_VT = 11 # Vertical Tab
__ASCII_FF = 12 # Form Feed
__ASCII_CR = 13 # Carriage Return
__ASCII_XON = 17 # Resume Transmission
__ASCII_XOFF = 19 # Stop Transmission or Ignore Characters
__ASCII_ESC = 27 # Escape
__ASCII_SPACE = 32 # Space
__ASCII_CSI = 153 # Control Sequence Introducer
__ESCSEQ_CUU = 'A' # n A: Moves the cursor up n(default 1) times.
__ESCSEQ_CUD = 'B' # n B: Moves the cursor down n(default 1) times.
__ESCSEQ_CUF = 'C' # n C: Moves the cursor forward n(default 1) times.
__ESCSEQ_CUB = 'D' # n D: Moves the cursor backward n(default 1) times.
__ESCSEQ_CHA = 'G' # n G: Cursor horizontal absolute position. 'n' denotes
# the column no(1 based index). Should retain the line
# position.
__ESCSEQ_CUP = 'H' # n ; m H: Moves the cursor to row n, column m.
# The values are 1-based, and default to 1 (top left
# corner).
__ESCSEQ_DECCKM_ON = 'h' # Turns on the application cursor key mode
# translation meaning cursors become 0A,0B,OC,OD
# instead of [A,[B,[C,[D This only works when n is
# is 1
__ESCSEQ_DECCKM_RESET = 'l' # Resets application cursor key mode to the
# standard mapping. It only works when n is 1.
__ESCSEQ_ED = 'J' # n J: Clears part of the screen. If n is zero
# (or missing), clear from cursor to end of screen.
# If n is one, clear from cursor to beginning of the
# screen. If n is two, clear entire screen.
__ESCSEQ_EL = 'K' # n K: Erases part of the line. If n is zero
# (or missing), clear from cursor to the end of the
# line. If n is one, clear from cursor to beginning of
# the line. If n is two, clear entire line. Cursor
# position does not change.
__ESCSEQ_VPA = 'd' # n d: Cursor vertical absolute position. 'n' denotes
# the line no(1 based index). Should retain the column
# position.
__ESCSEQ_SGR = 'm' # n [;k] m: Sets SGR (Select Graphic Rendition)
# parameters. After CSI can be zero or more parameters
# separated with ;. With no parameters, CSI m is treated
# as CSI 0 m (reset / normal), which is typical of most
# of the ANSI codes.
ESC = '\033' # ASCII text for escape sequence
RENDITION_STYLE_BOLD = 1
RENDITION_STYLE_DIM = 2
RENDITION_STYLE_ITALIC = 4
RENDITION_STYLE_UNDERLINE = 8
RENDITION_STYLE_SLOW_BLINK = 16
RENDITION_STYLE_FAST_BLINK = 32
RENDITION_STYLE_INVERSE = 64
RENDITION_STYLE_HIDDEN = 128
CALLBACK_SCROLL_UP_SCREEN = 1
CALLBACK_UPDATE_LINES = 2
CALLBACK_UPDATE_CURSOR_POS = 3
CALLBACK_UPDATE_WINDOW_TITLE = 4
CALLBACK_UNHANDLED_ESC_SEQ = 5
def __init__(self, rows, cols):
"""
Initializes the terminal with specified rows and columns. User can
resize the terminal any time using Resize method. By default the screen
is cleared(filled with blank spaces) and cursor positioned in the first
row and first column.
"""
self.cols = cols
self.rows = rows
self.curX = 0
self.curY = 0
self.ignoreChars = False
self.cursorKeyMapping = None
# special character handlers
self.charHandlers = {
self.__ASCII_NUL:self.__OnCharIgnore,
self.__ASCII_BEL:self.__OnCharIgnore,
self.__ASCII_BS:self.__OnCharBS,
self.__ASCII_HT:self.__OnCharHT,
self.__ASCII_LF:self.__OnCharLF,
self.__ASCII_VT:self.__OnCharLF,
self.__ASCII_FF:self.__OnCharLF,
self.__ASCII_CR:self.__OnCharCR,
self.__ASCII_XON:self.__OnCharXON,
self.__ASCII_XOFF:self.__OnCharXOFF,
self.__ASCII_ESC:self.__OnCharESC,
self.__ASCII_CSI:self.__OnCharCSI,
}
# escape sequence handlers
self.escSeqHandlers = \
{
self.__ESCSEQ_CUU:self.__OnEscSeqCUU,
self.__ESCSEQ_CUD:self.__OnEscSeqCUD,
self.__ESCSEQ_CUF:self.__OnEscSeqCUF,
self.__ESCSEQ_CUB:self.__OnEscSeqCUB,
self.__ESCSEQ_CHA:self.__OnEscSeqCHA,
self.__ESCSEQ_CUP:self.__OnEscSeqCUP,
self.__ESCSEQ_ED:self.__OnEscSeqED,
self.__ESCSEQ_EL:self.__OnEscSeqEL,
self.__ESCSEQ_VPA:self.__OnEscSeqVPA,
self.__ESCSEQ_SGR:self.__OnEscSeqSGR,
self.__ESCSEQ_DECCKM_ON:self.__OnEscSeqDECCKM_ON,
self.__ESCSEQ_DECCKM_RESET:self.__OnEscSeqDECCKM_RESET
}
# currsor key mode mapping
self.standardCursorMapping = \
{
CURSOR_UP: self.ESC + '[' + self.__ESCSEQ_CUU,
CURSOR_DOWN: self.ESC + '[' + self.__ESCSEQ_CUD,
CURSOR_RIGHT: self.ESC + '[' + self.__ESCSEQ_CUF,
CURSOR_LEFT: self.ESC + '[' + self.__ESCSEQ_CUB
}
self.applicationCursorMapping = \
{
CURSOR_UP: self.ESC + 'O' + self.__ESCSEQ_CUU,
CURSOR_DOWN: self.ESC + 'O' + self.__ESCSEQ_CUD,
CURSOR_RIGHT: self.ESC + 'O' + self.__ESCSEQ_CUF,
CURSOR_LEFT: self.ESC + 'O' + self.__ESCSEQ_CUB
}
self.cursorMapping = self.standardCursorMapping
# defines the printable characters, only these characters are printed
# on the terminal
self.printableChars = "0123456789"
self.printableChars += "abcdefghijklmnopqrstuvwxyz"
self.printableChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
self.printableChars += """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ """
self.printableChars += "\t"
# terminal screen, its a list of string in which each string always
# holds self.cols characters. If the screen doesn't contain any
# character then it'll blank space
self.screen = []
# terminal screen rendition, its a list of array of long. The first
# 8 bits of the long holds the rendition style/attribute(i.e. bold,
# italics and etc). The next 4 bits specifies the foreground color and
# next 4 bits for background
self.scrRendition = []
# current rendition
self.curRendition = 0L
# list of dirty lines since last call to GetDirtyLines
self.isLineDirty = []
for i in range(rows):
line = array('c')
rendition = array('L')
for j in range(cols):
line.append(' ')
rendition.append(0)
self.screen.append(line)
self.scrRendition.append(rendition)
self.isLineDirty.append(False)
# initializes callbacks
self.callbacks = {
self.CALLBACK_SCROLL_UP_SCREEN: None,
self.CALLBACK_UPDATE_LINES: None,
self.CALLBACK_UPDATE_CURSOR_POS: None,
self.CALLBACK_UNHANDLED_ESC_SEQ: None,
self.CALLBACK_UPDATE_WINDOW_TITLE: None,
}
# unparsed part of last input
self.unparsedInput = None
def GetRawScreen(self):
"""
Returns the screen as a list of strings. The list will have rows no. of
strings and each string will have columns no. of characters. Blank space
used represents no character.
"""
return self.screen
def GetRawScreenRendition(self):
"""
Returns the screen as a list of array of long. The list will have rows
no. of array and each array will have columns no. of longs. The first
8 bits of long represents rendition style like bold, italics and etc.
The next 4 bits represents foreground color and next 4 bits for
background color.
"""
return self.scrRendition
def GetRows(self):
"""
Returns no. rows in the terminal
"""
return self.rows
def GetCols(self):
"""
Returns no. cols in the terminal
"""
return self.cols
def GetSize(self):
"""
Returns terminal rows and cols as tuple
"""
return (self.rows, self.cols)
def Resize(self, rows, cols):
"""
Resizes the terminal to specified rows and cols.
- If the new no. rows is less than existing no. rows then existing rows
are deleted at top.
- If the new no. rows is greater than existing no. rows then
blank rows are added at bottom.
- If the new no. cols is less than existing no. cols then existing cols
are deleted at right.
- If the new no. cols is greater than existing no. cols then new cols
are added at right.
"""
if rows < self.rows:
# remove rows at top
for i in range(self.rows - rows):
self.isLineDirty.pop(0)
self.screen.pop(0)
self.scrRendition.pop(0)
elif rows > self.rows:
# add blank rows at bottom
for i in range(rows - self.rows):
line = array('c')
rendition = array('L')
for j in range(self.cols):
line.append(' ')
rendition.append(0)
self.screen.append(line)
self.scrRendition.append(rendition)
self.isLineDirty.append(False)
if cols < self.cols:
# remove cols at right
for i in range(self.rows):
self.screen[i] = self.screen[i][:cols - self.cols]
for j in range(self.cols - cols):
self.scrRendition[i].pop(len(self.scrRendition[i]) - 1)
elif cols > self.cols:
# add cols at right
for i in range(self.rows):
for j in range(cols - self.cols):
self.screen[i].append(' ')
self.scrRendition[i].append(0)
self.rows = rows
self.cols = cols
def GetCursorPos(self):
"""
Returns cursor position as tuple
"""
return (self.curY, self.curX)
def Clear(self):
"""
Clears the entire terminal screen
"""
ClearRect(0, 0, self.rows - 1, self.cols - 1)
def ClearRect(self, startRow, startCol, endRow, endCol):
"""
Clears the terminal screen starting from startRow and startCol to
endRow and EndCol.
"""
if startRow < 0:
startRow = 0
elif startRow >= self.rows:
startRow = self.rows - 1
if startCol < 0:
startCol = 0
elif startCol >= self.cols:
startCol = self.cols - 1
if endRow < 0:
endRow = 0
elif endRow >= self.rows:
endRow = self.rows - 1
if endCol < 0:
endCol = 0
elif endCol >= self.cols:
endCol = self.cols - 1
if startRow > endRow:
startRow, endRow = endRow, startRow
if startCol > endCol:
startCol, endCol = endCol, startCol
for i in range(startRow, endRow + 1):
start = 0
end = self.cols - 1
if i == startRow:
start = startCol
elif i == endRow:
end = endCol
for j in range(start, end + 1):
self.screen[i][j] = ' '
self.scrRendition[i][j] = 0
if end + 1 > start:
self.isLineDirty[i] = True
def GetChar(self, row, col):
"""
Returns the character at the location specified by row and col. The
row and col should be in the range 0..rows - 1 and 0..cols - 1.
"""
if row < 0 or row >= self.rows:
return None
if cols < 0 or col >= self.cols:
return None
return self.screen[row][col]
def GetRendition(self, row, col):
"""
Returns the screen rendition at the location specified by row and col.
The returned value is a long, the first 8 bits specifies the rendition
style and next 4 bits for foreground and another 4 bits for background
color.
"""
if row < 0 or row >= self.rows:
return None
if col < 0 or col >= self.cols:
return None
style = self.scrRendition[row][col] & 0x000000ff
fgcolor = (self.scrRendition[row][col] & 0x00000f00) >> 8
bgcolor = (self.scrRendition[row][col] & 0x0000f000) >> 12
return (style, fgcolor, bgcolor)
def GetLine(self, lineno):
"""
Returns the terminal screen line specified by lineno. The line is
returned as string, blank space represents empty character. The lineno
should be in the range 0..rows - 1
"""
if lineno < 0 or lineno >= self.rows:
return None
return self.screen[lineno].tostring()
def GetLines(self):
"""
Returns terminal screen lines as a list, same as GetScreen
"""
lines = []
for i in range(self.rows):
lines.append(self.screen[i].tostring())
return lines
def GetLinesAsText(self):
"""
Returns the entire terminal screen as a single big string. Each row
is seperated by \\n and blank space represents empty character.
"""
text = ""
for i in range(self.rows):
text += self.screen[i].tostring()
text += '\n'
text = text.rstrip("\n") # removes leading new lines
return text
def GetDirtyLines(self):
"""
Returns list of dirty lines(line nos) since last call to GetDirtyLines.
The line no will be 0..rows - 1.
"""
dirtyLines = []
for i in range(self.rows):
if self.isLineDirty[i]:
dirtyLines.append(i)
self.isLineDirty[i] = False
return dirtyLines
def SetCallback(self, event, func):
"""
Sets callback function for the specified event. The event should be
any one of the following. None can be passed as callback function to
reset the callback.
CALLBACK_SCROLL_UP_SCREEN
Called before scrolling up the terminal screen.
CALLBACK_UPDATE_LINES
Called when ever some lines need to be updated. Usually called
before leaving ProcessInput and before scrolling up the
terminal screen.
CALLBACK_UPDATE_CURSOR_POS
Called to update the cursor position. Usually called before leaving
ProcessInput.
CALLBACK_UPDATE_WINDOW_TITLE
Called when ever a window title escape sequence encountered. The
terminal window title will be passed as a string.
CALLBACK_UNHANDLED_ESC_SEQ
Called when ever a unsupported escape sequence encountered. The
unhandled escape sequence(escape sequence character and it
parameters) will be passed as a string.
"""
self.callbacks[event] = func
def ProcessInput(self, text):
"""
Processes the given input text. It detects V100 escape sequences and
handles it. Any partial unparsed escape sequences are stored internally
and processed along with next input text. Before leaving, the function
calls the callbacks CALLBACK_UPDATE_LINE and CALLBACK_UPDATE_CURSOR_POS
to update the changed lines and cursor position respectively.
"""
if text == None:
return
if self.unparsedInput != None:
text = self.unparsedInput + text
self.unparsedInput = None
textlen = len(text)
index = 0
while index < textlen:
ch = text[index]
ascii = ord(ch)
if self.ignoreChars:
index += 1
continue
if ascii in self.charHandlers.keys():
index = self.charHandlers[ascii](text, index)
else:
if ch in self.printableChars:
self.__PushChar(ch)
else:
print "WARNING: Unsupported character %s:%d" % (ch, ascii)
index += 1
# update the dirty lines
if self.callbacks[self.CALLBACK_UPDATE_LINES] != None:
self.callbacks[self.CALLBACK_UPDATE_LINES]()
# update cursor position
if self.callbacks[self.CALLBACK_UPDATE_CURSOR_POS] != None:
self.callbacks[self.CALLBACK_UPDATE_CURSOR_POS]()
def ScrollUp(self):
"""
Scrolls up the terminal screen by one line. The callbacks
CALLBACK_UPDATE_LINES and CALLBACK_SCROLL_UP_SCREEN are called before
scrolling the screen.
"""
# update the dirty lines
if self.callbacks[self.CALLBACK_UPDATE_LINES] != None:
self.callbacks[self.CALLBACK_UPDATE_LINES]()
# scrolls up the screen
if self.callbacks[self.CALLBACK_SCROLL_UP_SCREEN] != None:
self.callbacks[self.CALLBACK_SCROLL_UP_SCREEN]()
line = self.screen.pop(0)
for i in range(self.cols):
line[i] = ' '
self.screen.append(line)
rendition = self.scrRendition.pop(0)
for i in range(self.cols):
rendition[i] = 0
self.scrRendition.append(rendition)
def Dump(self, file=sys.stdout):
"""
Dumps the entire terminal screen into the given file/stdout
"""
for i in range(self.rows):
file.write(self.screen[i].tostring())
file.write("\n")
def TranslateCursor(self, cursor):
"""
Translates CURSOR_UP, etc. events into the proper escape sequence
"""
return self.cursorMapping[cursor]
def __NewLine(self):
"""
Moves the cursor to the next line, if the cursor is already at the
bottom row then scrolls up the screen.
"""
self.curX = 0
if self.curY + 1 < self.rows:
self.curY += 1
else:
self.ScrollUp()
def __PushChar(self, ch):
"""
Writes the character(ch) into current cursor position and advances
cursor position.
"""
if self.curX >= self.cols:
self.__NewLine()
self.screen[self.curY][self.curX] = ch
self.scrRendition[self.curY][self.curX] = self.curRendition
self.curX += 1
self.isLineDirty[self.curY] = True
def __ParseEscSeq(self, text, index):
"""
Parses escape sequence from the input and returns the index after escape
sequence, the escape sequence character and parameter for the escape
sequence
"""
textlen = len(text)
interChars = None
while index < textlen:
ch = text[index]
ascii = ord(ch)
if ascii >= 32 and ascii <= 63:
# intermediate char (32 - 47)
# parameter chars (48 - 63)
if interChars == None:
interChars = ch
else:
interChars += ch
elif ascii >= 64 and ascii <= 125:
# final char
return (index + 1, chr(ascii), interChars)
else:
print "Unexpected characters in escape sequence ", ch
index += 1
# the escape sequence is not complete, inform this to caller by giving
# '?' as final char
return (index, '?', interChars)
def __HandleEscSeq(self, text, index):
"""
Tries to parse escape sequence from input and if its not complete then
puts it in unparsedInput and process it when the ProcessInput called
next time.
"""
if text[index] == '[':
index += 1
index, finalChar, interChars = self.__ParseEscSeq(text, index)
if finalChar == '?':
self.unparsedInput = self.ESC + "["
if interChars != None:
self.unparsedInput += interChars
elif finalChar in self.escSeqHandlers.keys():
self.escSeqHandlers[finalChar](interChars)
else:
escSeq = ""
if interChars != None:
escSeq += interChars
escSeq += finalChar
if self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ] != None:
self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ](escSeq)
elif text[index] == ']':
textlen = len(text)
if index + 2 < textlen:
if text[index + 1] == '0' and text[index + 2] == ';':
# parse title, terminated by bell char(\007)
index += 3 # ignore '0' and ';'
start = index
while index < textlen:
if ord(text[index]) == self.__ASCII_BEL:
break
index += 1
self.__OnEscSeqTitle(text[start:index])
return index
def __OnCharBS(self, text, index):
"""
Handler for backspace character
"""
if self.curX > 0:
self.curX -= 1
return index + 1
def __OnCharHT(self, text, index):
"""
Handler for horizontal tab character
"""
while True:
self.curX += 1
if self.curX % 8 == 0:
break
return index + 1
def __OnCharLF(self, text, index):
"""
Handler for line feed character
"""
self.__NewLine()
return index + 1
def __OnCharCR(self, text, index):
"""
Handler for carriage return character
"""
self.curX = 0
return index + 1
def __OnCharXON(self, text, index):
"""
Handler for XON character
"""
self.ignoreChars = False
return index + 1
def __OnCharXOFF(self, text, index):
"""
Handler for XOFF character
"""
self.ignoreChars = True
return index + 1
def __OnCharESC(self, text, index):
"""
Handler for escape character
"""
index += 1
if index < len(text):
index = self.__HandleEscSeq(text, index)
return index
def __OnCharCSI(self, text, index):
"""
Handler for control sequence intruducer(CSI) character
"""
index += 1
index = self.__HandleEscSeq(text, index)
return index
def __OnCharIgnore(self, text, index):
"""
Dummy handler for unhandler characters
"""
return index + 1
def __OnEscSeqTitle(self, params):
"""
Handler for window title escape sequence
"""
if self.callbacks[self.CALLBACK_UPDATE_WINDOW_TITLE] != None:
self.callbacks[self.CALLBACK_UPDATE_WINDOW_TITLE](params)
def __OnEscSeqCUU(self, params):
"""
Handler for escape sequence CUU
"""
n = 1
if params != None:
n = int(params)
self.curY -= n;
if self.curY < 0:
self.curY = 0
def __OnEscSeqCUD(self, params):
"""
Handler for escape sequence CUD
"""
n = 1
if params != None:
n = int(params)
self.curY += n;
if self.curY >= self.rows:
self.curY = self.rows - 1
def __OnEscSeqCUF(self, params):
"""
Handler for escape sequence CUF
"""
n = 1
if params != None:
n = int(params)
self.curX += n;
if self.curX >= self.cols:
self.curX = self.cols - 1
def __OnEscSeqCUB(self, params):
"""
Handler for escape sequence CUB
"""
n = 1
if params != None:
n = int(params)
self.curX -= n;
if self.curX < 0:
self.curX = 0
def __OnEscSeqCHA(self, params):
"""
Handler for escape sequence CHA
"""
if params == None:
print "WARNING: CHA without parameter"
return
col = int(params)
# convert it to zero based index
col -= 1
if col >= 0 and col < self.cols:
self.curX = col
else:
print "WARNING: CHA column out of boundary"
def __OnEscSeqCUP(self, params):
"""
Handler for escape sequence CUP
"""
y = 0
x = 0
if params != None:
values = params.split(';')
if len(values) == 2:
y = int(values[0]) - 1
x = int(values[1]) - 1
else:
print "WARNING: escape sequence CUP has invalid parameters"
return
if x < 0:
x = 0
elif x >= self.cols:
x = self.cols - 1
if y < 0:
y = 0
elif y >= self.rows:
y = self.rows - 1
self.curX = x
self.curY = y
def __OnEscSeqDECCKM_ON(self, params):
"""
Handles the DECCKM_ON escape sequence
"""
goodCmd = False
n = 0
try:
n = int(params[1:])
except ValueError, TypeError:
pass
if 1 == n:
goodCmd = True
self.cursorMapping = self.applicationCursorMapping
if not goodCmd:
interChars = ''
if params != None:
interChars = params
escSeq = interChars + self.__ESCSEQ_DECCKM_ON
if self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ] != None:
self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ](escSeq)
def __OnEscSeqDECCKM_RESET(self, params):
"""
Handles the DECCKM_RESET escape sequence
"""
goodCmd = False
n = 0
try:
n = int(params[1:])
except ValueError, TypeError:
pass
if 1 == n:
goodCmd = True
self.cursorMapping = self.standardCursorMapping
if not goodCmd:
interChars = ''
if params != None:
interChars = params
escSeq = interChars + self.__ESCSEQ_DECCKM_ON
if self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ] != None:
self.callbacks[self.CALLBACK_UNHANDLED_ESC_SEQ](escSeq)
def __OnEscSeqED(self, params):
"""
Handler for escape sequence ED
"""
n = 0
if params != None:
n = int(params)
if n == 0:
self.ClearRect(self.curY, self.curX, self.rows - 1, self.cols - 1)
elif n == 1:
self.ClearRect(0, 0, self.curY, self.curX)
elif n == 2:
self.ClearRect(0, 0, self.rows - 1, self.cols - 1)
else:
print "WARNING: escape sequence ED has invalid parameter"
def __OnEscSeqEL(self, params):
"""
Handler for escape sequence EL
"""
n = 0
if params != None:
n = int(params)
if n == 0:
self.ClearRect(self.curY, self.curX, self.curY, self.cols - 1)
elif n == 1:
self.ClearRect(self.curY, 0, self.curY, self.curX)
elif n == 2:
self.ClearRect(self.curY, 0, self.curY, self.cols - 1)
else:
print "WARNING: escape sequence EL has invalid parameter"
def __OnEscSeqVPA(self, params):
"""
Handler for escape sequence VPA
"""
if params == None:
print "WARNING: VPA without parameter"
return
row = int(params)
# convert it to zero based index
row -= 1
if row >= 0 and row < self.rows:
self.curY = row
else:
print "WARNING: VPA line no. out of boundary"
def __OnEscSeqSGR(self, params):
"""
Handler for escape sequence SGR
"""
if params != None:
renditions = params.split(';')
for rendition in renditions:
irendition = int(rendition)
if irendition == 0:
# reset rendition
self.curRendition = 0L
elif irendition > 0 and irendition < 9:
# style
self.curRendition |= (1 << (irendition - 1))
elif irendition >= 30 and irendition <= 37:
# foreground
self.curRendition |= ((irendition - 29) << 8) & 0x00000f00
elif irendition >= 40 and irendition <= 47:
# background
self.curRendition |= ((irendition - 39) << 12) & 0x0000f000
elif irendition == 27:
# reverse video off
self.curRendition &= 0xffffffbf
elif irendition == 39:
# set underscore off, set default foreground color
self.curRendition &= 0xfffff0ff
elif irendition == 49:
# set default background color
self.curRendition &= 0xffff0fff
else:
print "WARNING: Unsupported rendition", irendition
else:
# reset rendition
self.curRendition = 0L
#print "Current attribute", self.curAttrib