Files
ProgressQuest-cli/pqcli/ui/curses/widgets/progress_bar.py
T
2019-01-04 00:56:55 +01:00

20 lines
648 B
Python

import curses
from .base import WindowWrapper
class ProgressBar(WindowWrapper):
def set_position(self, position: float, max_: float) -> None:
if not self._win:
return
self._win.erase()
text = f"{position / max_:.02%}"
x = max(0, (self.getmaxyx()[1] - len(text)) // 2)
self._win.addnstr(0, x, text, min(len(text), self.getmaxyx()[1] - 1))
x = int(position * self.getmaxyx()[1] // max_)
if x > 0:
self._win.chgat(0, 0, curses.A_REVERSE)
if x < self.getmaxyx()[1]:
self._win.chgat(0, x, curses.A_NORMAL)
self._win.noutrefresh()