Skip to content

Commit 9343528

Browse files
authored
Merge pull request #1362 from mathics/string-output-in-quotes
Surround string output in double quotes.
2 parents 030c36e + cd87f17 commit 9343528

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

mathics/main.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,29 @@ def read_line(self, prompt):
115115
return self.rl_read_line(prompt)
116116
return input(prompt)
117117

118-
def print_result(self, result, no_out_prompt=False):
119-
if result is not None and result.result is not None:
120-
output = self.to_output(str(result.result))
121-
mess = self.get_out_prompt() if not no_out_prompt else ""
122-
print(mess + output + "\n")
118+
def print_result(self, result, no_out_prompt=False, strict_wl_output=False):
119+
if result is None:
120+
# FIXME decide what to do here
121+
return
122+
123+
last_eval = result.last_eval
124+
125+
if last_eval is not None:
126+
try:
127+
eval_type = last_eval.get_head_name()
128+
except:
129+
print(sys.exc_info()[1])
130+
return
131+
132+
out_str = str(result.result)
133+
if eval_type == "System`String" and not strict_wl_output:
134+
out_str = '"' + out_str.replace('"', r'\"') + '"'
135+
if eval_type == "System`Graph":
136+
out_str = "-Graph-"
137+
138+
output = self.to_output(out_str)
139+
mess = self.get_out_prompt() if not no_out_prompt else ""
140+
print(mess + output + "\n")
123141

124142
def rl_read_line(self, prompt):
125143
# Wrap ANSI colour sequences in \001 and \002, so readline
@@ -191,10 +209,9 @@ def main() -> int:
191209
prog="mathics",
192210
usage="%(prog)s [options] [FILE]",
193211
add_help=False,
194-
description="Mathics is a general-purpose computer algebra system.",
195-
epilog="""Please feel encouraged to contribute to Mathics! Create
196-
your own fork, make the desired changes, commit, and make a pull
197-
request.""",
212+
description="A simple command-line interface to Mathics",
213+
epilog="""For a more extensive command-line interface see "mathicsscript".
214+
Please contribute to Mathics!""",
198215
)
199216

200217
argparser.add_argument(
@@ -274,6 +291,12 @@ def main() -> int:
274291
"--version", "-v", action="version", version="%(prog)s " + __version__
275292
)
276293

294+
argparser.add_argument(
295+
"--strict-wl-output",
296+
help="Most WL-output compatible (at the expense of useability).",
297+
action="store_true",
298+
)
299+
277300
args, script_args = argparser.parse_known_args()
278301

279302
quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-D"
@@ -319,7 +342,7 @@ def main() -> int:
319342
for expr in args.execute:
320343
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
321344
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
322-
shell.print_result(result, no_out_prompt=True)
345+
shell.print_result(result, no_out_prompt=True, strict_wl_output=args.strict_wl_output)
323346
if evaluation.exc_result == Symbol("Null"):
324347
exit_rc = 0
325348
elif evaluation.exc_result == Symbol("$Aborted"):
@@ -373,7 +396,7 @@ def main() -> int:
373396
print(query)
374397
result = evaluation.evaluate(query, timeout=settings.TIMEOUT)
375398
if result is not None:
376-
shell.print_result(result)
399+
shell.print_result(result, strict_wl_output=args.strict_wl_output)
377400
except (KeyboardInterrupt):
378401
print("\nKeyboardInterrupt")
379402
except EOFError:

0 commit comments

Comments
 (0)