• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jupyter / qtconsole / 11486866498

23 Oct 2024 07:32PM UTC coverage: 61.909% (-0.09%) from 61.996%
11486866498

push

github

web-flow
Merge pull request #616 from jsbautista/TQDMQtConsole

Add logic to handle ANSI escape sequences to move cursor

16 of 30 new or added lines in 2 files covered. (53.33%)

3 existing lines in 3 files now uncovered.

2932 of 4736 relevant lines covered (61.91%)

1.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.84
/qtconsole/ansi_code_processor.py
1
""" Utilities for processing ANSI escape codes and special ASCII characters.
2
"""
3
#-----------------------------------------------------------------------------
4
# Imports
5
#-----------------------------------------------------------------------------
6

7
# Standard library imports
8
from collections import namedtuple
3✔
9
import re
3✔
10

11
# System library imports
12
from qtpy import QtGui
3✔
13

14
# Local imports
15
from qtconsole.styles import dark_style
3✔
16

17
#-----------------------------------------------------------------------------
18
# Constants and datatypes
19
#-----------------------------------------------------------------------------
20

21
# An action for erase requests (ED and EL commands).
22
EraseAction = namedtuple('EraseAction', ['action', 'area', 'erase_to'])
3✔
23

24
# An action for cursor move requests (CUU, CUD, CUF, CUB, CNL, CPL, CHA, CUP,
25
# and HVP commands).
26
# FIXME: Not implemented in AnsiCodeProcessor.
27
MoveAction = namedtuple('MoveAction', ['action', 'dir', 'unit', 'count'])
3✔
28

29
# An action for scroll requests (SU and ST) and form feeds.
30
ScrollAction = namedtuple('ScrollAction', ['action', 'dir', 'unit', 'count'])
3✔
31

32
# An action for the carriage return character
33
CarriageReturnAction = namedtuple('CarriageReturnAction', ['action'])
3✔
34

35
# An action for the \n character
36
NewLineAction = namedtuple('NewLineAction', ['action'])
3✔
37

38
# An action for the beep character
39
BeepAction = namedtuple('BeepAction', ['action'])
3✔
40

41
# An action for backspace
42
BackSpaceAction = namedtuple('BackSpaceAction', ['action'])
3✔
43

44
# Regular expressions.
45
CSI_COMMANDS = 'ABCDEFGHJKSTfmnsu'
3✔
46
CSI_SUBPATTERN = '\\[(.*?)([%s])' % CSI_COMMANDS
3✔
47
OSC_SUBPATTERN = '\\](.*?)[\x07\x1b]'
3✔
48
ANSI_PATTERN = ('\x01?\x1b(%s|%s)\x02?' % \
3✔
49
                (CSI_SUBPATTERN, OSC_SUBPATTERN))
50
ANSI_OR_SPECIAL_PATTERN = re.compile('(\a|\b|\r(?!\n)|\r?\n)|(?:%s)' % ANSI_PATTERN)
3✔
51
SPECIAL_PATTERN = re.compile('([\f])')
3✔
52

53
#-----------------------------------------------------------------------------
54
# Classes
55
#-----------------------------------------------------------------------------
56

57
class AnsiCodeProcessor(object):
3✔
58
    """ Translates special ASCII characters and ANSI escape codes into readable
59
        attributes. It also supports a few non-standard, xterm-specific codes.
60
    """
61

62
    # Whether to increase intensity or set boldness for SGR code 1.
63
    # (Different terminals handle this in different ways.)
64
    bold_text_enabled = True
3✔
65

66
    # We provide an empty default color map because subclasses will likely want
67
    # to use a custom color format.
68
    default_color_map = {}
3✔
69

70
    #---------------------------------------------------------------------------
71
    # AnsiCodeProcessor interface
72
    #---------------------------------------------------------------------------
73

74
    def __init__(self):
3✔
75
        self.actions = []
3✔
76
        self.color_map = self.default_color_map.copy()
3✔
77
        self.reset_sgr()
3✔
78

79
    def reset_sgr(self):
3✔
80
        """ Reset graphics attributs to their default values.
81
        """
82
        self.intensity = 0
3✔
83
        self.italic = False
3✔
84
        self.bold = False
3✔
85
        self.underline = False
3✔
86
        self.foreground_color = None
3✔
87
        self.background_color = None
3✔
88

89
    def split_string(self, string):
3✔
90
        """ Yields substrings for which the same escape code applies.
91
        """
92
        self.actions = []
3✔
93
        start = 0
3✔
94

95
        last_char = None
3✔
96
        string = string[:-1] if last_char is not None else string
3✔
97

98
        for match in ANSI_OR_SPECIAL_PATTERN.finditer(string):
3✔
99
            raw = string[start:match.start()]
3✔
100
            substring = SPECIAL_PATTERN.sub(self._replace_special, raw)
3✔
101
            if substring or self.actions:
3✔
102
                yield substring
3✔
103
                self.actions = []
3✔
104
            start = match.end()
3✔
105

106
            groups = [g for g in match.groups() if (g is not None)]
3✔
107
            g0 = groups[0]
3✔
108
            if g0 == '\a':
3✔
109
                self.actions.append(BeepAction('beep'))
3✔
110
                yield None
3✔
111
                self.actions = []
3✔
112
            elif g0 == '\r':
3✔
113
                self.actions.append(CarriageReturnAction('carriage-return'))
3✔
114
                yield None
3✔
115
                self.actions = []
3✔
116
            elif g0 == '\b':
3✔
117
                self.actions.append(BackSpaceAction('backspace'))
3✔
118
                yield None
3✔
119
                self.actions = []
3✔
120
            elif g0 == '\n' or g0 == '\r\n':
3✔
121
                self.actions.append(NewLineAction('newline'))
3✔
122
                yield None
3✔
123
                self.actions = []
3✔
124
            else:
125
                params = [ param for param in groups[1].split(';') if param ]
3✔
126
                if g0.startswith('['):
3✔
127
                    # Case 1: CSI code.
128
                    try:
3✔
129
                        params = list(map(int, params))
3✔
130
                    except ValueError:
×
131
                        # Silently discard badly formed codes.
132
                        pass
×
133
                    else:
134
                        self.set_csi_code(groups[2], params)
3✔
135

136
                elif g0.startswith(']'):
3✔
137
                    # Case 2: OSC code.
138
                    self.set_osc_code(params)
3✔
139

140
        raw = string[start:]
3✔
141
        substring = SPECIAL_PATTERN.sub(self._replace_special, raw)
3✔
142
        if substring or self.actions:
3✔
143
            yield substring
3✔
144

145
        if last_char is not None:
3✔
UNCOV
146
            self.actions.append(NewLineAction('newline'))
×
NEW
147
            yield None
×
148

149
    def set_csi_code(self, command, params=[]):
3✔
150
        """ Set attributes based on CSI (Control Sequence Introducer) code.
151

152
        Parameters
153
        ----------
154
        command : str
155
            The code identifier, i.e. the final character in the sequence.
156

157
        params : sequence of integers, optional
158
            The parameter codes for the command.
159
        """
160
        if command == 'm':   # SGR - Select Graphic Rendition
3✔
161
            if params:
3✔
162
                self.set_sgr_code(params)
3✔
163
            else:
164
                self.set_sgr_code([0])
×
165

166
        elif (command == 'J' or # ED - Erase Data
3✔
167
              command == 'K'):  # EL - Erase in Line
168
            code = params[0] if params else 0
3✔
169
            if 0 <= code <= 2:
3✔
170
                area = 'screen' if command == 'J' else 'line'
3✔
171
                if code == 0:
3✔
172
                    erase_to = 'end'
3✔
173
                elif code == 1:
3✔
174
                    erase_to = 'start'
3✔
175
                elif code == 2:
3✔
176
                    erase_to = 'all'
3✔
177
                self.actions.append(EraseAction('erase', area, erase_to))
3✔
178

179
        elif (command == 'S' or # SU - Scroll Up
3✔
180
              command == 'T'):  # SD - Scroll Down
181
            dir = 'up' if command == 'S' else 'down'
3✔
182
            count = params[0] if params else 1
3✔
183
            self.actions.append(ScrollAction('scroll', dir, 'line', count))
3✔
184

185
        elif command == 'A':  # Move N lines Up
3✔
186
            dir = 'up'
3✔
187
            count = params[0] if params else 1
3✔
188
            self.actions.append(MoveAction('move', dir, 'line', count))
3✔
189

190
        elif command == 'B':  # Move N lines Down
3✔
NEW
191
            dir = 'down'
×
NEW
192
            count = params[0] if params else 1
×
NEW
193
            self.actions.append(MoveAction('move', dir, 'line', count))
×
194

195
        elif command == 'F':  # Goes back to the begining of the n-th previous line
3✔
196
            dir = 'leftup'
3✔
197
            count = params[0] if params else 1
3✔
198
            self.actions.append(MoveAction('move', dir, 'line', count))
3✔
199
        
200

201
    def set_osc_code(self, params):
3✔
202
        """ Set attributes based on OSC (Operating System Command) parameters.
203

204
        Parameters
205
        ----------
206
        params : sequence of str
207
            The parameters for the command.
208
        """
209
        try:
3✔
210
            command = int(params.pop(0))
3✔
211
        except (IndexError, ValueError):
×
212
            return
×
213

214
        if command == 4:
3✔
215
            # xterm-specific: set color number to color spec.
216
            try:
3✔
217
                color = int(params.pop(0))
3✔
218
                spec = params.pop(0)
3✔
219
                self.color_map[color] = self._parse_xterm_color_spec(spec)
3✔
220
            except (IndexError, ValueError):
×
221
                pass
×
222

223
    def set_sgr_code(self, params):
3✔
224
        """ Set attributes based on SGR (Select Graphic Rendition) codes.
225

226
        Parameters
227
        ----------
228
        params : sequence of ints
229
            A list of SGR codes for one or more SGR commands. Usually this
230
            sequence will have one element per command, although certain
231
            xterm-specific commands requires multiple elements.
232
        """
233
        # Always consume the first parameter.
234
        if not params:
3✔
235
            return
3✔
236
        code = params.pop(0)
3✔
237

238
        if code == 0:
3✔
239
            self.reset_sgr()
3✔
240
        elif code == 1:
3✔
241
            if self.bold_text_enabled:
3✔
242
                self.bold = True
3✔
243
            else:
244
                self.intensity = 1
×
245
        elif code == 2:
3✔
246
            self.intensity = 0
×
247
        elif code == 3:
3✔
248
            self.italic = True
×
249
        elif code == 4:
3✔
250
            self.underline = True
×
251
        elif code == 22:
3✔
252
            self.intensity = 0
×
253
            self.bold = False
×
254
        elif code == 23:
3✔
255
            self.italic = False
×
256
        elif code == 24:
3✔
257
            self.underline = False
×
258
        elif code >= 30 and code <= 37:
3✔
259
            self.foreground_color = code - 30
3✔
260
        elif code == 38 and params:
3✔
261
            _color_type = params.pop(0)
3✔
262
            if _color_type == 5 and params:
3✔
263
                # xterm-specific: 256 color support.
264
                self.foreground_color = params.pop(0)
3✔
265
            elif _color_type == 2:
3✔
266
                # 24bit true colour support.
267
                self.foreground_color = params[:3]
3✔
268
                params[:3] = []
3✔
269
        elif code == 39:
3✔
270
            self.foreground_color = None
3✔
271
        elif code >= 40 and code <= 47:
3✔
272
            self.background_color = code - 40
3✔
273
        elif code == 48 and params:
3✔
274
            _color_type = params.pop(0)
3✔
275
            if _color_type == 5 and params:
3✔
276
                # xterm-specific: 256 color support.
277
                self.background_color = params.pop(0)
3✔
278
            elif _color_type == 2:
3✔
279
                # 24bit true colour support.
280
                self.background_color = params[:3]
3✔
281
                params[:3] = []
3✔
282
        elif code == 49:
3✔
283
            self.background_color = None
3✔
284

285
        # Recurse with unconsumed parameters.
286
        self.set_sgr_code(params)
3✔
287

288
    #---------------------------------------------------------------------------
289
    # Protected interface
290
    #---------------------------------------------------------------------------
291

292
    def _parse_xterm_color_spec(self, spec):
3✔
293
        if spec.startswith('rgb:'):
3✔
294
            return tuple(map(lambda x: int(x, 16), spec[4:].split('/')))
3✔
295
        elif spec.startswith('rgbi:'):
3✔
296
            return tuple(map(lambda x: int(float(x) * 255),
3✔
297
                             spec[5:].split('/')))
298
        elif spec == '?':
×
299
            raise ValueError('Unsupported xterm color spec')
×
300
        return spec
×
301

302
    def _replace_special(self, match):
3✔
303
        special = match.group(1)
3✔
304
        if special == '\f':
3✔
305
            self.actions.append(ScrollAction('scroll', 'down', 'page', 1))
3✔
306
        return ''
3✔
307

308
    def _parse_ansi_color(self, color, intensity):
3✔
309
        """
310
        Map an ANSI color code to color name or a RGB tuple.
311
        Based on: https://gist.github.com/MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b
312
        """
313
        parsed_color = None
3✔
314
        if color < 16:
3✔
315
            # Adjust for intensity, if possible.
316
            if intensity > 0 and color < 8:
3✔
317
                color += 8
×
318
            parsed_color = self.color_map.get(color, None)
