Coverage for src/qollib/ui/progress.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-02 08:34 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4module provides functions to print progress to the shell (and only the shell) 

5""" 

6 

7RESET = '\033[1;0m' 

8GREEN = '\033[1;32m' 

9RED = '\033[1;31m' 

10 

11 

12def dot(success: bool = True, flush: bool = False, char: str = '*') -> None: 

13 """Print a progress dot to std. out""" 

14 if flush: 

15 print('') 

16 else: 

17 print(f'{GREEN if success else RED}{char}{RESET}', end='', flush=True) 

18 

19 

20def msg(txt: str = '', flush: bool = False) -> None: 

21 if flush: 

22 print('') 

23 else: 

24 print(txt, end='\r', flush=True) 

25 

26 

27def bar(current: float, total: float, flush: bool = False, length: int = 50) -> None: 

28 """print a progress bar and a percent of completion to std. out """ 

29 percent = (current * 100) / total 

30 done = int(round(length * (percent/100))) 

31 prog = ''.join(['━' for _ in range(done)]) 

32 empty = ''.join([' ' for _ in range(length - done)]) 

33 stop = '┃' if len(empty) > 0 else '┫' 

34 print(f'{prog}{empty}{stop} \t[{percent:.2f} %]', end='\r', flush=True) 

35 if flush: 

36 print('')