summaryrefslogtreecommitdiffstats
path: root/examples/dialogs/progress_dialog.py
blob: 1fd3ffbadbe4d8a39c8764776c1f18a77f430e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()