From 4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 18:35:31 +0200 Subject: Adding upstream version 3.0.43. Signed-off-by: Daniel Baumann --- examples/progress-bar/custom-key-bindings.py | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 examples/progress-bar/custom-key-bindings.py (limited to 'examples/progress-bar/custom-key-bindings.py') diff --git a/examples/progress-bar/custom-key-bindings.py b/examples/progress-bar/custom-key-bindings.py new file mode 100755 index 0000000..f700811 --- /dev/null +++ b/examples/progress-bar/custom-key-bindings.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +""" +A very simple progress bar which keep track of the progress as we consume an +iterator. +""" +import os +import signal +import time + +from prompt_toolkit import HTML +from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.patch_stdout import patch_stdout +from prompt_toolkit.shortcuts import ProgressBar + + +def main(): + bottom_toolbar = HTML( + ' [f] Print "f" [q] Abort [x] Send Control-C.' + ) + + # Create custom key bindings first. + kb = KeyBindings() + cancel = [False] + + @kb.add("f") + def _(event): + print("You pressed `f`.") + + @kb.add("q") + def _(event): + "Quit by setting cancel flag." + cancel[0] = True + + @kb.add("x") + def _(event): + "Quit by sending SIGINT to the main thread." + os.kill(os.getpid(), signal.SIGINT) + + # Use `patch_stdout`, to make sure that prints go above the + # application. + with patch_stdout(): + with ProgressBar(key_bindings=kb, bottom_toolbar=bottom_toolbar) as pb: + for i in pb(range(800)): + time.sleep(0.01) + + if cancel[0]: + break + + +if __name__ == "__main__": + main() -- cgit v1.2.3