From e106bf94eff07d9a59771d9ccc4406421e18ab64 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 19:35:20 +0200 Subject: Adding upstream version 3.0.36. Signed-off-by: Daniel Baumann --- examples/dialogs/button_dialog.py | 19 ++++++++++++++ examples/dialogs/checkbox_dialog.py | 36 +++++++++++++++++++++++++++ examples/dialogs/input_dialog.py | 17 +++++++++++++ examples/dialogs/messagebox.py | 16 ++++++++++++ examples/dialogs/password_dialog.py | 19 ++++++++++++++ examples/dialogs/progress_dialog.py | 47 +++++++++++++++++++++++++++++++++++ examples/dialogs/radio_dialog.py | 39 +++++++++++++++++++++++++++++ examples/dialogs/styled_messagebox.py | 37 +++++++++++++++++++++++++++ examples/dialogs/yes_no_dialog.py | 17 +++++++++++++ 9 files changed, 247 insertions(+) create mode 100755 examples/dialogs/button_dialog.py create mode 100755 examples/dialogs/checkbox_dialog.py create mode 100755 examples/dialogs/input_dialog.py create mode 100755 examples/dialogs/messagebox.py create mode 100755 examples/dialogs/password_dialog.py create mode 100755 examples/dialogs/progress_dialog.py create mode 100755 examples/dialogs/radio_dialog.py create mode 100755 examples/dialogs/styled_messagebox.py create mode 100755 examples/dialogs/yes_no_dialog.py (limited to 'examples/dialogs') diff --git a/examples/dialogs/button_dialog.py b/examples/dialogs/button_dialog.py new file mode 100755 index 0000000..7a99b9a --- /dev/null +++ b/examples/dialogs/button_dialog.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +""" +Example of button dialog window. +""" +from prompt_toolkit.shortcuts import button_dialog + + +def main(): + result = button_dialog( + title="Button dialog example", + text="Are you sure?", + buttons=[("Yes", True), ("No", False), ("Maybe...", None)], + ).run() + + print(f"Result = {result}") + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/checkbox_dialog.py b/examples/dialogs/checkbox_dialog.py new file mode 100755 index 0000000..90be263 --- /dev/null +++ b/examples/dialogs/checkbox_dialog.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +""" +Example of a checkbox-list-based dialog. +""" +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.shortcuts import checkboxlist_dialog, message_dialog +from prompt_toolkit.styles import Style + +results = checkboxlist_dialog( + title="CheckboxList dialog", + text="What would you like in your breakfast ?", + values=[ + ("eggs", "Eggs"), + ("bacon", HTML("Bacon")), + ("croissants", "20 Croissants"), + ("daily", "The breakfast of the day"), + ], + style=Style.from_dict( + { + "dialog": "bg:#cdbbb3", + "button": "bg:#bf99a4", + "checkbox": "#e8612c", + "dialog.body": "bg:#a9cfd0", + "dialog shadow": "bg:#c98982", + "frame.label": "#fcaca3", + "dialog.body label": "#fd8bb6", + } + ), +).run() +if results: + message_dialog( + title="Room service", + text="You selected: %s\nGreat choice sir !" % ",".join(results), + ).run() +else: + message_dialog("*starves*").run() diff --git a/examples/dialogs/input_dialog.py b/examples/dialogs/input_dialog.py new file mode 100755 index 0000000..6235265 --- /dev/null +++ b/examples/dialogs/input_dialog.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +""" +Example of an input box dialog. +""" +from prompt_toolkit.shortcuts import input_dialog + + +def main(): + result = input_dialog( + title="Input dialog example", text="Please type your name:" + ).run() + + print(f"Result = {result}") + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/messagebox.py b/examples/dialogs/messagebox.py new file mode 100755 index 0000000..4642b84 --- /dev/null +++ b/examples/dialogs/messagebox.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +""" +Example of a message box window. +""" +from prompt_toolkit.shortcuts import message_dialog + + +def main(): + message_dialog( + title="Example dialog window", + text="Do you want to continue?\nPress ENTER to quit.", + ).run() + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/password_dialog.py b/examples/dialogs/password_dialog.py new file mode 100755 index 0000000..39d7b9c --- /dev/null +++ b/examples/dialogs/password_dialog.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +""" +Example of an password input dialog. +""" +from prompt_toolkit.shortcuts import input_dialog + + +def main(): + result = input_dialog( + title="Password dialog example", + text="Please type your password:", + password=True, + ).run() + + print(f"Result = {result}") + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/progress_dialog.py b/examples/dialogs/progress_dialog.py new file mode 100755 index 0000000..1fd3ffb --- /dev/null +++ b/examples/dialogs/progress_dialog.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +""" +Example of a progress bar dialog. +""" +import os +import time + +from prompt_toolkit.shortcuts import progress_dialog + + +def worker(set_percentage, log_text): + """ + This worker function is called by `progress_dialog`. It will run in a + background thread. + + The `set_percentage` function can be used to update the progress bar, while + the `log_text` function can be used to log text in the logging window. + """ + percentage = 0 + for dirpath, dirnames, filenames in os.walk("../.."): + for f in filenames: + log_text(f"{dirpath} / {f}\n") + set_percentage(percentage + 1) + percentage += 2 + time.sleep(0.1) + + if percentage == 100: + break + if percentage == 100: + break + + # Show 100% for a second, before quitting. + set_percentage(100) + time.sleep(1) + + +def main(): + progress_dialog( + title="Progress dialog example", + text="As an examples, we walk through the filesystem and print " + "all directories", + run_callback=worker, + ).run() + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/radio_dialog.py b/examples/dialogs/radio_dialog.py new file mode 100755 index 0000000..94d80e2 --- /dev/null +++ b/examples/dialogs/radio_dialog.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +""" +Example of a radio list box dialog. +""" +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.shortcuts import radiolist_dialog + + +def main(): + result = radiolist_dialog( + values=[ + ("red", "Red"), + ("green", "Green"), + ("blue", "Blue"), + ("orange", "Orange"), + ], + title="Radiolist dialog example", + text="Please select a color:", + ).run() + + print(f"Result = {result}") + + # With HTML. + result = radiolist_dialog( + values=[ + ("red", HTML('')), + ("green", HTML('')), + ("blue", HTML('')), + ("orange", HTML('')), + ], + title=HTML("Radiolist dialog example with colors"), + text="Please select a color:", + ).run() + + print(f"Result = {result}") + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/styled_messagebox.py b/examples/dialogs/styled_messagebox.py new file mode 100755 index 0000000..3f6fc53 --- /dev/null +++ b/examples/dialogs/styled_messagebox.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +""" +Example of a style dialog window. +All dialog shortcuts take a `style` argument in order to apply a custom +styling. + +This also demonstrates that the `title` argument can be any kind of formatted +text. +""" +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.shortcuts import message_dialog +from prompt_toolkit.styles import Style + +# Custom color scheme. +example_style = Style.from_dict( + { + "dialog": "bg:#88ff88", + "dialog frame-label": "bg:#ffffff #000000", + "dialog.body": "bg:#000000 #00ff00", + "dialog shadow": "bg:#00aa00", + } +) + + +def main(): + message_dialog( + title=HTML( + ' ' + ' window' + ), + text="Do you want to continue?\nPress ENTER to quit.", + style=example_style, + ).run() + + +if __name__ == "__main__": + main() diff --git a/examples/dialogs/yes_no_dialog.py b/examples/dialogs/yes_no_dialog.py new file mode 100755 index 0000000..4b08dd6 --- /dev/null +++ b/examples/dialogs/yes_no_dialog.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +""" +Example of confirmation (yes/no) dialog window. +""" +from prompt_toolkit.shortcuts import yes_no_dialog + + +def main(): + result = yes_no_dialog( + title="Yes/No dialog example", text="Do you want to confirm?" + ).run() + + print(f"Result = {result}") + + +if __name__ == "__main__": + main() -- cgit v1.2.3