Skip to content

Commit ab58b65

Browse files
committed
Add Settings`$PygmentsShowTokens
1 parent c757996 commit ab58b65

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

mathicsscript/__main__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,13 @@ def main(
247247
# Set a default value for $ShowFullFormInput to False.
248248
# Then, it can be changed by the settings file (in WL)
249249
# and overwritten by the command line parameter.
250-
definitions.set_ownvalue("Settings`$ShowFullFormInput", from_python(False))
250+
definitions.set_ownvalue(
251+
"Settings`$ShowFullFormInput", from_python(1 if full_form else 0)
252+
)
253+
definitions.set_ownvalue(
254+
"Settings`$PygmentsShowTokens", from_python(1 if pygments_tokens else 0)
255+
)
256+
251257
shell = TerminalShell(definitions, style, readline, completion)
252258
load_settings(shell)
253259
if initfile:
@@ -277,7 +283,7 @@ def main(
277283
shell.definitions, output=TerminalOutput(shell), format="text"
278284
)
279285
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
280-
shell.print_result(result, debug_pygments=pygments_tokens)
286+
shell.print_result(result)
281287

282288
# After the next release, we can remove the hasattr test.
283289
if hasattr(evaluation, "exc_result"):
@@ -326,6 +332,9 @@ def main(
326332
definitions.set_ownvalue(
327333
"Settings`$PygmentsStyle", from_python(shell.pygments_style)
328334
)
335+
definitions.set_ownvalue(
336+
"Settings`$PygmentsShowTokens", from_python(pygments_tokens)
337+
)
329338

330339
TeXForm = Symbol("System`TeXForm")
331340

@@ -369,7 +378,7 @@ def main(
369378
print(fmt(query))
370379
result = evaluation.evaluate(query, timeout=settings.TIMEOUT)
371380
if result is not None:
372-
shell.print_result(result, output_style, debug_pygments=pygments_tokens)
381+
shell.print_result(result, output_style)
373382
except (KeyboardInterrupt):
374383
print("\nKeyboardInterrupt")
375384
except EOFError:

mathicsscript/settings.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@
1212
Note this is for input entered, not the output of the evaluated result.
1313
"
1414

15-
1615
Settings`$ShowFullFormInput = 0
17-
18-
1916
Settings`$PygmentsStyle::usage = "This sets the Pygments style used to colorize output. The value should be a string.
2017
2118
The default value changes background depending on whether the terminal has a light or dark background. You can also set the color style used on the command with the ``--style`` option, or look at the variable ```Settings`PygmentsStylesAvailable```. Or it can be set in the settings.m file."
2219

23-
24-
25-
20+
Settings`$PygmentsShowTokens::usage = "Setting this 1 will show Pygments tokenization of the output."

mathicsscript/termshell.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from mathics.core.rules import Rule
1515
from mathics.core.characters import named_characters
1616

17-
from pygments import highlight
18-
from mathematica.lexer import MathematicaLexer, MToken
17+
from pygments import highlight, lex
18+
from mathematica.lexer import MathematicaLexer
1919

2020
mma_lexer = MathematicaLexer()
2121

@@ -30,7 +30,7 @@
3030
# Keyword,
3131
Name,
3232
Literal,
33-
# Operator,
33+
Operator,
3434
# String,
3535
Token,
3636
)
@@ -40,7 +40,7 @@
4040
color_scheme = TERMINAL_COLORS.copy()
4141
color_scheme[Token.Name] = ("yellow", "ansibrightyellow")
4242
color_scheme[Name.Function] = ("ansigreen", "ansibrightgreen")
43-
color_scheme[Name.NameSpace] = ("magenta", "ansibrightmagenta")
43+
color_scheme[Operator] = ("magenta", "ansibrightmagenta")
4444
color_scheme[Literal.Number] = ("ansiblue", "ansibrightblue")
4545

4646
from colorama import init as colorama_init
@@ -152,6 +152,9 @@ def __init__(
152152

153153
self.pygments_style = style
154154
self.definitions = definitions
155+
self.definitions.set_ownvalue(
156+
"Settings`$PygmentsShowTokens", from_python(False)
157+
)
155158
self.definitions.set_ownvalue("Settings`$PygmentsStyle", from_python(style))
156159
self.definitions.set_ownvalue(
157160
"Settings`PygmentsStylesAvailable", from_python(ALL_PYGMENTS_STYLES)
@@ -214,15 +217,13 @@ def read_line(self, prompt):
214217
return self.rl_read_line(prompt)
215218
return input(prompt)
216219

217-
def print_result(self, result, output_style="", debug_pygments=False):
220+
def print_result(self, result, output_style=""):
218221
if result is not None and result.result is not None:
219222
out_str = str(result.result)
220223
if self.terminal_formatter: # pygmentize
221-
from pygments import lex
222-
223-
if debug_pygments:
224-
print(list(lex(out_str, mma_lexer)))
225-
224+
show_pygments_tokens = self.definitions.get_ownvalue(
225+
"Settings`$PygmentsShowTokens"
226+
).replace.to_python()
226227
pygments_style = self.definitions.get_ownvalue(
227228
"Settings`$PygmentsStyle"
228229
).replace.get_string_value()
@@ -232,6 +233,8 @@ def print_result(self, result, output_style="", debug_pygments=False):
232233
"Settings`$PygmentsStyle", String(self.pygments_style)
233234
)
234235

236+
if show_pygments_tokens:
237+
print(list(lex(out_str, mma_lexer)))
235238
out_str = highlight(out_str, mma_lexer, self.terminal_formatter)
236239
output = self.to_output(out_str)
237240
print(self.get_out_prompt(output_style) + output + "\n")

settings/settings.m

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)