diff --git a/mathicsscript/__main__.py b/mathicsscript/__main__.py index 2d65795..7a9c74f 100755 --- a/mathicsscript/__main__.py +++ b/mathicsscript/__main__.py @@ -469,29 +469,40 @@ def main( load_settings_file(shell) style_from_settings_file = definitions.get_ownvalue("Settings`$PygmentsStyle") - if style_from_settings_file is SymbolNull and style is None: + if style_from_settings_file is not SymbolNull and style is None: style = style_from_settings_file shell.setup_pygments_style(style) if file: - with open(file, "r") as ifile: - feeder = MathicsFileLineFeeder(ifile) + if not os.path.exists(file): + print(f"\nFile {file} does not exist; skipping reading.") + file = None + elif os.path.isdir(file): + print(f"\nFile {file} does is a directory; skipping reading.") + file = None + else: try: - while not feeder.empty(): - evaluation = Evaluation( - shell.definitions, - output=TerminalOutput(shell), - catch_interrupt=False, - format="text", - ) - query = evaluation.parse_feeder(feeder) - if query is None: - continue - evaluation.evaluate(query, timeout=settings.TIMEOUT) - except KeyboardInterrupt: - print("\nKeyboardInterrupt") - - definitions.set_line_no(0) + with open(file, "r") as ifile: + feeder = MathicsFileLineFeeder(ifile) + try: + while not feeder.empty(): + evaluation = Evaluation( + shell.definitions, + output=TerminalOutput(shell), + catch_interrupt=False, + format="text", + ) + query = evaluation.parse_feeder(feeder) + if query is None: + continue + evaluation.evaluate(query, timeout=settings.TIMEOUT) + except KeyboardInterrupt: + print("\nKeyboardInterrupt") + except Exception as e: + print(f"\nError reading {file}: {e}; skipping reading.") + file = None + else: + definitions.set_line_no(0) if code: for expr in code: diff --git a/mathicsscript/format.py b/mathicsscript/format.py index 5d6af4b..42a0b31 100644 --- a/mathicsscript/format.py +++ b/mathicsscript/format.py @@ -168,15 +168,18 @@ def eval_boxes(result, fn: Callable, obj, **options): obj.last_eval = SymbolAborted return "$Aborted" if format == "text": - if expr_head is SymbolStringForm: - return expr.elements[0].value - elif isinstance(expr, String): + if isinstance(expr, String): return expr.value result = expr.format(obj, SymbolOutputForm) elif format == "xml": result = Expression(SymbolStandardForm, expr).format(obj, SymbolMathMLForm) elif format == "tex": result = Expression(SymbolStandardForm, expr).format(obj, SymbolTeXForm) + elif format == "error": + # We may do fancier things in the future. + if isinstance(expr, String): + return expr.value + result = expr.format(obj, SymbolOutputForm) elif format == "unformatted": if expr_head is PyMathicsGraph and hasattr(expr, "G"): return format_graph(expr.G) diff --git a/mathicsscript/termshell.py b/mathicsscript/termshell.py index 131b0f2..f9b2390 100644 --- a/mathicsscript/termshell.py +++ b/mathicsscript/termshell.py @@ -10,7 +10,7 @@ import mathics_scanner.location from columnize import columnize -from mathics.core.atoms import Symbol +from mathics.core.atoms import String, Symbol from mathics.core.attributes import attribute_string_to_number from mathics.core.expression import Expression, from_python # strip_context, from mathics.core.rules import Rule @@ -238,7 +238,8 @@ def print_result( out_str = str(result.result) use_highlight = True if eval_type == "System`String": - if strict_wl_output: # exact-wl-compatibility + # Use exact-wl-compatibility? + if strict_wl_output: out_str = ( format( [(MToken.STRING, out_str.rstrip())], self.terminal_formatter @@ -263,6 +264,7 @@ def print_result( if eval_type == "System`Graph": out_str = "-Graph-" + elif self.terminal_formatter: # pygmentize show_pygments_tokens = get_settings_value( self.definitions, "Settings`$PygmentsShowTokens" diff --git a/mathicsscript/termshell_prompt.py b/mathicsscript/termshell_prompt.py index 8bc3748..372fb18 100644 --- a/mathicsscript/termshell_prompt.py +++ b/mathicsscript/termshell_prompt.py @@ -164,7 +164,8 @@ def print_result( out_str = str(result.result) use_highlight = True if eval_type == "System`String": - if strict_wl_output: # exact-wl-compatibility + # Use exact-wl-compatibility? + if strict_wl_output: out_str = ( format( [(MToken.STRING, out_str.rstrip())], self.terminal_formatter @@ -189,8 +190,8 @@ def print_result( if eval_type == "System`Graph": out_str = "-Graph-" - elif self.terminal_formatter: # pygmentize + elif self.terminal_formatter: # pygmentize if show_pygments_tokens: print(list(lex(out_str, mma_lexer))) if use_highlight: