File: progress.py

package info (click to toggle)
kitty 0.45.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,476 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (39 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>


from .operations import repeat, styled


def render_progress_bar(frac: float, width: int = 80) -> str:
    if frac >= 1:
        return styled('🬋' * width, fg='green')
    if frac <= 0:
        return styled('🬋' * width, dim=True)
    w = frac * width
    fl = int(w)
    overhang = w - fl
    filled = repeat('🬋', fl)
    if overhang < 0.2:
        needs_break = True
    elif overhang < 0.8:
        filled += '🬃'
        fl += 1
        needs_break = False
    else:
        if fl < width - 1:
            filled += '🬋'
            fl += 1
            needs_break = True
        else:
            filled += '🬃'
            fl += 1
            needs_break = False
    ans = styled(filled, fg='blue')
    unfilled = '🬇' if width > fl and needs_break else ''
    filler = width - fl - len(unfilled)
    if filler > 0:
        unfilled += repeat('🬋', filler)
    if unfilled:
        ans += styled(unfilled, dim=True)
    return ans