Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Mathics3/mathicsscript
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
rocky committed Apr 9, 2021
2 parents 52229ad + a7cbce5 commit 06aef70
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
54 changes: 54 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features
* Syntax highlighting using `pygments <https://pygments.org>`_.
* Automatic detection of light or dark `terminal background color <https://pypi.org/project/term-background/>`_.
* Entering and displaying Unicode symbols such as used for Pi or Rule arrows
* Provision for running in non-interactive batch mode which an be used inside POSIX shells

Installing
----------
Expand All @@ -36,6 +37,59 @@ To install from git sources so that you run from the git source tree:
$ make develop


Running
-------

Once install run using ``mathicsscript``:

::

$ mathicsscript
Mathicscript: 2.1.2, Mathics 2.1.1.dev0
on CPython 3.7.10 (default, Feb 27 2021, 08:15:51)
using SymPy 1.7.1, mpmath 1.2.1, numpy 1.20.1

Copyright (C) 2011-2021 The Mathics Team.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
See the documentation for the full license.

Quit by evaluating Quit[] or by pressing CONTROL-D.

In[1]:=


For batch use:
::

$ mathicsscript -c "N[Pi, 30]"
3.14159265358979323846264338328

To read from a file

In file ``/tmp/test.m``:

::

sum=2+2
integral=Integrate[1,x]
Print["Results: ",{sum,integral}]

Feeding this into ``mathicsscript``:

::

$ mathicsscript --no-prompt </tmp/test.m
4
x
Results: {4, x}
None


For a full list of options, type ``mathicsscript --help``.


Why not IPython via Jupyter?
----------------------------

Expand Down
28 changes: 18 additions & 10 deletions mathicsscript/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def out(self, out):
show_default=True,
help="Accept Unicode operators in input and show unicode in output.",
)
@click.option(
"--prompt/--no-prompt",
default=True,
show_default=True,
help="Do not prompt In[] or Out[].",
)
@click.option(
"--pyextensions",
"-l",
Expand All @@ -166,10 +172,10 @@ def out(self, out):
required=False,
)
@click.option(
"--initfile",
"--run",
type=click.Path(readable=True),
help=(
"go to interactive shell after evaluating INITFILE but leave "
"go to interactive shell after evaluating PATH but leave "
"history empty and set $Line to 1"
),
)
Expand All @@ -195,9 +201,10 @@ def main(
readline,
completion,
unicode,
prompt,
pyextensions,
execute,
initfile,
run,
style,
pygments_tokens,
file,
Expand Down Expand Up @@ -227,10 +234,10 @@ def main(
"Settings`$PygmentsShowTokens", from_python(True if pygments_tokens else False)
)

shell = TerminalShell(definitions, style, readline, completion, unicode)
shell = TerminalShell(definitions, style, readline, completion, unicode, prompt)
load_settings(shell)
if initfile:
with open(initfile, "r") as ifile:
if run:
with open(run, "r") as ifile:
feeder = MathicsFileLineFeeder(ifile)
try:
while not feeder.empty():
Expand All @@ -256,7 +263,7 @@ def main(
)
shell.terminal_formatter = None
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
shell.print_result(result, "text")
shell.print_result(result, prompt, "text")

# After the next release, we can remove the hasattr test.
if hasattr(evaluation, "exc_result"):
Expand Down Expand Up @@ -293,7 +300,7 @@ def main(
if not persist:
return exit_rc

if not quiet:
if not quiet and prompt:
print(f"\nMathicscript: {__version__}, {version_string}\n")
print(license_string + "\n")
print(f"Quit by evaluating Quit[] or by pressing {quit_command}.\n")
Expand Down Expand Up @@ -359,7 +366,7 @@ def main(
query, timeout=settings.TIMEOUT, format="unformatted"
)
if result is not None:
shell.print_result(result, output_style)
shell.print_result(result, prompt, output_style)

except ShellEscapeException as e:
source_code = e.line
Expand All @@ -379,7 +386,8 @@ def main(
except (KeyboardInterrupt):
print("\nKeyboardInterrupt")
except EOFError:
print("\n\nGoodbye!\n")
if prompt:
print("\n\nGoodbye!\n")
break
except SystemExit:
print("\n\nGoodbye!\n")
Expand Down
21 changes: 12 additions & 9 deletions mathicsscript/termshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@
from mathics.core.parser import MathicsLineFeeder


def is_pygments_style(style):
def is_pygments_style(style: str):
if style not in ALL_PYGMENTS_STYLES:
print("Pygments style name '%s' not found." % style)
print("Style names are:\n%s" % columnize(ALL_PYGMENTS_STYLES))
print(f"Pygments style name '{style}' not found.")
print(f"Style names are:\n{columnize(ALL_PYGMENTS_STYLES)}")
return False
return True

Expand All @@ -106,11 +106,13 @@ def __init__(
want_readline: bool,
want_completion: bool,
use_unicode: bool,
prompt: bool,
):
super(TerminalShell, self).__init__("<stdin>")
super().__init__("<stdin>")
self.input_encoding = locale.getpreferredencoding()
self.lineno = 0
self.terminal_formatter = None
self.prompt = prompt

# Try importing readline to enable arrow keys support etc.
self.using_readline = False
Expand Down Expand Up @@ -212,7 +214,7 @@ def __init__(
)
self.definitions.set_attribute("Settings`$UseUnicode", "System`Locked")

def change_pygments_style(self, style):
def change_pygments_style(self, style:str):
if style == self.pygments_style:
return False
if is_pygments_style(style):
Expand Down Expand Up @@ -259,7 +261,7 @@ def read_line(self, prompt):
raise ShellEscapeException(line)
return replace_unicode_with_wl(line)

def print_result(self, result, output_style=""):
def print_result(self, result, prompt: bool, output_style=""):
if result is None:
# FIXME decide what to do here
return
Expand Down Expand Up @@ -293,7 +295,7 @@ def print_result(self, result, output_style=""):
print(list(lex(out_str, mma_lexer)))
out_str = highlight(out_str, mma_lexer, self.terminal_formatter)
output = self.to_output(out_str)
if output_style == "text":
if output_style == "text" or not prompt:
print(output)
else:
print(self.get_out_prompt("") + output + "\n")
Expand Down Expand Up @@ -346,7 +348,7 @@ def _complete_symbol_name(self, text, state):
except IndexError:
return None

def get_completion_candidates(self, text):
def get_completion_candidates(self, text: str):

brace_pos = text.rfind("[")
if brace_pos >= 0:
Expand All @@ -367,7 +369,8 @@ def reset_lineno(self):
self.lineno = 0

def feed(self):
result = self.read_line(self.get_in_prompt()) + "\n"
prompt_str = self.get_in_prompt() if self.prompt else ""
result = self.read_line(prompt_str) + "\n"
if result == "\n":
return "" # end of input
self.lineno += 1
Expand Down
1 change: 1 addition & 0 deletions test/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_completion():
want_readline=True,
want_completion=True,
use_unicode=False,
prompt=True,
)

for prefix, completions in (
Expand Down

0 comments on commit 06aef70

Please sign in to comment.