Skip to content

Commit 73ef437

Browse files
committed
Sync "lib.format" with trepan3k
1 parent 14aa314 commit 73ef437

1 file changed

Lines changed: 78 additions & 36 deletions

File tree

trepan/lib/format.py

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2013, 2015, 2017, 2019, 2020 Rocky Bernstein <rocky@gnu.org>
2+
3+
# Copyright (C) 2013, 2015, 2017, 2019, 2020, 2023-2024
4+
# Rocky Bernstein <rocky@gnu.org>
35
#
46
# This program is free software: you can redistribute it and/or modify
57
# it under the terms of the GNU General Public License as published by
@@ -15,9 +17,11 @@
1517
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1618
"""Pygments-related terminal formatting"""
1719

18-
import re, sys
20+
import re
21+
import sys
22+
1923
import pyficache
20-
from pygments import highlight, lex
24+
from pygments import highlight, lex, __version__ as pygments_version
2125
from pygments.console import ansiformat
2226
from pygments.filter import Filter
2327
from pygments.formatter import Formatter
@@ -34,26 +38,58 @@
3438
String,
3539
Token,
3640
)
37-
from pygments.util import get_choice_opt
3841

42+
from trepan.lib.default import DEBUGGER_SETTINGS
3943

40-
# Set up my own color scheme with some addtional definitions
44+
# Set up my own color scheme with some additional definitions.
4145
color_scheme = TERMINAL_COLORS.copy()
46+
# color_scheme = {
47+
# Token: ('', ''),
48+
# Comment: ('gray', 'brightblack'),
49+
# Comment.Preproc: ('cyan', 'brightcyan'),
50+
# Keyword: ('blue', 'brightblue'),
51+
# Keyword.Type: ('cyan', 'brightcyan'),
52+
# Operator.Word: ('magenta', 'brightmagenta'),
53+
# Name.Builtin: ('cyan', 'brightcyan'),
54+
# Name.Function: ('green', 'brightgreen'),
55+
# Name.Namespace: ('_cyan_', '_brightcyan_'),
56+
# Name.Class: ('_green_', '_brightgreen_'),
57+
# Name.Exception: ('cyan', 'brightcyan'),
58+
# Name.Decorator: ('brightblack', 'gray'),
59+
# Name.Variable: ('red', 'brightred'),
60+
# Name.Constant: ('red', 'brightred'),
61+
# Name.Attribute: ('cyan', 'brightcyan'),
62+
# Name.Tag: ('brightblue', 'brightblue'),
63+
# String: ('gray', 'yellow'),
64+
# Number: ('blue', 'brightblue'),
65+
# Generic.Deleted: ('brightred', 'brightred'),
66+
# Generic.Inserted: ('green', 'brightgreen'),
67+
# Generic.Heading: ('**', '**'),
68+
# Generic.Subheading: ('*magenta*', '*brightmagenta*'),
69+
# Generic.Prompt: ('**', '**'),
70+
# Generic.Error: ('brightred', 'brightred'),
71+
# }
72+
73+
4274
color_scheme[Generic.Strong] = ("*black*", "*white*")
4375
color_scheme[Name.Variable] = ("_black_", "_white_")
4476

4577
color_scheme[Generic.Strong] = ("*black*", "*white*")
4678
color_scheme[Name.Variable] = ("_black_", "_white_")
47-
color_scheme[Generic.Emph] = color_scheme[Comment.Preproc]
79+
color_scheme[Generic.Emph] = ("blue", "brightcyan")
80+
81+
pygments_version_tuple = tuple([int(num) for num in pygments_version.split(".")[:2]])
82+
purple = "magenta" if pygments_version_tuple >= (2, 5) else "purple"
83+
color_scheme[Token.Literal.String] = (purple, "yellow")
4884

4985
# Assume pygments has fixed up the horrible atom colors
50-
## FIXME: change some horrible colors under atom dark
51-
## this is a hack until I get general way to do colorstyle setting
52-
## color_scheme[Token.Comment] = ('darkgray', 'white')
53-
## color_scheme[Token.Keyword] = ('darkblue', 'green')
54-
## color_scheme[Token.Number] = ('darkblue', 'blue')
55-
## color_scheme[Keyword] = ('darkblue', 'turquoise')
56-
## color_scheme[Number] = ('darkblue', 'green')
86+
# FIXME: change some horrible colors under atom dark
87+
# this is a hack until I get general way to do colorstyle setting
88+
# color_scheme[Token.Comment] = ('darkgray', 'white')
89+
# color_scheme[Token.Keyword] = ('darkblue', 'green')
90+
# color_scheme[Token.Number] = ('darkblue', 'blue')
91+
# color_scheme[Keyword] = ('darkblue', 'turquoise')
92+
# color_scheme[Number] = ('darkblue', 'green')
5793

