-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.py
444 lines (362 loc) · 14.9 KB
/
widget.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# haikuterm is Copyright (c) 2011 Facundo de Guzmán <facudeguzman@gmail.com>
#
# This file is part of haikuterm.
#
# haikuterm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Foobar 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with haikuterm. If not, see <http://www.gnu.org/licenses/>.
from PyQt4 import QtGui, QtCore
import sys
import emuvt100
import session
# NORMAL, LIGHT/BRIGHT
COLOR_TABLE = [#black
(QtGui.QColor(0, 0, 0), QtGui.QColor(0, 0, 0)),
#red
(QtGui.QColor(205, 0, 0), QtGui.QColor(205, 0, 0)),
#green
(QtGui.QColor(0, 205, 0), QtGui.QColor(205, 0, 0)),
#brown,yellow
(QtGui.QColor(205, 205, 0), QtGui.QColor(205, 0, 0)),
#blue
(QtGui.QColor(0, 0, 238), QtGui.QColor(205, 0, 0)),
#magenta
(QtGui.QColor(205, 0, 205), QtGui.QColor(205, 0, 0)),
#cyan
(QtGui.QColor(0, 205, 205), QtGui.QColor(205, 0, 0)),
#gray
(QtGui.QColor(229, 229, 229), QtGui.QColor(205, 0, 0))]
class HaikutermWidget(QtGui.QFrame):
def __init__(self, parent=None, app=None):
super(HaikutermWidget, self).__init__(parent)
self.app = app
self.ROWS = self.rows = 24
self.COLS = self.cols = 80
self._fixed_size = 0
self.row_spacing = 1
self.col_spacing = 1
self.background_color = QtGui.QColor(0, 0, 0)
self.foreground_color = QtGui.QColor(255, 255, 255)
self.terminal = None
self.set_terminal()
self.setAutoFillBackground(True)
self.setFocusPolicy(QtCore.Qt.WheelFocus)
# this is an important optimization, it tells Qt
# that TerminalDisplay will handle repainting its entire area.
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
self.font_height = 1
self.font_weight = 1
self.font_width = 1
self.cell_width = 1
self.cell_height = 1
self.screen = {}
self.screenRend = {}
self.shell = None
self.set_default_font()
self._changes = []
self.redraw_screen = False
self.cursor_pos = [{"x":0, "y":0, "row":0, "col":0},
{"x":0, "y":0, "row":0, "col":0}]
self.cursor_row = 0
self.cursor_col = 0
self.cursor_color = COLOR_TABLE[7][0]
self.cursor_type = 0
self.blinking = False
self.blink = False
self.blink_timer = QtCore.QTimer()
self.blink_timer.start(1000)
self.update_blinking(activate=True)
self.history_size = 100
self.scroll_area = QtGui.QScrollArea()
self.set_scroll()
def set_scroll(self):
self.scroll_area.setVerticalScrollBarPolicy(
QtCore.Qt.ScrollBarAlwaysOn)
self.scroll_area.setWidget(self)
self.scroll_area.setWidgetResizable(True)
self.scroll_area.show()
def activateWindow(self):
self.redraw_screen = True
super(HaikutermWidget, self).activateWindow()
def update_blinking(self, activate=False):
if not activate and self.blinking:
self.disconnect(self.blink_timer, QtCore.SIGNAL("timeout()"))
else:
self.connect(self.blink_timer, QtCore.SIGNAL("timeout()"),
self.blink_bang)
def set_cursor_type(self, line=True, block=False):
if block:
self.cursor_type = 1
else:
self.cursor_type = 0
def blink_bang(self):
self.blink = not self.blink
self.update()
def set_terminal(self):
self.terminal = emuvt100.V102Terminal(self.rows, self.cols)
self.terminal.SetCallback(self.terminal.CALLBACK_SCROLL_UP_SCREEN,
self.scroll_up)
self.terminal.SetCallback(self.terminal.CALLBACK_UPDATE_LINES,
self.update_lines)
self.terminal.SetCallback(self.terminal.CALLBACK_UPDATE_CURSOR_POS,
self.update_cursor_position)
self.terminal.SetCallback(self.terminal.CALLBACK_UPDATE_WINDOW_TITLE,
self.set_window_title)
self.terminal.SetCallback(self.terminal.CALLBACK_UNHANDLED_ESC_SEQ,
self.unhandled_esc_seq)
def scroll_up(self):
self.resize(self.width(), self.height() + self.cell_height)
self.scroll_area.scrollContentsBy(0, self.cell_height)
def update_cursor_position(self):
self.cursor_pos[0] = self.cursor_pos[1]
row, col = self.terminal.GetCursorPos()
self.cursor_pos[1]["row"] = row
self.cursor_pos[1]["col"] = col
def set_window_title(self, title):
self.setWindowTitle(title)
def unhandled_esc_seq(self, key):
pass
def set_default_font(self):
font = QtGui.QFont()
font.setFamily("Terminus")
font.setPixelSize(14)
font.setStyle(font.StyleNormal)
font.setWeight(font.Normal)
self.setFont(font)
self.fontChange(font)
def print_test_screen(self):
for line in self.terminal.screen:
current_line = u"".join([char for char in line])
print current_line
def _get_rendition_font(self, rendition):
font = self.font()
if rendition.intensity > 0:
font.setBold(True)
elif rendition.intensity < 0:
font.setWeight(font.Light)
if rendition.italic:
font.setItalic(True)
if rendition.underline:
font.setUnderline(True)
return font
def _get_color_from_table(self, color_index):
return COLOR_TABLE[color_index][0]
def draw_cursor(self, painter):
left = self.contentsRect().left()
top = self.contentsRect().top()
# redraw the pass cursor position
row = self.cursor_pos[0]["row"]
col = self.cursor_pos[0]["col"]
rendition = self.terminal.GetRendition(row, col)
char = self.terminal.GetChar(row, col)
self.draw_char(painter, row, col, rendition, char)
# redraw the current cursor position
x = left + self.cursor_pos[1]["col"] * self.cell_width
y = top + self.cursor_pos[1]["row"] * self.cell_height
rect = QtCore.QRect(QtCore.QPoint(x, y), QtCore.QSize(self.cell_width,
self.cell_height))
if self.blink:
cursor_color = self.cursor_color
else:
cursor_color = self.background_color
if self.cursor_type:
painter.fillRect(rect, cursor_color)
else:
painter.setPen(cursor_color)
painter.drawLine(QtCore.QPoint(x, y + 2),
QtCore.QPoint(x, y + self.cell_height - 1))
def draw_char(self, painter, row, col, rendition, char):
left = self.contentsRect().left()
top = self.contentsRect().top()
x = left + col * self.cell_width
y = top + row * self.cell_height
if rendition:
font = self._get_rendition_font(rendition)
fg_color = self._get_color_from_table(rendition.fg_color)
bg_color = self._get_color_from_table(rendition.bg_color)
else:
font = self.font()
fg_color = self.background_color
bg_color = self.background_color
rect = QtCore.QRect(QtCore.QPoint(x, y),
QtCore.QSize(self.cell_width,
self.cell_height))
painter.fillRect(rect, bg_color)
painter.setFont(font)
painter.setPen(fg_color)
painter.drawText(rect, QtCore.Qt.AlignCenter, char)
def draw_screen(self, painter):
update_char_count = 0
changes = []
changes, self._changes = self._changes, changes
curRendition = emuvt100.Rendition()
for row, col, char, rendition in changes:
if rendition:
curRendition = rendition
self.draw_char(painter, row, col, curRendition, char)
update_char_count += 1
self.changes_per_update(painter, update_char_count)
def changes_per_update(self, painter, update_char_count):
field_width = 80
left = self.contentsRect().right() - field_width
top = self.contentsRect().bottom() - self.cell_height - 5
update_rect = QtCore.QRect(QtCore.QPoint(left, top),
QtCore.QSize(field_width, self.cell_height))
painter.fillRect(update_rect, self.background_color)
painter.setPen(QtCore.Qt.white)
painter.drawText(QtCore.QPoint(left, top + self.cell_height),
"Upd: %s" % update_char_count)
def update_lines(self):
for row in self.terminal.GetDirtyLines(self.redraw_screen):
line = self.terminal.GetLine(row)
if row not in self.screen:
self.screen[row] = {}
self.screenRend[row] = {}
for col in xrange(len(line)):
char = self.terminal.GetChar(row, col)
rendition = self.terminal.GetRendition(row, col)
self.screen[row].setdefault(col, {1:None})
self.screen[row][col] = char
self.screenRend[row].setdefault(col, {1:None})
self.screenRend[row][col] = rendition
self._changes.append((row, col, char, rendition))
else:
for col in xrange(len(line)):
char = self.terminal.GetChar(row, col)
rendition = self.terminal.GetRendition(row, col)
if (self.screen[row][col] != char or
rendition is None or
self.screenRend[row][col] != rendition):
self.screen[row][col] = char
self.screenRend[row][col] = rendition
self._changes.append((row, col, char, rendition))
self.update()
def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
if self.redraw_screen:
painter.fillRect(self.contentsRect(), self.background_color)
self.draw_screen(painter)
self.draw_cursor(painter)
painter.end()
self.redraw_screen = False
def fontChange(self, font):
fm = QtGui.QFontMetrics(font) # QFontMetrics fm(font())
self.font_height = fm.leading()
self.row_spacing = fm.lineSpacing()
for i in xrange(128):
i = chr(i)
if not i.isalnum():
continue
fw = fm.width(i)
if self.font_width < fw:
self.font_width = fw
if self.font_width > 200: # don't trust unrealistic value, fallback to
# QFontMetrics::maxWidth()
self.font_width = fm.maxWidth()
if self.font_width < 1:
self.font_width = 1
self.font_width = fm.averageCharWidth()
self._recalculate_grid_size()
self.update()
def _recalculate_grid_size(self):
if not self.fixed_size:
self.cell_width = self.font_width + self.col_spacing
self.cell_height = self.font_height + self.row_spacing
self.cols = self.contentsRect().width() / self.cell_width
self.rows = self.contentsRect().height() / self.cell_height
else:
self.cols = self.COLS
self.rows = self.ROWS
self.cell_width = (self.contentsRect().width() / self.COLS +
self.col_spacing)
self.cell_height = (self.contentsRect().height() / self.ROWS +
self.row_spacing)
if self.terminal:
term_rows, term_cols = self.terminal.GetSize()
if term_rows != self.rows or term_cols != self.cols:
self.terminal.Resize(self.rows, self.cols)
self.emit(QtCore.SIGNAL("resize"), self.rows, self.cols)
def resizeEvent(self, e):
self._recalculate_grid_size()
self.redraw_screen = True
self.screen = {}
super(HaikutermWidget, self).resizeEvent(e)
def get_fixed_size(self):
return self._fixed_size
def set_fixed_size(self, value):
self._fixed_size = value
self._recalculate_grid_size()
fixed_size = property(get_fixed_size, set_fixed_size)
def run_shell(self, path):
self.shell = session.Session(self, path)
self.connect(self.shell, QtCore.SIGNAL("receive"), self.read_output)
self.connect(self.shell, QtCore.SIGNAL("done"), self.done)
self.shell.start()
self.emit(QtCore.SIGNAL("resize"), self.rows, self.cols)
def read_output(self, output):
self.terminal.ProcessInput(output)
self.update()
def keyPressEvent(self, event):
char_ordinal = event.key()
keystrokes = None
if char_ordinal == QtCore.Qt.Key_Enter:
keystrokes = u"\r"
elif char_ordinal == QtCore.Qt.Key_Tab:
keystrokes = u"\t"
elif char_ordinal == QtCore.Qt.Key_Up:
keystrokes = u"\033[A"
elif char_ordinal == QtCore.Qt.Key_Down:
keystrokes = u"\033[B"
elif char_ordinal == QtCore.Qt.Key_Right:
keystrokes = u"\033[C"
elif char_ordinal == QtCore.Qt.Key_Left:
keystrokes = u"\033[D"
elif char_ordinal == QtCore.Qt.Key_PageUp:
keystrokes = u"\033[1S"
elif char_ordinal == QtCore.Qt.Key_PageDown:
keystrokes = u"\033[1T"
if keystrokes is not None:
self.emit(QtCore.SIGNAL("write"), keystrokes)
else:
self.emit(QtCore.SIGNAL("write"), event.text())
def closeEvent(self, event):
if self.shell:
self.emit(QtCore.SIGNAL("close_pty"))
super(HaikutermWidget, self).closeEvent(event)
def done(self):
self.scroll_area.close()
#self.close()
def profile():
from hotshot import Profile
prof = Profile('widget.prof')
prof.runcall(main)
prof.close()
import hotshot.stats
stats = hotshot.stats.load('widget.prof')
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(30)
def main():
my_app = QtGui.QApplication(sys.argv)
w = HaikutermWidget(app=my_app)
w.run_shell("/bin/bash")
w.show()
my_app.exec_()
if __name__ == '__main__':
if "--profile" in sys.argv:
profile()
else:
main()