Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 3 | import sys |
| 4 | |
| 5 | |
Alex | e4de114 | 2022-11-04 19:26:03 -0500 | [diff] [blame] | 6 | class cl_typewriter(object): |
| 7 | previous = 0 |
| 8 | carret = 0 |
| 9 | |
| 10 | def cl_start(self, sttr): |
| 11 | self.previous = self.carret |
| 12 | self.carret += len(sttr) |
| 13 | sys.stdout.write("\r{}".format(sttr)) |
| 14 | |
| 15 | def cl_inline(self, sttr): |
| 16 | self.carret += len(sttr) |
| 17 | sys.stdout.write("{}".format(sttr)) |
| 18 | |
| 19 | def cl_sameline(self, sttr): |
| 20 | self.cl_inline("\r" + sttr) |
| 21 | self.cl_flush() |
| 22 | |
| 23 | def cl_flush(self, newline=False): |
| 24 | if newline: |
| 25 | self.cl_inline("\n") |
| 26 | self.carret = 0 |
| 27 | elif self.previous > self.carret: |
| 28 | self.cl_inline(" "*(self.previous - self.carret)) |
| 29 | sys.stdout.flush() |
| 30 | |
| 31 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 32 | class Progress(object): |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 33 | _strsize = 0 |
| 34 | _note_size = 0 |
| 35 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 36 | def __init__(self, max_index, bar_size=21): |
| 37 | self.total = max_index |
| 38 | # bar size in symbols |
| 39 | self.bar_size = bar_size |
| 40 | |
| 41 | def write_progress(self, index, note=''): |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 42 | # calc index and percent values |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 43 | _suffix = '' |
| 44 | new_size = len(note) |
| 45 | if self._note_size > new_size: |
| 46 | _suffix = ' '*(self._note_size - new_size) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 47 | if index: |
| 48 | _percent = (100 * index) // self.total |
| 49 | _index = (self.bar_size * index) // self.total |
| 50 | else: |
| 51 | _percent = 0 |
| 52 | _index = 0 |
| 53 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 54 | # clear the line |
| 55 | sys.stdout.write('\r') |
| 56 | # print new progress |
| 57 | _format = "[{:"+str(self.bar_size-1)+"}] {}/{} ({}%) {}" |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 58 | _progress_string = _format.format( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 59 | '='*_index, |
| 60 | index, |
| 61 | self.total, |
| 62 | _percent, |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 63 | note + _suffix |
| 64 | ) |
| 65 | sys.stdout.write(_progress_string) |
| 66 | # Save new note size and whole string size |
| 67 | self._strsize = len(_progress_string) |
| 68 | self._note_size = new_size |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 69 | sys.stdout.flush() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 70 | |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 71 | def clearline(self): |
| 72 | sys.stdout.write('\r') |
| 73 | sys.stdout.write(' '*self._strsize) |
| 74 | sys.stdout.write('\r') |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 75 | sys.stdout.flush() |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 76 | |
| 77 | def end(self): |
| 78 | self._note_size = 0 |
| 79 | self._strsize = 0 |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 80 | sys.stdout.write('\n') |