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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
|
# Copyright 2010-2020, Damian Johnson and The Tor Project
# See LICENSE for licensing information
"""
Toolkit for working with curses. Curses earns its name, and this abstracts away
its usage providing us more easy to use high level functions. This abstraction
may also allow us to use libraries like `PDCurses <http://pdcurses.sourceforge.net/>`_
if we want Windows support in the future too.
**Module Overview:**
::
start - initializes curses with the given function
key_input - get keypress by user
str_input - text field where user can input a string
curses_attr - curses encoded text attribute
clear - wipes all content from the screen
screen_size - provides the dimensions of our screen
screenshot - dump of the present on-screen content
asci_to_curses - converts terminal formatting to curses
demo_glyphs - renders a chart showing the ACS options
halt - prevents further curses rendering during shutdown
is_color_supported - checks if terminal supports color output
get_color_override - provides color we override requests with
set_color_override - sets color we override requests with
is_wide_characters_supported - checks if curses supports wide character
draw - renders subwindow that can be drawn into
Subwindow - subwindow that can be drawn within
|- addstr - draws a string
|- addstr_wrap - draws a string with line wrapping
|- box - draws box with the given dimensions
|- scrollbar - draws a left-hand scrollbar
|- hline - draws a horizontal line
+- vline - draws a vertical line
KeyInput - user keyboard input
|- match - checks if this matches the given inputs
|- is_scroll - true if key is used for scrolling
+- is_selection - true if key should trigger selection
Scroller - scrolls content with keyboard navigation
|- location - present scroll location
+- handle_key - moves scroll based on user input
CursorScroller - scrolls content with a cursor for selecting items
|- selection - present selection and scroll location
+- handle_key - moves cursor based on user input
.. data:: Color (enum)
Terminal colors.
=========== ===========
Color Description
=========== ===========
**RED** red color
**GREEN** green color
**YELLOW** yellow color
**BLUE** blue color
**CYAN** cyan color
**MAGENTA** magenta color
**BLACK** black color
**WHITE** white color
=========== ===========
.. data:: Attr (enum)
Terminal text attributes.
=================== ===========
Attr Description
=================== ===========
**NORMAL** no text attributes
**BOLD** heavy typeface
**UNDERLINE** underlined text
**HIGHLIGHT** inverted foreground and background
=================== ===========
"""
from __future__ import absolute_import
import collections
import curses
import curses.ascii
import curses.textpad
import functools
import os
import re
import threading
import time
import stem.util.conf
import stem.util.enum
import stem.util.log
import stem.util.str_tools
import stem.util.system
# Curses screen we've initialized and lock for interacting with it. Curses
# isn't thread safe and concurrency bugs produce especially sinister glitches.
CURSES_SCREEN = None
CURSES_LOCK = threading.RLock()
HALT_ACTIVITY = False
# Text colors and attributes. These are *very* commonly used so including
# shorter aliases (so they can be referenced as just GREEN or BOLD).
Color = stem.util.enum.Enum('RED', 'GREEN', 'YELLOW', 'BLUE', 'CYAN', 'MAGENTA', 'BLACK', 'WHITE')
RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, BLACK, WHITE = list(Color)
Attr = stem.util.enum.Enum('NORMAL', 'BOLD', 'UNDERLINE', 'HIGHLIGHT')
NORMAL, BOLD, UNDERLINE, HIGHLIGHT = list(Attr)
ANSI_RE = re.compile('\x1B\\[([0-9;]+)m')
CURSES_COLORS = {
Color.RED: curses.COLOR_RED,
Color.GREEN: curses.COLOR_GREEN,
Color.YELLOW: curses.COLOR_YELLOW,
Color.BLUE: curses.COLOR_BLUE,
Color.CYAN: curses.COLOR_CYAN,
Color.MAGENTA: curses.COLOR_MAGENTA,
Color.BLACK: curses.COLOR_BLACK,
Color.WHITE: curses.COLOR_WHITE,
}
CURSES_ATTRIBUTES = {
Attr.NORMAL: curses.A_NORMAL,
Attr.BOLD: curses.A_BOLD,
Attr.UNDERLINE: curses.A_UNDERLINE,
Attr.HIGHLIGHT: curses.A_STANDOUT,
}
ASCI_TO_CURSES = {
'1': BOLD,
'30': BLACK,
'31': RED,
'32': GREEN,
'33': YELLOW,
'34': BLUE,
'35': MAGENTA,
'36': CYAN,
'37': WHITE,
}
DEFAULT_COLOR_ATTR = dict([(color, 0) for color in Color])
COLOR_ATTR = None
SCROLL_KEYS = (curses.KEY_UP, curses.KEY_DOWN, curses.KEY_PPAGE, curses.KEY_NPAGE, curses.KEY_HOME, curses.KEY_END)
SPECIAL_KEYS = {
'up': curses.KEY_UP,
'down': curses.KEY_DOWN,
'left': curses.KEY_LEFT,
'right': curses.KEY_RIGHT,
'home': curses.KEY_HOME,
'end': curses.KEY_END,
'page_up': curses.KEY_PPAGE,
'page_down': curses.KEY_NPAGE,
'esc': 27,
}
Dimensions = collections.namedtuple('Dimensions', ['width', 'height'])
def conf_handler(key, value):
if key == 'color_override':
value = stem.util.str_tools._to_camel_case(value)
if value not in Color and value != 'None':
raise ValueError('"%s" isn\'t a valid color' % value)
return value
elif key == 'max_line_wrap':
return max(1, value)
CONFIG = stem.util.conf.config_dict('nyx', {
'color_interface': True,
'color_override': 'None',
'max_line_wrap': 8,
}, conf_handler)
def start(function, acs_support = True, transparent_background = False, cursor = True):
"""
Starts a curses interface, delegating to the given function.
:param funtion: function to invoke when curses starts
:param bool acs_support: uses wide characters for pipes
:param bool transparent_background: allows background transparency
:param bool cursor: makes cursor visible
"""
def _wrapper(stdscr):
global CURSES_SCREEN
CURSES_SCREEN = stdscr
if not acs_support:
_disable_acs()
if transparent_background:
try:
curses.use_default_colors()
except curses.error:
pass
if not cursor:
try:
curses.curs_set(0)
except curses.error:
pass
function()
curses.wrapper(_wrapper)
def key_input(input_timeout = None):
"""
Gets a key press from the user.
:param int input_timeout: duration in seconds to wait for user input
:returns: :class:`~nyx.curses.KeyInput` that was pressed
"""
if input_timeout:
# Timeout can't be longer than 25.5 seconds...
# https://docs.python.org/2/library/curses.html?#curses.halfdelay
curses.halfdelay(min(input_timeout * 10, 255))
else:
curses.cbreak() # wait indefinitely for key presses (no timeout)
return KeyInput(CURSES_SCREEN.getch())
def str_input(x, y, initial_text = '', backlog = None, tab_completion = None):
"""
Provides a text field where the user can input a string, blocking until
they've done so and returning the result. If the user presses escape then
this terminates and provides back **None**.
This blanks any content within the space that the input field is rendered
(otherwise stray characters would be interpreted as part of the initial
input).
:param int x: horizontal location
:param int y: vertical location
:param str initial_text: initial input of the field
:param list backlog: previous inputs that can be selected by pressing
up/down, oldest to newest
:param func tab_completion: function to suggest inputs to tab complete with
:returns: **str** with the user input or **None** if the prompt is canceled
"""
with CURSES_LOCK:
if HALT_ACTIVITY:
return None
try:
curses.curs_set(1) # show cursor
except curses.error:
pass
width = screen_size().width - x
curses_subwindow = CURSES_SCREEN.subwin(1, width, y, x)
curses_subwindow.erase()
curses_subwindow.addstr(0, 0, initial_text[:width - 1])
textbox = curses.textpad.Textbox(curses_subwindow, insert_mode = True)
handler = _handle_key
if backlog:
handler = functools.partial(_TextBacklog(backlog)._handler, handler)
if tab_completion:
handler = functools.partial(_handle_tab_completion, handler, tab_completion)
user_input = textbox.edit(lambda key: handler(textbox, key)).rstrip()
try:
curses.curs_set(0) # hide cursor
except curses.error:
pass
return None if textbox.lastcmd == curses.ascii.BEL else user_input
def _handle_key(textbox, key):
"""
Handles esc, home, end, right arrow, and resizing. We don't need to handle
the left arrow key because curses has reasonable behavior for that one.
:param Textbox textbox: current textbox context
:param int key: key pressed
:returns: **str** with the user input or **None** if the prompt is canceled
"""
y, x = textbox.win.getyx()
if key == 27:
return curses.ascii.BEL # user pressed esc
elif key == curses.KEY_HOME:
textbox.win.move(y, 0)
elif key in (curses.KEY_END, curses.KEY_RIGHT):
msg_length = len(textbox.gather())
textbox.win.move(y, x) # reverts cursor movement during gather call
if key == curses.KEY_END and x < msg_length - 1:
textbox.win.move(y, msg_length - 1) # if we're in the content then move to the end
elif key == curses.KEY_RIGHT and x < msg_length - 1:
textbox.win.move(y, x + 1) # only move cursor if there's content after it
elif key == curses.KEY_DC:
# Delete key. Remove the character after the cursor if there is one.
y, x = textbox.win.getyx()
content = textbox.gather()
if x < len(content):
content = content[:x] + content[x + 1:]
textbox.win.clear()
textbox.win.addstr(y, 0, content)
textbox.win.move(y, x) # revert cursor to its prior position
elif key == 410:
# if we're resizing the display during text entry then cancel it
# (otherwise the input field is filled with nonprintable characters)
return curses.ascii.BEL
else:
return key
def _handle_tab_completion(next_handler, tab_completion, textbox, key):
"""
Allows user to tab complete commands if sufficient context is provided to
narrow to a single option.
:param func next_handler: handler to invoke after this
:param func tab_completion: function to suggest inputs to tab complete with
:param Textbox textbox: current textbox context
:param int key: key pressed
:returns: **None** when tab is pressed, otherwise invokes next handler
"""
if key == 9:
y, x = textbox.win.getyx()
current_contents = textbox.gather().strip()
textbox.win.move(y, x) # reverts cursor movement during gather call
new_input = None
matches = tab_completion(current_contents)
if len(matches) == 1:
new_input = matches[0]
elif len(matches) > 1:
common_prefix = os.path.commonprefix(matches)
if common_prefix != current_contents:
new_input = common_prefix
if new_input:
_, max_x = textbox.win.getmaxyx()
textbox.win.clear()
textbox.win.addstr(y, 0, new_input[:max_x - 1])
textbox.win.move(y, min(len(new_input), max_x - 1))
return None
return next_handler(textbox, key)
class _TextBacklog(object):
"""
History backlog that allows the :func:`~nyx.curses.str_input` function to
scroll through prior inputs when pressing the up and down arrow keys.
"""
def __init__(self, backlog = []):
self._backlog = backlog # backlog contents, oldest to newest
self._selection = None # selected item, None if we're not on the backlog
self._custom_input = '' # field's input prior to selecting a backlog item
def _handler(self, next_handler, textbox, key):
if key in (curses.KEY_UP, curses.KEY_DOWN):
if key == curses.KEY_UP:
if self._selection is None:
new_selection = len(self._backlog) - 1
else:
new_selection = max(0, self._selection - 1)
else:
if self._selection is None or self._selection == len(self._backlog) - 1:
new_selection = None
else:
new_selection = self._selection + 1
if self._selection == new_selection:
return None
if self._selection is None:
self._custom_input = textbox.gather().strip() # save custom input
new_input = self._custom_input if new_selection is None else self._backlog[new_selection]
y, _ = textbox.win.getyx()
_, max_x = textbox.win.getmaxyx()
textbox.win.clear()
textbox.win.addstr(y, 0, new_input[:max_x - 1])
textbox.win.move(y, min(len(new_input), max_x - 1))
self._selection = new_selection
return None
return next_handler(textbox, key)
def curses_attr(*attributes):
"""
Provides encoding for the given curses text attributes.
:param list attributes: curses text attributes and colors
:returns: **int** that can be used with curses
"""
encoded = curses.A_NORMAL
for attr in attributes:
if attr in Color:
override = get_color_override()
encoded |= _color_attr()[override if override else attr]
elif attr in Attr:
encoded |= CURSES_ATTRIBUTES[attr]
else:
raise ValueError("'%s' isn't a valid curses text attribute" % attr)
return encoded
def clear():
"""
Clears all content from the screen.
"""
CURSES_SCREEN.clear()
def screen_size():
"""
Provides the current dimensions of our screen.
:returns: :data:`~nyx.curses.Dimensions` with our screen size
"""
height, width = CURSES_SCREEN.getmaxyx()
return Dimensions(width, height)
def screenshot():
"""
Provides a dump of the present content of the screen.
:returns: **str** with the present content shown on the screen
"""
lines = []
for y in range(screen_size().height):
lines.append(CURSES_SCREEN.instr(y, 0).rstrip())
return stem.util.str_tools._to_unicode(b'\n'.join(lines).rstrip())
def asci_to_curses(msg):
"""
Translates ANSI terminal escape sequences to curses formatting.
:param str msg: string to be converted
:returns: **list** series of (text, attr) tuples that's renderable by curses
"""
entries, next_attr = [], ()
match = ANSI_RE.search(msg)
while match:
if match.start() > 0:
entries.append((msg[:match.start()], next_attr))
curses_attr = match.group(1).split(';')
new_attr = [ASCI_TO_CURSES[num] for num in curses_attr if num in ASCI_TO_CURSES]
if '0' in curses_attr:
next_attr = tuple(new_attr) # includes a 'reset'
else:
combined_attr = list(next_attr)
for attr in new_attr:
if attr in combined_attr:
continue
elif attr in Color:
# replace previous color with new one
combined_attr = list(filter(lambda attr: attr not in Color, combined_attr))
combined_attr.append(attr)
next_attr = tuple(combined_attr)
msg = msg[match.end():]
match = ANSI_RE.search(msg)
if msg:
entries.append((msg, next_attr))
return entries
def demo_glyphs():
"""
Renders a chart of all ACS options with their corresponding representation.
These are undocumented in the pydocs. For more information see the following
man page...
http://www.mkssoftware.com/docs/man5/terminfo.5.asp
"""
def _render():
height, width = CURSES_SCREEN.getmaxyx()
columns = width / 30
if columns == 0:
return # not wide enough to show anything
# mapping of keycodes to their ACS option names (for instance, ACS_LTEE)
acs_options = dict((v, k) for (k, v) in curses.__dict__.items() if k.startswith('ACS_'))
CURSES_SCREEN.addstr(0, 0, 'Curses Glyphs:', curses.A_STANDOUT)
x, y = 0, 2
for keycode in sorted(acs_options.keys()):
CURSES_SCREEN.addstr(y, x * 30, '%s (%i)' % (acs_options[keycode], keycode))
CURSES_SCREEN.addch(y, (x * 30) + 25, keycode)
x += 1
if x >= columns:
x, y = 0, y + 1
if y >= height:
break
CURSES_SCREEN.getch() # quit on keyboard input
try:
start(_render, transparent_background = True, cursor = False)
except KeyboardInterrupt:
pass # quit
def halt():
"""
Prevents further rendering of curses content while python's shutting down.
"""
global HALT_ACTIVITY
with CURSES_LOCK:
HALT_ACTIVITY = True
def is_color_supported():
"""
Checks if curses currently supports rendering colors.
:returns: **True** if colors can be rendered, **False** otherwise
"""
return _color_attr() != DEFAULT_COLOR_ATTR
def get_color_override():
"""
Provides the override color used by the interface.
:returns: :data:`~nyx.curses.Color` for the color requrests will be
overwritten with, **None** if no override is set
"""
color_override = CONFIG.get('color_override', 'None')
return None if color_override == 'None' else color_override
def set_color_override(color = None):
"""
Overwrites all requests for color with the given color instead.
:param nyx.curses.Color color: color to override all requests with, **None**
if color requests shouldn't be overwritten
:raises: **ValueError** if the color name is invalid
"""
nyx_config = stem.util.conf.get_config('nyx')
if color is None:
nyx_config.set('color_override', 'None')
elif color in Color:
nyx_config.set('color_override', color)
else:
raise ValueError('"%s" isn\'t a valid color' % color)
def _color_attr():
"""
Initializes color mappings usable by curses. This can only be done after
calling curses.initscr().
"""
global COLOR_ATTR
if COLOR_ATTR is None:
if not CONFIG['color_interface']:
COLOR_ATTR = DEFAULT_COLOR_ATTR
elif curses.has_colors():
color_attr = dict(DEFAULT_COLOR_ATTR)
for color_pair, color_name in enumerate(CURSES_COLORS):
foreground_color = CURSES_COLORS[color_name]
background_color = -1 # allows for default (possibly transparent) background
curses.init_pair(color_pair + 1, foreground_color, background_color)
color_attr[color_name] = curses.color_pair(color_pair + 1)
stem.util.log.info('Terminal color support detected and enabled')
COLOR_ATTR = color_attr
else:
stem.util.log.info('Terminal color support unavailable')
COLOR_ATTR = DEFAULT_COLOR_ATTR
return COLOR_ATTR
def _disable_acs():
"""
Replaces ACS characters used for showing borders. This can be preferable if
curses is `unable to render them
<https://www.atagar.com/arm/images/acs_display_failure.png>`_.
"""
for item in curses.__dict__:
if item.startswith('ACS_'):
curses.__dict__[item] = ord('+')
# replace common border pipe cahracters
curses.ACS_SBSB = ord('|')
curses.ACS_VLINE = ord('|')
curses.ACS_BSBS = ord('-')
curses.ACS_HLINE = ord('-')
def is_wide_characters_supported():
"""
Checks if our version of curses has wide character support. This is required
to print unicode.
:returns: **bool** that's **True** if curses supports wide characters, and
**False** if it either can't or this can't be determined
"""
try:
# Gets the dynamic library used by the interpretor for curses. This uses
# 'ldd' on Linux or 'otool -L' on OSX.
#
# atagar@fenrir:~/Desktop$ ldd /usr/lib/python2.6/lib-dynload/_curses.so
# linux-gate.so.1 => (0x00a51000)
# libncursesw.so.5 => /lib/libncursesw.so.5 (0x00faa000)
# libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0x002f1000)
# libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00158000)
# libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0x00398000)
# /lib/ld-linux.so.2 (0x00ca8000)
#
# atagar$ otool -L /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/_curses.so
# /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/_curses.so:
# /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
# /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
# /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.6)
import _curses
if stem.util.system.is_available('ldd'):
return 'libncursesw' in '\n'.join(lib_dependency_lines = stem.util.system.call('ldd %s' % _curses.__file__))
elif stem.util.system.is_available('otool'):
return 'libncursesw' in '\n'.join(lib_dependency_lines = stem.util.system.call('otool -L %s' % _curses.__file__))
except:
pass
return False
def draw(func, left = 0, top = 0, width = None, height = None, background = None, draw_if_resized = None):
"""
Renders a subwindow. This calls the given draw function with a
:class:`~nyx.curses._Subwindow`.
:param function func: draw function for rendering the subwindow
:param int left: left position of the panel
:param int top: top position of the panel
:param int width: panel width, uses all available space if **None**
:param int height: panel height, uses all available space if **None**
:param nyx.curses.Color background: background color, unset if **None**
:param nyx.curses.Dimension draw_if_resized: only draw content if
dimentions have changed from this
:returns: :class:`~nyx.curses.Dimension` for the space we drew within
"""
start = time.time()
while not CURSES_LOCK.acquire(False):
if (time.time() - start) > 1:
return # if we've been blocked from drawing for a full second then abort
time.sleep(0.05)
try:
if HALT_ACTIVITY:
return
dimensions = screen_size()
subwindow_width = max(0, dimensions.width - left)
subwindow_height = max(0, dimensions.height - top)
if width:
subwindow_width = min(width, subwindow_width)
if height:
subwindow_height = min(height, subwindow_height)
subwindow_dimensions = Dimensions(subwindow_width, subwindow_height)
if subwindow_dimensions == draw_if_resized:
return subwindow_dimensions # draw size hasn't changed
curses_subwindow = CURSES_SCREEN.subwin(subwindow_height, subwindow_width, top, left)
curses_subwindow.erase()
if background:
curses_subwindow.bkgd(' ', curses_attr(background, HIGHLIGHT))
func(_Subwindow(subwindow_width, subwindow_height, curses_subwindow))
curses_subwindow.refresh()
return subwindow_dimensions
except curses.error:
return # raw curses access, such as subwin, can raise in edge cases such as resizing
finally:
CURSES_LOCK.release()
class _Subwindow(object):
"""
Subwindow that can be drawn within.
:var int width: subwindow width
:var int height: subwindow height
"""
def __init__(self, width, height, curses_subwindow):
self.width = width
self.height = height
self._curses_subwindow = curses_subwindow
def addstr(self, x, y, msg, *attr):
"""
Draws a string in the subwindow.
:param int x: horizontal location
:param int y: vertical location
:param str msg: string to be written
:param list attr: text attributes to apply
:returns: **int** with the horizontal position we drew to
"""
if self.width > x and self.height > y:
try:
cropped_msg = msg[:self.width - x]
self._curses_subwindow.addstr(y, x, cropped_msg, curses_attr(*attr))
return x + len(cropped_msg)
except:
pass
return x
def addstr_wrap(self, x, y, msg, width, min_x = 0, *attr):
"""
Draws a string in the subwindow, with text wrapped if it exceeds a width.
:param int x: horizontal location
:param int y: vertical location
:param str msg: string to be written
:param int width: width avaialble to render the string
:param int min_x: horizontal position to wrap to on new lines
:param list attr: text attributes to apply
:returns: **tuple** of the (x, y) position we drew to
"""
orig_y = y
while msg:
draw_msg, msg = stem.util.str_tools.crop(msg, width - x, None, ending = None, get_remainder = True)
if not draw_msg:
draw_msg, msg = stem.util.str_tools.crop(msg, width - x), '' # first word is longer than the line
x = self.addstr(x, y, draw_msg, *attr)
msg = msg.lstrip()
if (y - orig_y + 1) >= CONFIG['max_line_wrap']:
break # maximum number we'll wrap
if msg:
x, y = min_x, y + 1
return x, y
def box(self, left = 0, top = 0, width = None, height = None, *attr):
"""
Draws a box with the given bounds.
:param int left: left position of the box
:param int top: top position of the box
:param int width: box width, uses all available space if **None**
:param int height: box height, uses all available space if **None**
:param list attr: text attributes to apply
"""
if width is None:
width = self.width - left
if height is None:
height = self.height - top
self.hline(left + 1, top, width - 2, *attr) # top
self.hline(left + 1, top + height - 1, width - 2, *attr) # bottom
self.vline(left, top + 1, height - 2, *attr) # left
self.vline(left + width - 1, top + 1, height - 2, *attr) # right
self._addch(left, top, curses.ACS_ULCORNER, *attr) # upper left corner
self._addch(left, top + height - 1, curses.ACS_LLCORNER, *attr) # lower left corner
self._addch(left + width - 1, top, curses.ACS_URCORNER, *attr) # upper right corner
self._addch(left + width - 1, top + height - 1, curses.ACS_LRCORNER, *attr) # lower right corner
def scrollbar(self, top, top_index, size, fill_char = ' '):
"""
Draws a left justified scrollbar reflecting position within a vertical
listing. The bottom is squared off, having a layout like:
::
-+---------------------------
| lovely content we're
*| showing in the window
*|
*|
|
-+
:param int top: top row in the subwindow where the scrollbar should be drawn
:param int top_index: list index for the top-most visible element
:param int size: size of the list in which the listed elements are contained
:param str fill_char: character to use for scrollbar handle
"""
if (self.height - top) < 2:
return # not enough room
# determines scrollbar dimensions
scrollbar_height = self.height - top - 1 # -1 is for the bottom border
# Bottom index of the scroll bar. We show (scrollbar_height + 1) items at a
# time but have -1 to make the bottom bound inclusive.
bottom_index = top_index + scrollbar_height
slider_top = (scrollbar_height * top_index) // size
slider_size = (scrollbar_height * (bottom_index - top_index + 1)) // size
max_slider_top = scrollbar_height - slider_size - 1
# Ensures slider isn't at top or bottom unless really at those extreme
# bounds. This is an index vs size comparison, hence the -1 offset.
slider_top = 0 if top_index == 0 else max(slider_top, 1)
slider_top = max_slider_top if bottom_index == size - 1 else min(slider_top, max_slider_top - 1)
# draws scrollbar slider
for i in range(scrollbar_height):
if i >= slider_top and i <= slider_top + slider_size:
self.addstr(0, i + top, fill_char, Attr.HIGHLIGHT)
else:
self.addstr(0, i + top, ' ')
# draws box around the scrollbar
self.vline(1, top, self.height - 2)
self._addch(1, self.height - 1, curses.ACS_LRCORNER)
self._addch(0, self.height - 1, curses.ACS_HLINE)
def _addch(self, x, y, char, *attr):
if self.width > x and self.height > y:
try:
self._curses_subwindow.addch(y, x, char, curses_attr(*attr))
return x + 1
except:
pass
return x
def hline(self, x, y, length, *attr, **kwargs):
char = kwargs.get('char', curses.ACS_HLINE)
char = ord(char) if isinstance(char, str) else char
if self.width > x and self.height > y and length > 0:
try:
self._curses_subwindow.hline(max(0, y), max(0, x), char | curses_attr(*attr), min(length, self.width - x))
except:
pass
def vline(self, x, y, length, *attr, **kwargs):
char = kwargs.get('char', curses.ACS_VLINE)
char = ord(char) if isinstance(char, str) else char
if self.width > x and self.height > y and length > 0:
try:
self._curses_subwindow.vline(max(0, y), max(0, x), char | curses_attr(*attr), min(length, self.height - y))
except:
pass
class KeyInput(object):
"""
Keyboard input by the user.
"""
def __init__(self, key):
self._key = key # pressed key as an integer
def match(self, *keys):
"""
Checks if we have a case insensitive match with the given key. Beside
characters, this also recognizes: up, down, left, right, home, end,
page_up, page_down, and esc.
"""
if self.is_null():
return False
for key in keys:
if key in SPECIAL_KEYS:
if self._key == SPECIAL_KEYS[key]:
return True
elif len(key) == 1:
if self._key in (ord(key.lower()), ord(key.upper())):
return True
else:
raise ValueError("%s wasn't among our recognized key codes" % key)
return False
def is_null(self):
"""
True if there wasn't a key event (key_input() timed out).
"""
return self._key == -1
def is_scroll(self):
"""
True if the key is used for scrolling, false otherwise.
"""
return self._key in SCROLL_KEYS
def is_selection(self):
"""
True if the key matches the enter or space keys.
"""
return self._key in (curses.KEY_ENTER, 10, ord(' '))
def __eq__(self, other):
if isinstance(other, KeyInput):
return self._key == other._key
else:
return False
def __ne__(self, other):
return not self == other
class Scroller(object):
"""
Simple scroller that provides keyboard navigation of content.
"""
def __init__(self):
self._location = 0
def location(self, content_height = None, page_height = None):
"""
Provides the position we've scrolled to.
If a **content_height** and **page_height** are provided this ensures our
scroll position falls within a valid range. This should be done when the
content changes or panel resized.
:param int content_height: height of the content being renered
:param int page_height: height visible on the page
:returns: **int** position we've scrolled to
"""
if content_height is not None and page_height is not None:
self._location = max(0, min(self._location, content_height - page_height))
return self._location
def handle_key(self, key, content_height, page_height):
"""
Moves scrolling location according to the given input...
* up / down - scrolls one position up or down
* page up / page down - scrolls by the page_height
* home / end - moves to the top or bottom
:param nyx.curses.KeyInput key: pressed key
:param int content_height: height of the content being renered
:param int page_height: height visible on the page
:returns: **bool** that's **True** if the scrolling position changed and
**False** otherwise
"""
new_location = _scroll_position(self._location, key, content_height, page_height, False)
if new_location != self._location:
self._location = new_location
return True
else:
return False
class CursorScroller(object):
"""
Scroller that tracks a cursor's position.
"""
def __init__(self):
self._location = 0
# We track the cursor location by the item we have selected, so it stays
# selected as the content changes. We also keep track of its last location
# so we can fall back to that if it disappears.
self._cursor_location = 0
self._cursor_selection = None
def selection(self, content, page_height = None):
"""
Provides the item from the content that's presently selected. If provided
the height of our page this provides the scroll position as well...
::
selected, scroll = my_scroller.selection(content, page_height)
:param list content: content the scroller is tracking
:param int page_height: height visible on the page
:returns: **tuple** of the form **(cursor, scroll)**, the cursor is
**None** if content is empty
"""
content = list(content) # shallow copy for thread safety
if not content:
self._cursor_location = 0
self._cursor_selection = None
return None if page_height is None else None, 0
if self._cursor_selection in content:
# moves cursor location to track the selection
self._cursor_location = content.index(self._cursor_selection)
else:
# select the next closest entry
self._cursor_location = max(0, min(self._cursor_location, len(content) - 1))
self._cursor_selection = content[self._cursor_location]
# ensure our cursor is visible
if page_height:
if self._cursor_location < self._location:
self._location = self._cursor_location
elif self._cursor_location > self._location + page_height - 1:
self._location = self._cursor_location - page_height + 1
if page_height is None:
return self._cursor_selection
else:
return self._cursor_selection, self._location
def handle_key(self, key, content, page_height):
self.selection(content, page_height) # reset cursor position
new_location = _scroll_position(self._cursor_location, key, len(content), page_height, True)
if new_location != self._cursor_location:
self._cursor_location = new_location
self._cursor_selection = content[new_location]
return True
else:
return False
def _scroll_position(location, key, content_height, page_height, is_cursor):
if key.match('up'):
shift = -1
elif key.match('down'):
shift = 1
elif key.match('page_up'):
shift = -page_height + 1 if is_cursor else -page_height
elif key.match('page_down'):
shift = page_height - 1 if is_cursor else page_height
elif key.match('home'):
shift = -content_height
elif key.match('end'):
shift = content_height
else:
return location
max_position = content_height - 1 if is_cursor else content_height - page_height
return max(0, min(location + shift, max_position))
|