mirror of
https://github.com/rr-/pq-cli.git
synced 2026-09-28 22:33:59 -06:00
ui: show time left in some progress bars
This commit is contained in:
@@ -4,12 +4,24 @@ import typing as T
|
||||
import urwid
|
||||
|
||||
|
||||
def format_timespan(timespan: datetime.timedelta) -> str:
|
||||
num = timespan.total_seconds()
|
||||
if num < 60.0:
|
||||
return f"~{int(num)}s"
|
||||
num /= 60
|
||||
if num < 60.0:
|
||||
return f"~{int(num)}m"
|
||||
num /= 60
|
||||
return f"~{num:.01f}h"
|
||||
|
||||
|
||||
class CustomProgressBar(urwid.Widget):
|
||||
_sizing = frozenset(["flow"])
|
||||
text_align = "center"
|
||||
eighths = " ▏▎▍▌▋▊▉"
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, show_time: bool) -> None:
|
||||
self.show_time = show_time
|
||||
self.position = 0.0
|
||||
self.max_ = 1.0
|
||||
self.normal = "progressbar-normal"
|
||||
@@ -48,7 +60,10 @@ class CustomProgressBar(urwid.Widget):
|
||||
|
||||
def get_text(self) -> str:
|
||||
percent = min(100.0, max(0.0, self.position * 100.0 / self.max_))
|
||||
return f"{percent:.02f} %"
|
||||
ret = f"{percent:.02f} %"
|
||||
if self.time_left and self.show_time:
|
||||
ret += f" ({format_timespan(self.time_left)})"
|
||||
return ret
|
||||
|
||||
# workaround for https://github.com/urwid/urwid/issues/317
|
||||
def render(self, size: T.Tuple[int, int], focus: bool = False) -> T.Any:
|
||||
|
||||
@@ -29,7 +29,7 @@ class CharacterSheetView(DoubleLineBox):
|
||||
|
||||
self.level_text = urwid.Text("")
|
||||
self.stat_texts = {stat: urwid.Text("") for stat in list(StatType)}
|
||||
self.exp_bar = CustomProgressBar()
|
||||
self.exp_bar = CustomProgressBar(show_time=True)
|
||||
|
||||
data_table = DataTable(columns=[(urwid.WEIGHT, 1), (urwid.WEIGHT, 2)])
|
||||
data_table.add_row(urwid.Text("Name"), urwid.Text(player.name))
|
||||
@@ -142,7 +142,7 @@ class InventoryView(DoubleLineBox):
|
||||
self.data_table = DataTable(columns=[(urwid.WEIGHT, 3), (urwid.PACK,)])
|
||||
self.data_table.add_row(urwid.Text("Gold"), self.gold_text)
|
||||
self.scrollable = Scrollable(self.data_table)
|
||||
self.encumbrance_bar = CustomProgressBar()
|
||||
self.encumbrance_bar = CustomProgressBar(show_time=False)
|
||||
|
||||
self.player.connect("new_task", self.on_new_task)
|
||||
self.player.inventory.connect("gold_change", self.sync_gold)
|
||||
@@ -218,7 +218,7 @@ class PlotView(DoubleLineBox):
|
||||
|
||||
self.data_table = DataTable(columns=[(urwid.WEIGHT, 1)])
|
||||
self.scrollable = Scrollable(self.data_table)
|
||||
self.plot_bar = CustomProgressBar()
|
||||
self.plot_bar = CustomProgressBar(show_time=True)
|
||||
|
||||
self.player.quest_book.connect("start_act", self.sync_act_add)
|
||||
self.player.quest_book.plot_bar.connect("change", self.sync_position)
|
||||
@@ -265,7 +265,7 @@ class QuestBookView(DoubleLineBox):
|
||||
self.player = player
|
||||
|
||||
self.data_table = DataTable(columns=[(urwid.WEIGHT, 1)])
|
||||
self.quest_bar = CustomProgressBar()
|
||||
self.quest_bar = CustomProgressBar(show_time=True)
|
||||
self.scrollable = Scrollable(self.data_table)
|
||||
|
||||
self.player.quest_book.connect("start_quest", self.sync_quest_add)
|
||||
@@ -307,7 +307,7 @@ class TaskView(NPile):
|
||||
self.player = player
|
||||
|
||||
self.current_task_text = urwid.Text("...")
|
||||
self.current_task_bar = CustomProgressBar()
|
||||
self.current_task_bar = CustomProgressBar(show_time=False)
|
||||
|
||||
self.player.connect("new_task", self.sync_task_name)
|
||||
self.player.task_bar.connect("change", self.sync_position)
|
||||
|
||||
Reference in New Issue
Block a user