3✔
319
        elif (color > 231):
3✔
320
                s = int((color - 232) * 10 + 8)
3✔
321
                parsed_color = (s, s, s)
3✔
322
        else:
323
            n = color - 16
3✔
324
            b = n % 6
3✔
325
            g = (n - b) / 6 % 6
3✔
326
            r = (n - b - g * 6) / 36 % 6
3✔
327
            r = int(r * 40 + 55) if r else 0
3✔
328
            g = int(g * 40 + 55) if g else 0
3✔
329
            b = int(b * 40 + 55) if b else 0
3✔
330
            parsed_color = (r, g, b)
3✔
331
        return parsed_color
3✔
332

333

334
class QtAnsiCodeProcessor(AnsiCodeProcessor):
3✔
335
    """ Translates ANSI escape codes into QTextCharFormats.
336
    """
337

338
    # A map from ANSI color codes to SVG color names or RGB(A) tuples.
339
    darkbg_color_map = {
3✔
340
        0  : 'black',       # black
341
        1  : 'darkred',     # red
342
        2  : 'darkgreen',   # green
343
        3  : 'gold',       # yellow
344
        4  : 'darkblue',    # blue
345
        5  : 'darkviolet',  # magenta
346
        6  : 'steelblue',   # cyan
347
        7  : 'grey',        # white
348
        8  : 'grey',        # black (bright)
349
        9  : 'red',         # red (bright)
350
        10 : 'lime',        # green (bright)
351
        11 : 'yellow',      # yellow (bright)
352
        12 : 'deepskyblue', # blue (bright)
353
        13 : 'magenta',     # magenta (bright)
354
        14 : 'cyan',        # cyan (bright)
355
        15 : 'white' }      # white (bright)
356

357
    # Set the default color map for super class.
358
    default_color_map = darkbg_color_map.copy()
3✔
359

360
    def get_color(self, color, intensity=0):
3✔
361
        """ Returns a QColor for a given color code or rgb list, or None if one
362
            cannot be constructed.
363
        """
364
        if isinstance(color, int):
3✔
365
            constructor = self._parse_ansi_color(color, intensity)
3✔
366
        elif isinstance(color, (tuple, list)):
3✔
367
            constructor = color
×
368
        else:
369
            return None
3✔
370

371
        if isinstance(constructor, str):
3✔
372
            # If this is an X11 color name, we just hope there is a close SVG
373
            # color name. We could use QColor's static method
374
            # 'setAllowX11ColorNames()', but this is global and only available
375
            # on X11. It seems cleaner to aim for uniformity of behavior.
376
            return QtGui.QColor(constructor)
3✔
377

378
        elif isinstance(constructor, (tuple, list)):
3✔
379
            return QtGui.QColor(*constructor)
3✔
380

381
        return None
×
382

383
    def get_format(self):
3✔
384
        """ Returns a QTextCharFormat that encodes the current style attributes.
385
        """
386
        format = QtGui.QTextCharFormat()
3✔
387

388
        # Set foreground color
389
        qcolor = self.get_color(self.foreground_color, self.intensity)
3✔
390
        if qcolor is not None:
3✔
391
            format.setForeground(qcolor)
3✔
392

393
        # Set background color
394
        qcolor = self.get_color(self.background_color, self.intensity)
3✔
395
        if qcolor is not None:
3✔
396
            format.setBackground(qcolor)
3✔
397

398
        # Set font weight/style options
399
        if self.bold:
3✔
400
            format.setFontWeight(QtGui.QFont.Bold)
3✔
401
        else:
402
            format.setFontWeight(QtGui.QFont.Normal)
3✔
403
        format.setFontItalic(self.italic)
3✔
404
        format.setFontUnderline(self.underline)
3✔
405

406
        return format
3✔
407

408
    def set_background_color(self, style):
3✔
409
        """
410
        Given a syntax style, attempt to set a color map that will be
411
        aesthetically pleasing.
412
        """
413
        # Set a new default color map.
414
        self.default_color_map = self.darkbg_color_map.copy()
3✔
415

416
        if not dark_style(style):
3✔
417
            # Colors appropriate for a terminal with a light background. For
418
            # now, only use non-bright colors...
419
            for i in range(8):
3✔
420
                self.default_color_map[i + 8] = self.default_color_map[i]
3✔
421

422
            # ...and replace white with black.
423
            self.default_color_map[7] = self.default_color_map[15] = 'black'
3✔
424

425
        # Update the current color map with the new defaults.
426
        self.color_map.update(self.default_color_map)
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc