summaryrefslogtreecommitdiffstats
path: root/examples/full-screen/calculator.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:35:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:35:20 +0000
commite106bf94eff07d9a59771d9ccc4406421e18ab64 (patch)
treeedb6545500e39df9c67aa918a6125bffc8ec1aee /examples/full-screen/calculator.py
parentInitial commit. (diff)
downloadprompt-toolkit-upstream/3.0.36.tar.xz
prompt-toolkit-upstream/3.0.36.zip
Adding upstream version 3.0.36.upstream/3.0.36upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rwxr-xr-xexamples/full-screen/calculator.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/examples/full-screen/calculator.py b/examples/full-screen/calculator.py
new file mode 100755
index 0000000..fda6567
--- /dev/null
+++ b/examples/full-screen/calculator.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+"""
+A simple example of a calculator program.
+This could be used as inspiration for a REPL.
+"""
+from prompt_toolkit.application import Application
+from prompt_toolkit.document import Document
+from prompt_toolkit.filters import has_focus
+from prompt_toolkit.key_binding import KeyBindings
+from prompt_toolkit.layout.containers import HSplit, Window
+from prompt_toolkit.layout.layout import Layout
+from prompt_toolkit.styles import Style
+from prompt_toolkit.widgets import SearchToolbar, TextArea
+
+help_text = """
+Type any expression (e.g. "4 + 4") followed by enter to execute.
+Press Control-C to exit.
+"""
+
+
+def main():
+ # The layout.
+ search_field = SearchToolbar() # For reverse search.
+
+ output_field = TextArea(style="class:output-field", text=help_text)
+ input_field = TextArea(
+ height=1,
+ prompt=">>> ",
+ style="class:input-field",
+ multiline=False,
+ wrap_lines=False,
+ search_field=search_field,
+ )
+
+ container = HSplit(
+ [
+ output_field,
+ Window(height=1, char="-", style="class:line"),
+ input_field,
+ search_field,
+ ]
+ )
+
+ # Attach accept handler to the input field. We do this by assigning the
+ # handler to the `TextArea` that we created earlier. it is also possible to
+ # pass it to the constructor of `TextArea`.
+ # NOTE: It's better to assign an `accept_handler`, rather then adding a
+ # custom ENTER key binding. This will automatically reset the input
+ # field and add the strings to the history.
+ def accept(buff):
+ # Evaluate "calculator" expression.
+ try:
+ output = "\n\nIn: {}\nOut: {}".format(
+ input_field.text, eval(input_field.text)
+ ) # Don't do 'eval' in real code!
+ except BaseException as e:
+ output = f"\n\n{e}"
+ new_text = output_field.text + output
+
+ # Add text to output buffer.
+ output_field.buffer.document = Document(
+ text=new_text, cursor_position=len(new_text)
+ )
+
+ input_field.accept_handler = accept
+
+ # The key bindings.
+ kb = KeyBindings()
+
+ @kb.add("c-c")
+ @kb.add("c-q")
+ def _(event):
+ "Pressing Ctrl-Q or Ctrl-C will exit the user interface."
+ event.app.exit()
+
+ # Style.
+ style = Style(
+ [
+ ("output-field", "bg:#000044 #ffffff"),
+ ("input-field", "bg:#000000 #ffffff"),
+ ("line", "#004400"),
+ ]
+ )
+
+ # Run application.
+ application = Application(
+ layout=Layout(container, focused_element=input_field),
+ key_bindings=kb,
+ style=style,
+ mouse_support=True,
+ full_screen=True,
+ )
+
+ application.run()
+
+
+if __name__ == "__main__":
+ main()