summaryrefslogtreecommitdiffstats
path: root/examples/dialogs/progress_dialog.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
commit4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1 (patch)
treee5dee7be2f0d963da4faad6517278d03783e3adc /examples/dialogs/progress_dialog.py
parentInitial commit. (diff)
downloadprompt-toolkit-upstream.tar.xz
prompt-toolkit-upstream.zip
Adding upstream version 3.0.43.upstream/3.0.43upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/dialogs/progress_dialog.py')
-rwxr-xr-xexamples/dialogs/progress_dialog.py47
1 files changed, 47 insertions, 0 deletions
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()