-
Notifications
You must be signed in to change notification settings - Fork 300
/
shell.py
1086 lines (840 loc) · 35.6 KB
/
shell.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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from .parser import *
from lighthouse.util import *
from lighthouse.util.qt import *
from lighthouse.util.disassembler import disassembler
#------------------------------------------------------------------------------
# Composing Shell
#------------------------------------------------------------------------------
class ComposingShell(QtWidgets.QWidget):
"""
The ComposingShell UI for interactive coverage composition.
This class ties together all the individual components that make up
the Composing Shell, wrapping it up in a nice portable widget. This
includes the label sitting at the head of the shell, the text box
(the shell, a.k.a ComposingLine), and the composition parser.
In theory, multiple ComposingShell objects could be instantiated and
placed in various dialogs, forms, views, etc. These shells are fairly
independent, but obviously must communicate with the director.
"""
def __init__(self, lctx, table_model, table_view=None):
super(ComposingShell, self).__init__()
self.setObjectName(self.__class__.__name__)
# external entities
self._director = lctx.director
self._palette = lctx.palette
self._table_model = table_model
self._table_view = table_view
# command / input
self._search_text = ""
self._command_timer = QtCore.QTimer()
# the last known user AST
self._last_ast = None
# composition parser related members
self._parser = CompositionParser()
self._parser_error = None
self._parsed_tokens = []
self._shorthand = []
# configure the widget for use
self._ui_init()
self.refresh_theme()
#--------------------------------------------------------------------------
# Properties
#--------------------------------------------------------------------------
@property
def text(self):
"""
The existing shell text.
"""
return str(self._line.toPlainText())
#--------------------------------------------------------------------------
# Initialization - UI
#--------------------------------------------------------------------------
def _ui_init(self):
"""
Initialize UI elements.
"""
# initialize a monospace font to use with our widget(s)
self._font = MonospaceFont()
self._font.setPointSizeF(normalize_to_dpi(10))
self._font_metrics = QtGui.QFontMetricsF(self._font)
# initialize our ui elements
self._ui_init_shell()
self._ui_init_completer()
self._ui_init_signals()
self._ui_layout()
def _ui_init_shell(self):
"""
Initialize the shell UI elements.
"""
# the composer label at the head of the shell
self._line_label = QtWidgets.QLabel("Composer")
self._line_label.setStyleSheet("QLabel { margin: 0 1ex 0 1ex }")
self._line_label.setAlignment(QtCore.Qt.AlignCenter)
self._line_label.setFont(self._font)
self._line_label.setFixedWidth(self._line_label.sizeHint().width())
# the text box / shell / ComposingLine
self._line = ComposingLine()
def _ui_init_completer(self):
"""
Initialize the coverage hint UI elements.
"""
self._completer_model = QtCore.QStringListModel([])
self._completer = QtWidgets.QCompleter(self)
self._completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
self._completer.setModelSorting(QtWidgets.QCompleter.CaseInsensitivelySortedModel)
self._completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self._completer.setModel(self._completer_model)
self._completer.setWrapAround(False)
self._completer.popup().setFont(self._font)
self._completer.setWidget(self._line)
def _ui_init_signals(self):
"""
Connect UI signals.
"""
# text changed in the shell
self._line.textChanged.connect(self._ui_shell_text_changed)
# cursor position changed in the shell
self._line.cursorPositionChanged.connect(self._ui_shell_cursor_changed)
# return key pressed in the shell
self._line.returnPressed.connect(self._ui_shell_return_pressed)
# register for cues from the director
self._director.coverage_created(self._internal_refresh)
self._director.coverage_deleted(self._internal_refresh)
self._director.coverage_modified(self._internal_refresh)
self._director.coverage_switched(self._coverage_switched)
# register for cues from the model
self._table_model.layoutChanged.connect(self._ui_shell_text_changed)
def _ui_layout(self):
"""
Layout the major UI elements of the widget.
"""
# create a qt layout for the 'composer' (the shell)
layout = QtWidgets.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
#
# Shell Layout:
# [ [ 'Composer' ][ ComposingLine ... ] ]
#
layout.addWidget(self._line_label)
layout.addWidget(self._line)
# apply the widget layout
self.setLayout(layout)
#--------------------------------------------------------------------------
# Refresh
#--------------------------------------------------------------------------
def refresh(self):
"""
Public refresh of the shell.
"""
self._internal_refresh()
@disassembler.execute_ui
def refresh_theme(self):
"""
Refresh UI facing elements to reflect the current theme.
"""
assert (self._line and self._completer), "UI not yet initialized..."
# configure the shell background & default text color
qpal = self._line.palette()
qpal.setColor(QtGui.QPalette.Text, self._palette.shell_text)
qpal.setColor(QtGui.QPalette.WindowText, self._palette.shell_text)
self._line.setPalette(qpal)
# set other hard to access shell theme elements
self._line.setStyleSheet(
"QPlainTextEdit {"
" color: %s;" % self._palette.shell_text.name() + # this line ensures the text cursor changes color, with the theme
" background-color: %s;" % self._palette.shell_background.name() +
" border: 1px solid %s;" % self._palette.shell_border.name() +
"} "
"QPlainTextEdit:hover, QPlainTextEdit:focus {"
" border: 1px solid %s;" % self._palette.shell_border_focus.name() +
"}"
)
# refresh completer popup style...
self._completer.popup().setStyleSheet(
"background: %s;" % self._palette.shell_hint_background.name() +
"color: %s;" % self._palette.shell_hint_text.name()
)
@disassembler.execute_ui
def _internal_refresh(self):
"""
Internal refresh of the shell.
"""
self._refresh_hint_list()
self._ui_shell_text_changed()
def _refresh_hint_list(self):
"""
Refresh the shell coverage hint contents.
"""
hints = []
self._shorthand = []
# get the detailed coverage strings from the director
for x in self._director.coverage_names:
hints.append(self._director.get_coverage_string(x))
symbol = self._director.get_shorthand(x)
if symbol:
self._shorthand.append(symbol)
# install the fresh coverage strings to the hint completer dialog
self._completer_model.setStringList(hints)
# queue a UI coverage hint if necessary
self._ui_hint_coverage_refresh()
def _coverage_switched(self):
"""
Handle a coverage switched event.
specifically, we want cover the specical case where the hot shell is
being switched to. In these cases, we should forcefully clear the
'last' AST so that the full shell expression is re-evaluated and
sent forward to the director.
this will ensure that the director will evaluate and display the
results of the present expression as the 'Hot Shell' is now active.
"""
if self._director.coverage_name == "Hot Shell":
self._last_ast = None
self._internal_refresh()
#--------------------------------------------------------------------------
# Signal Handlers
#--------------------------------------------------------------------------
def _ui_hint_tooltip(self, text, index):
"""
Display a non-intrusive error tooltip to the user.
"""
#
# hide the coverage hint if it is visible. things can look cluttered
# down by the shell if we're trying to show both.
#
self._ui_hint_coverage_hide()
# create a cursor and move it to the parse error location on the shell
cursor_tip = QtGui.QTextCursor(self._line.document())
cursor_tip.setPosition(index)
#
# using our carefully positioned cursor, we can now extract the relative
# pixel position of the parse error on the shell and map its global
# (absolute) pixel position on the screen.
#
position = self._line.mapToGlobal(self._line.cursorRect(cursor_tip).topLeft())
# draw the tooltip at the computed parse error position
x = QtWidgets.QToolTip.showText(position, text)
def _ui_shell_cursor_changed(self):
"""
Cursor position changed in the shell.
"""
self._ui_hint_coverage_refresh()
def _ui_shell_text_changed(self):
"""
Text changed in the shell.
"""
text = self.text
#
# a Search, eg '/DnsParse_'
#
if self.is_search(text):
self._execute_search(text)
self._highlight_search()
return
# not a search query clear any lingering filters for it
else:
self._table_model.filter_string("")
#
# a Jump, eg '0x804010a' or 'sub_1400016F0'
#
if self.is_jump(text) and self._table_view:
self._line_label.setText("Jump")
self._highlight_jump()
return
#
# a Composition, eg '(A | B) - C'
#
self._execute_composition(text)
self._highlight_composition()
self._ui_hint_coverage_refresh()
def _ui_shell_return_pressed(self):
"""
Return / Enter pressed in the shell.
The user pressed 'enter' in the shell, this means we want to try
and save their composition as a new coverage set to the director.
"""
text = self.text
# a search query has no accept state, nothing to do
if self.is_search(text):
return
# jump to the function entry containing the requested address
if self.is_jump(text) and self._table_view:
self._execute_jump(text)
return
# attempt to save the user crafted composition
self._accept_composition()
#--------------------------------------------------------------------------
# Search
#--------------------------------------------------------------------------
@staticmethod
def is_search(text):
"""
Check if a string (text) looks like a search query.
A search query is used to filter functions listed in the coverage
overview table based on their name.
eg: text = '/DnsParse_'
"""
return (text and text[0] == "/")
def _execute_search(self, text):
"""
Execute the search semantics.
"""
self._search_text = text[1:]
#
# if the user input is only "/" (starting to type something), hint
# that they are entering the Search mode. nothing else to do!
#
if text == "/":
self._line_label.setText("Search")
return
#
# stop an existing command timer if there is one running. we are about
# to schedule a new one or execute inline. so the old/deferred command
# is no longer needed.
#
self._command_timer.stop()
#
# if the functions list is HUGE, we want to defer the filtering until
# we think the user has stopped typing as each pass may take awhile
# to compute (while blocking the main thread...)
#
if self._director.metadata.is_big():
self._command_timer = singleshot(1000, self._execute_search_internal)
self._command_timer.start()
#
# the database is not *massive*, let's execute the search immediately
#
else:
self._execute_search_internal()
# done
return
def _execute_search_internal(self):
"""
Execute the actual search filtering & coverage metrics.
"""
# the given text is a real search query, apply it as a filter now
self._table_model.filter_string(self._search_text)
# compute coverage % of the visible (filtered) results
percent = self._table_model.get_modeled_coverage_percent()
# show the coverage % of the search results in the shell label
self._line_label.setText("%1.2f%%" % percent)
def _highlight_search(self):
"""
Syntax highlight a search query.
"""
self._line.setUpdatesEnabled(False)
################# UPDATES DISABLED #################
# clear any existing text colors
self._color_clear()
# color search based on if there are any matching results
if self._table_model.rowCount():
self._color_text(self._palette.shell_text_valid, start=1)
else:
self._color_text(self._palette.shell_text_invalid, start=1)
################# UPDATES ENABLED #################
self._line.setUpdatesEnabled(True)
# done
return
#--------------------------------------------------------------------------
# Jump
#--------------------------------------------------------------------------
def is_jump(self, text):
"""
Check if a string (text) looks like a jump query.
A jump query is used to jump to a function in the coverage overview
table based on their address.
eg: text = '0x8040100', or 'sub_1400016F0'
"""
return self._compute_jump(text) != 0
def _compute_jump(self, text):
"""
Compute the function address destination of a jump target from a string.
eg: text = '0x8040100', or 'sub_8040100' --> jump to function 0x8040100
"""
text = text.strip()
#
# if the user input is less than two characters, we automatically
# dismiss it as a valid jump target. the primary reasons for this
# is to avoid possible shorthand parsing clashes.
#
# eg: imagine the user has a valid function named 'A' that they want to
# jump to - well we actually choose to ignore that request here.
#
# We favor the importance of shorthand symbols as used in compositions.
#
if len(text) < 2:
return 0
#
# attempt to convert the user input from a hex number eg '0x8040105'
# to its corresponding function address validated by the director
#
try:
address = int(text, 16)
except ValueError:
pass
else:
functions = self._director.metadata.get_functions_containing(address)
if functions:
return functions[0].address
#
# the user string did not translate to a parsable hex number (address)
# or the function it falls within could not be found in the director.
#
# attempt to convert the user input from a function name, eg 'main',
# or 'sub_1400016F0' to a function address validated by the director.
#
# special case to make 'sub_*' prefixed user inputs case insensitive
if text.lower().startswith("sub_"):
# attempt uppercase hex (IDA...)
function_metadata = self._director.metadata.get_function_by_name("sub_" + text[4:].upper())
if function_metadata:
return function_metadata.address
# attempt lowercase hex (Binja...)
function_metadata = self._director.metadata.get_function_by_name("sub_" + text[4:].lower())
if function_metadata:
return function_metadata.address
#
# no luck yet, let's just throw the user's raw text at the lookup. this
# would probably be a function they renamed, such as 'foobar'
#
function_metadata = self._director.metadata.get_function_by_name(text)
if function_metadata:
return function_metadata.address
#
# the user string did not translate to a function name that could
# be found in the director. so I guess they're not trying to jump...
#
# failure, the user input (text) isn't a jump ...
return 0
def _execute_jump(self, text):
"""
Execute the jump semantics.
"""
assert self._table_view
# retrieve the jump target
function_address = self._compute_jump(text)
assert function_address
# select the function entry in the coverage overview table
self._table_view.selectRow(self._table_model.func2row[function_address])
self._table_view.scrollTo(
self._table_view.currentIndex(),
QtWidgets.QAbstractItemView.PositionAtCenter
)
def _highlight_jump(self):
"""
Syntax highlight a jump query.
"""
self._line.setUpdatesEnabled(False)
################# UPDATES DISABLED #################
# clear any existing text colors
self._color_clear()
# color jump
self._color_text(self._palette.shell_text_valid)
################# UPDATES ENABLED #################
self._line.setUpdatesEnabled(True)
# done
return
#--------------------------------------------------------------------------
# Composition
#--------------------------------------------------------------------------
def _execute_composition(self, text):
"""
Execute a composition query.
"""
# reset the shell head text
self._line_label.setText("Composer")
# attempt to parse & execute a composition
try:
# clear any previous parse attempts/failures
self._parser_error = None
# attempt to parse the user input against the composition grammar
self._parsed_tokens, ast = self._parser.parse(text, self._shorthand)
# if the AST changed since the last parse, inform the director
if not ast_equal(self._last_ast, ast):
self._director.cache_composition(ast)
# save the newly parsed ast
self._last_ast = ast
# parse failure
except ParseError as e:
self._parser_error = e
#
# even though we failed to generate an AST that can be evaluated
# by the director, we still want to save the list of tokens parsed.
# these tokens will still be used for basic syntax highlighting.
#
self._parsed_tokens = e.parsed_tokens
# done
return True
def _highlight_composition(self):
"""
Syntax highlight a composition.
"""
self._line.setUpdatesEnabled(False)
################# UPDATES DISABLED #################
# clear any existing text colors
self._color_clear()
# the parse failed, so there will be invalid text to highlight
if self._parser_error:
self._color_invalid()
# paint any valid tokens
self._color_tokens()
################# UPDATES ENABLED #################
self._line.setUpdatesEnabled(True)
# done
return
def _accept_composition(self):
"""
Save the user crafted composition to the director.
"""
#
# if there's an existing parse error on the shell, there's nothing we
# can do but pop a hint for the user and have them try again
#
if self._parser_error:
self._ui_hint_tooltip("Invalid Composition", self._parser_error.error_index)
return
#
# While the user is picking a name for the new composite, we might as well
# try and compute/cache it asynchronously :-). kick the caching off now.
#
self._director.cache_composition(self._last_ast, force=True)
#
# the user has entered a valid composition that we have parsed. we
# want to save this to the director, but first we need a name for the
# new composition. pop a simple dialog prompting the user for a
# composition name
#
ok, coverage_name = prompt_string(
"Composition Name:",
"Please enter a name for this composition",
"COMP_%s" % self.text
)
#
# once the naming prompt closes, the composing shell tries to pop
# the coverage hint again which can make it annoying and too
# aggressive.
#
# clearing focus on the text line will ensure the hint does not pop
#
self._line.clearFocus()
#
# returning back to the naming prompt, if the user did not enter a
# coverage name (or hit cancel), we will abort saving the composition
#
if not (ok and coverage_name):
return
#
# a name was given and all is good, ask the director to save the last
# composition under the user specified coverage name
#
self._director.add_composition(coverage_name, self._last_ast)
# switch to the newly created composition
self._director.select_coverage(coverage_name)
#--------------------------------------------------------------------------
# Coverage Hint
#--------------------------------------------------------------------------
def _ui_hint_coverage_refresh(self):
"""
Draw the coverage hint as applicable.
"""
#
# if the shell is not focused (or empty), don't bother to show a hint
# as it frequently gets in the way and is really annoying...
#
if not (self._line.hasFocus() and self.text):
self._ui_hint_coverage_hide()
return
#
# if the text cursor is moving and the user has their left mouse
# button held, then they are probably doing a click + drag text
# selection so we shouldn't be naggin them with hints and stuff
#
# without this condition, click+drag selection gets really choppy
#
if QtWidgets.QApplication.mouseButtons() & QtCore.Qt.LeftButton:
self._ui_hint_coverage_hide()
return
# scrape info from the current shell text state
cursor_index = self._line.textCursor().position()
text_token = self._get_cursor_coverage_token(cursor_index)
#
# if the user's text cursor is touching the index that produced the
# parse error (assuming there was one) ...
#
if self._parser_error and self._parser_error.error_index == cursor_index:
#
# if the parse error indicates the parse failed because it expected
# a coverage token but didn't get one, show the complete coverage
# list. The user should know their list of options bro.
#
if self._parser_error.expected == TokenCoverageSingle:
self._ui_hint_coverage_show()
#
# if the user's text cursor is touching a valid coverage token, we want
# to pop a hint that shows the details for the coverage matching that
# explicit token / shorthand. It's a subtle convenience :-)
#
elif text_token and (text_token.type == "COVERAGE_TOKEN"):
self._ui_hint_coverage_show(text_token.value)
#
# if the user's text cursor is not touching any text index of interest,
# there's no reason for us to show any sort of hints. be sure any hints
# are hidden.
#
else:
self._ui_hint_coverage_hide()
# done
return
def _ui_hint_coverage_show(self, prefix=''):
"""
Show the coverage hint at the shell's cursor position.
Optionally, one can specify a prefix (eg, the shorthand 'A') to
limit the scope of coverage items hinted.
"""
#
# if the completer is already visible and showing the requested prefix,
# then we have nothing to do. this will help mitigate refresh flickers
#
if self._completer.popup().isVisible() and \
self._completer.completionPrefix() == prefix:
return
# if there was anything previously selected in the popup, clear it now
self._completer.popup().clearSelection()
# show only hints matching the given prefix
# eg: prefix = 'A' will show only entry 'A - 42.30% - drcov.8...'
self._completer.setCompletionPrefix(prefix)
# specify the position and size of the hint popup
cr = self._line.cursorRect()
cr.setWidth(self._completer.popup().sizeHintForColumn(0))
# show the coverage hint popup
self._completer.complete(cr)
self._completer.popup().repaint() # reduces hint flicker on the Hot Shell
# done
return
def _ui_hint_coverage_hide(self):
"""
Hide the coverage hint.
"""
self._completer.popup().hide()
def _get_cursor_coverage_token(self, index):
"""
Get the coverage token touching the cursor (if there is one).
"""
# iterate through the list of parsed tokens on the line edit / shell
for text_token in self._parsed_tokens:
# skip any non-coverage text tokens
if not text_token.type == "COVERAGE_TOKEN":
continue
# if this coverage text token touches our cursor, return it
if text_token.span[0] <= index <= text_token.span[1]:
return text_token
# no coverage token on either side of the cursor
return None
#--------------------------------------------------------------------------
# Composition Highlighting
#--------------------------------------------------------------------------
def _color_tokens(self):
"""
Syntax highlight the valid composition tokens.
"""
# more code-friendly, readable aliases
TOKEN_COLORS = self._palette.TOKEN_COLORS
#
# in order to syntax highlight text of interest, we must use a text
# cursor as the vehicle to move around the text box (shell) and
# manipulate its contents (eg, painting colors)
#
# this is simply the way Qt exposes this functionality
#
# alias the user cursor, and save its original (current) position
cursor = self._line.textCursor()
cursor_position = cursor.position()
# configure text formatting properties we want our cursor to apply
highlight = QtGui.QTextCharFormat()
highlight.setFontWeight(QtGui.QFont.Bold) # bolds text we 'type'
#
# we are about to start painting our text, but we want to disable the
# shell from emitting any textChanged/cursorMoved kind of signals
# that originate from our painting code.
#
# we use the blockSignals gateways below to disable/enable the signals
# for the duration of our painting.
#
self._line.blockSignals(True)
################# UPDATES DISABLED #################
# iterate through every parsed token, and paint it
for token in self._parsed_tokens:
# if the palette doesn't define a color for this token, ignore it
if token.type not in TOKEN_COLORS:
continue
# alias the start and end indexes of the text token to paint
token_start, token_end = token.span
# 'click' and 'drag' to select the token text
cursor.setPosition(token_start, QtGui.QTextCursor.MoveAnchor)
cursor.setPosition(token_end, QtGui.QTextCursor.KeepAnchor)
# configure the colors/style for this explicit token
#highlight.setBackground(QtGui.QBrush(QtGui.QColor(TOKEN_COLORS[token.type])))
highlight.setForeground(QtGui.QBrush(TOKEN_COLORS[token.type]))
cursor.setCharFormat(highlight)
#
# we are done painting all the parsed tokens. let's restore the user
# cursor back to its original state so they are none-the-wiser
#
cursor.setPosition(cursor_position)
cursor.setCharFormat(QtGui.QTextCharFormat())
self._line.setTextCursor(cursor)
################# UPDATES ENABLED #################
self._line.blockSignals(False)
# done
return
def _color_invalid(self):
"""
Highlight the invalid (un-parsable) text.
Please read through the _color_tokens() function for a more
complete walkthrough of the text painting process.
"""
assert self._parser_error
# the invalid text starts from the token that caused a parse error
invalid_start = self._parser_error.error_index
invalid_text = self.text[invalid_start:]
# no invalid text? nothing to highlight I guess!
if not invalid_text:
return
# alias the user cursor, and save its original (current) position
cursor = self._line.textCursor()
cursor_position = cursor.position()
# setup the invalid text highlighter
invalid_color = self._palette.shell_highlight_invalid
highlight = QtGui.QTextCharFormat()
highlight.setFontWeight(QtGui.QFont.Bold)
highlight.setBackground(QtGui.QBrush(invalid_color))
self._line.blockSignals(True)
################# UPDATES DISABLED #################
# select the invalid text
cursor.setPosition(invalid_start, QtGui.QTextCursor.MoveAnchor)
cursor.setPosition(len(self.text), QtGui.QTextCursor.KeepAnchor)
# insert a highlighted version of the invalid text
cursor.setCharFormat(highlight)
# reset the cursor position & style
cursor.setPosition(cursor_position)
cursor.setCharFormat(QtGui.QTextCharFormat())
self._line.setTextCursor(cursor)
################# UPDATES ENABLED #################
self._line.blockSignals(False)
# done
return
#--------------------------------------------------------------------------
# General Highlighting
#--------------------------------------------------------------------------
def _color_clear(self):
"""
Clear any existing text colors.
"""
self._color_text()
def _color_text(self, color=None, start=0, end=0):
"""
Color shell text with the given color.
"""
# if no end was specified, apply the style till the end of input
if end == 0:
end = len(self.text)
# alias the user cursor, and save its original (current) position
cursor = self._line.textCursor()
cursor_position = cursor.position()
# setup a simple font coloring (or clearing) text format
simple = QtGui.QTextCharFormat()
if color:
simple.setForeground(QtGui.QBrush(color))
self._line.blockSignals(True)
################# UPDATES DISABLED #################
# select the entire line
cursor.setPosition(start, QtGui.QTextCursor.MoveAnchor)
cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
# set all the text to the simple format
cursor.setCharFormat(simple)
# reset the cursor position & style
cursor.setPosition(cursor_position)
self._line.setTextCursor(cursor)
################# UPDATES ENABLED #################
self._line.blockSignals(False)
# done
return
#------------------------------------------------------------------------------
# Composing Line
#------------------------------------------------------------------------------
class ComposingLine(QtWidgets.QPlainTextEdit):
"""
The textbox UI where user compositions are entered (typed).
While this a QLineEdit may appear to be more appropriate for our
'Composing Shell', its support for syntax highlighting like features
are completely absent.
QPlainTextEdit has much better support for coloring or highlighting
entered text, so we subclass from it and make a best effort attempt
to make it appear and act like a QLineEdit 'shell'