5894
pyficache.dark_terminal_formatter.colorscheme = color_scheme
5995
pyficache.light_terminal_formatter.colorscheme = color_scheme
@@ -62,11 +98,13 @@
6298
def format_token(ttype, token, colorscheme=color_scheme, highlight="light"):
6399
if "plain" == highlight:
64100
return token
65-
dark_bg = "dark" == highlight
66-
101+
is_dark_bg = 1 if DEBUGGER_SETTINGS["highlight"] == "dark" else 0
67102
color = colorscheme.get(ttype)
68103
if color:
69-
color = color[dark_bg]
104+
color = color[is_dark_bg]
105+
if isinstance(token, tuple):
106+
# have (token, start offset)
107+
token = token[0]
70108
return ansiformat(color, token)
71109
pass
72110
return token
@@ -120,7 +158,7 @@ def filter(self, lexer, stream):
120158
# That is remove:
121159
# Header
122160
# ------ <- remove this line
123-
if last_was_heading_title and re.match(r"^(?:[=]|[-])+$", value):
161+
if last_was_heading_title and re.match(r"^(?:=|-)+$", value):
124162
value = ""
125163
last_was_heading_title = ""
126164
else:
@@ -154,15 +192,14 @@ class RSTTerminalFormatter(Formatter):
154192
A dictionary mapping token types to (lightbg, darkbg) color names or
155193
``None`` (default: ``None`` = use builtin colorscheme).
156194
"""
195+
157196
name = "Terminal"
158197
aliases = ["terminal", "console"]
159198
filenames = []
160199

161200
def __init__(self, **options):
162201
Formatter.__init__(self, **options)
163-
self.darkbg = (
164-
get_choice_opt(options, "bg", ["light", "dark"], "light") != "dark"
165-
)
202+
self.is_dark_bg = 1 if DEBUGGER_SETTINGS["highlight"] == "dark" else 0
166203
self.colorscheme = options.get("colorscheme", None) or color_scheme
167204
self.width = options.get("width", 80)
168205
self.verbatim = False
@@ -197,7 +234,7 @@ def write_verbatim(self, text):
197234
# color
198235
if self.__class__ != MonoRSTTerminalFormatter:
199236
cs = self.colorscheme.get(Verbatim)
200-
color = cs[self.darkbg]
237+
color = cs[self.is_dark_bg]
201238
else:
202239
color = None
203240
pass
@@ -220,7 +257,7 @@ def reflow_text(self, text, ttype, color):
220257
# print '%r' % text
221258
# from trepan.api import debug
222259
# if u' or ' == text: debug()
223-
if text == u'::' and ttype == Token.Literal.String.Escape:
260+
if text == u'::' and ttype == Token.Literal.String:
224261
self.verbatim = "colon-verbatim"
225262
return
226263
elif (
@@ -229,51 +266,58 @@ def reflow_text(self, text, ttype, color):
229266
self.write_nl()
230267
self.last_was_nl = True
231268
return
232-
elif self.verbatim == 'colon-verbatim' and ttype == Token.Text and text == '\n':
269+
elif self.verbatim == "colon-verbatim" and ttype == Token.Text and text == "\n":
233270
self.write_nl()
234271
self.last_was_nl = False
235272
return
236273
last_last_nl = self.last_was_nl
237-
if text == '':
274+
if text == "":
238275
pass
239-
elif text[-1] == '\n':
276+
elif text[-1] == "\n":
240277
if self.last_was_nl:
241278
self.write_nl()
242279
self.write_nl()
243280
text = text[:-1]
244281
elif self.verbatim:
245282
self.write_verbatim(text)
246283
self.column = 0
247-
self.verbatim = len(text) >=2 and text[-2] == '\n'
284+
self.verbatim = len(text) >= 2 and text[-2] == "\n"
248285
self.last_was_nl = True
249286
return
250287
else:
251-
self.write(' ', color)
288+
self.write(" ", color)
252289
text = text[:-1]
253290
pass
254291
self.last_was_nl = True
255-
if '' == text: return
256-
while text[-1] == '\n':
292+
if "" == text:
293+
return
294+
while text[-1] == "\n":
257295
self.write_nl()
258296
text = text[:-1]
259-
if '' == text: return
297+
if "" == text:
298+
return
260299
pass
261300
pass
262301
else:
263302
self.last_was_nl = False
264303
pass
265304
self.in_list = False
266305
if last_last_nl:
267-
if ' * ' == text[0:3]: self.in_list = True
268-
elif ' ' == text[0:2]: self.verbatim = self.verbatim or True
306+
if " * " == text[0:3]:
307+
self.in_list = True
308+
elif " " == text[0:2]:
309+
self.verbatim = self.verbatim or True
269310
pass
270311

271312
# FIXME: there may be nested lists, tables and so on.
272313
if self.verbatim:
273314
self.write_verbatim(text)
274315
elif self.in_list:
275316
# FIXME:
276-
self.write(text, color,)
317+
self.write(
318+
text,
319+
color,
320+
)
277321
else:
278322
words = re.compile("[ \t]+").split(text)
279323
for word in words[:-1]:
@@ -305,7 +349,7 @@ def format_unencoded(self, tokensource, outfile):
305349
color = self.colorscheme.get(resolved_type)
306350
pass
307351
if color:
308-
color = color[self.darkbg]
352+
color = color[self.is_dark_bg]
309353
self.reflow_text(text, ttype, color)
310354
pass
311355
return
@@ -339,7 +383,6 @@ def format_unencoded(self, tokensource, outfile):
339383
text = '"%s"' % text
340384
pass
341385
elif ttype is Token.Generic.Emph:
342-
type
343386
text = "*%s*" % text
344387
pass
345388
elif ttype is Token.Generic.Strong:
@@ -411,7 +454,6 @@ def show_it(string, tf, width=80):
411454
show_it(test_string, rst_tf, 30)
412455

413456
text = """**break** [*location*] [if *condition*]]
414-
415457
With a line number argument, set a break there in the current file.
416458
With a function name, set a break at first executable line of that
417459
function. Without argument, set a breakpoint at current location. If

0 commit comments

Comments
 (0)