mirror of
https://github.com/XuehaiPan/nvitop.git
synced 2026-05-15 06:06:12 -06:00
chore(tui/utils): rename functions for better name
This commit is contained in:
parent
51ee688d2b
commit
466f4f57b4
5 changed files with 54 additions and 41 deletions
|
|
@ -38,7 +38,7 @@ from nvitop.tui.library.utils import (
|
|||
bytes2human,
|
||||
colored,
|
||||
cut_string,
|
||||
make_bar,
|
||||
make_bar_chart,
|
||||
set_color,
|
||||
timedelta2human,
|
||||
ttl_cache,
|
||||
|
|
@ -83,7 +83,7 @@ __all__ = [
|
|||
'cut_string',
|
||||
'host',
|
||||
'libcurses',
|
||||
'make_bar',
|
||||
'make_bar_chart',
|
||||
'normalize_keybinding',
|
||||
'set_color',
|
||||
'setlocale_utf8',
|
||||
|
|
|
|||
|
|
@ -255,20 +255,20 @@ class HistoryGraph: # pylint: disable=too-many-instance-attributes
|
|||
return
|
||||
|
||||
self.graph, self.last_graph = self.last_graph, self.graph
|
||||
bar = self.make_bar(self.reversed_history[1], value) # pylint: disable=disallowed-name
|
||||
for i, (line, char) in enumerate(zip(self.graph, bar)):
|
||||
bar_chart = self.make_bar_chart(self.reversed_history[1], value)
|
||||
for i, (line, char) in enumerate(zip(self.graph, bar_chart)):
|
||||
self.graph[i] = (line + char)[-self.width :]
|
||||
|
||||
def remake_graph(self) -> None:
|
||||
with self.remake_lock:
|
||||
if self.max_value >= self.baseline:
|
||||
reversed_bars = []
|
||||
reversed_bar_charts = []
|
||||
for _, (value2, value1) in zip(
|
||||
range(self.width),
|
||||
grouped(self.reversed_history, size=2, fillvalue=self.baseline),
|
||||
):
|
||||
reversed_bars.append(self.make_bar(value1, value2))
|
||||
graph = list(map(''.join, zip(*reversed(reversed_bars))))
|
||||
reversed_bar_charts.append(self.make_bar_chart(value1, value2))
|
||||
graph = list(map(''.join, zip(*reversed(reversed_bar_charts))))
|
||||
|
||||
for i, line in enumerate(graph):
|
||||
graph[i] = line.rjust(self.width)[-self.width :]
|
||||
|
|
@ -279,7 +279,7 @@ class HistoryGraph: # pylint: disable=too-many-instance-attributes
|
|||
self.graph = [' ' * self.width for _ in range(self.height)]
|
||||
self.last_graph = [' ' * (self.width - 1) for _ in range(self.height)]
|
||||
|
||||
def make_bar(self, value1: float, value2: float) -> list[str]:
|
||||
def make_bar_chart(self, value1: float, value2: float) -> list[str]:
|
||||
if self.bound <= self.baseline:
|
||||
return [' '] * self.height
|
||||
|
||||
|
|
@ -289,15 +289,14 @@ class HistoryGraph: # pylint: disable=too-many-instance-attributes
|
|||
value1 = max(value1, 0.2)
|
||||
if value2 >= 0.0:
|
||||
value2 = max(value2, 0.2)
|
||||
# pylint: disable=disallowed-name,invalid-name
|
||||
bar = []
|
||||
bar_charts = []
|
||||
for h in range(self.height):
|
||||
s1 = min(max(round(5 * (value1 - h)), 0), 4)
|
||||
s2 = min(max(round(5 * (value2 - h)), 0), 4)
|
||||
bar.append(self.value2symbol[s1, s2])
|
||||
bar_charts.append(self.value2symbol[s1, s2])
|
||||
if not self.upsidedown:
|
||||
bar.reverse()
|
||||
return bar
|
||||
bar_charts.reverse()
|
||||
return bar_charts
|
||||
|
||||
def shift_line(self, line: str) -> str:
|
||||
return ''.join(self.pair2symbol[p] for p in zip(line, line[1:]))
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ __all__ = [
|
|||
'bytes2human',
|
||||
'colored',
|
||||
'cut_string',
|
||||
'make_bar',
|
||||
'make_bar_chart',
|
||||
'set_color',
|
||||
'timedelta2human',
|
||||
'ttl_cache',
|
||||
|
|
@ -97,37 +97,44 @@ def cut_string(
|
|||
return str(padstr + s[-(maxlen - len(padstr)) :])
|
||||
|
||||
|
||||
# pylint: disable=disallowed-name
|
||||
def make_bar(
|
||||
# pylint: disable-next=too-many-arguments
|
||||
def make_bar_chart(
|
||||
prefix: str,
|
||||
percent: float | str,
|
||||
width: int,
|
||||
*,
|
||||
extra_text: str = '',
|
||||
swap_text: bool = False,
|
||||
extra_blank: str = '',
|
||||
) -> str:
|
||||
bar = f'{prefix}: '
|
||||
bar_chart = f'{prefix}: '
|
||||
if percent != NA and not (isinstance(percent, float) and not math.isfinite(percent)):
|
||||
if isinstance(percent, str) and percent.endswith('%'):
|
||||
percent = percent.replace('%', '')
|
||||
percent = float(percent) if '.' in percent else int(percent)
|
||||
percentage = max(0.0, min(float(percent) / 100.0, 1.0))
|
||||
quotient, remainder = divmod(max(1, round(8 * (width - len(bar) - 4) * percentage)), 8)
|
||||
bar += '█' * quotient
|
||||
quotient, remainder = divmod(
|
||||
max(1, round(8 * (width - len(bar_chart) - 4) * percentage)),
|
||||
8,
|
||||
)
|
||||
bar_chart += '█' * quotient
|
||||
if remainder > 0:
|
||||
bar += ' ▏▎▍▌▋▊▉'[remainder]
|
||||
if isinstance(percent, float) and len(f'{bar} {percent:.1f}%') <= width:
|
||||
bar_chart += ' ▏▎▍▌▋▊▉'[remainder]
|
||||
if isinstance(percent, float) and len(f'{bar_chart} {percent:.1f}%') <= width:
|
||||
text = f'{percent:.1f}%'
|
||||
else:
|
||||
text = f'{min(round(percent), 100):d}%'.replace('100%', 'MAX') # type: ignore[arg-type]
|
||||
else:
|
||||
bar += '░' * (width - len(bar) - 4)
|
||||
bar_chart += '░' * (width - len(bar_chart) - 4)
|
||||
text = 'N/A'
|
||||
if extra_text:
|
||||
if len(f'{bar} {text} {extra_text}') <= width:
|
||||
if len(f'{bar_chart} {text} {extra_blank}{extra_text}') <= width:
|
||||
if swap_text:
|
||||
text, extra_text = extra_text, text
|
||||
return f'{bar} {text}'.ljust(width - len(extra_text) - 3) + f' {extra_text}'
|
||||
if len(f'{bar} {extra_text}') <= width and swap_text:
|
||||
return f'{bar} {extra_text}'.ljust(width)
|
||||
return f'{bar} {text}'.ljust(width)
|
||||
return (
|
||||
f'{bar_chart} {text}'.ljust(width - len(extra_blank) - len(extra_text) - 1)
|
||||
+ f' {extra_blank}{extra_text}'
|
||||
)
|
||||
if len(f'{bar_chart} {extra_text}') <= width and swap_text:
|
||||
return f'{bar_chart} {extra_text}'.ljust(width)
|
||||
return f'{bar_chart} {text}'.ljust(width)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ from nvitop.tui.library import (
|
|||
Snapshot,
|
||||
colored,
|
||||
cut_string,
|
||||
make_bar,
|
||||
make_bar_chart,
|
||||
ttl_cache,
|
||||
)
|
||||
from nvitop.tui.screens.main.panels.base import BasePanel
|
||||
|
|
@ -482,17 +482,17 @@ class DevicePanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
|
||||
for y, row in enumerate(matrix, start=y_start):
|
||||
for x_offset, width, prefix, utilization, color, extra_text in row:
|
||||
# pylint: disable-next=disallowed-name
|
||||
bar = make_bar(
|
||||
bar_chart = make_bar_chart(
|
||||
prefix,
|
||||
utilization,
|
||||
width=width,
|
||||
extra_text=extra_text,
|
||||
swap_text=not extra_text.endswith('MHz'),
|
||||
extra_blank=' ',
|
||||
)
|
||||
self.addstr(y, x_offset, f'{bar} │')
|
||||
self.addstr(y, x_offset, f'{bar_chart} │')
|
||||
if self.TERM_256COLOR:
|
||||
parts = bar.rstrip().split(' ')
|
||||
parts = bar_chart.rstrip().split(' ')
|
||||
prefix_len = len(parts[0])
|
||||
bar_len = len(parts[1])
|
||||
full_bar_len = width - prefix_len - 5
|
||||
|
|
@ -661,14 +661,15 @@ class DevicePanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
]
|
||||
for y, row in enumerate(matrix, start=y_start):
|
||||
for prefix, utilization, color, width, extra_text in row:
|
||||
bar = make_bar( # pylint: disable=disallowed-name
|
||||
bar_chart = make_bar_chart(
|
||||
prefix,
|
||||
utilization,
|
||||
width=width,
|
||||
extra_text=extra_text,
|
||||
swap_text=not extra_text.endswith('MHz'),
|
||||
extra_blank=' ',
|
||||
)
|
||||
lines[y] += f' {colored(bar, color)} │' # type: ignore[arg-type]
|
||||
lines[y] += f' {colored(bar_chart, color)} │' # type: ignore[arg-type]
|
||||
|
||||
if index == len(self.snapshots) - 1:
|
||||
lines[y_start + len(matrix)] = (
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ from nvitop.tui.library import (
|
|||
bytes2human,
|
||||
colored,
|
||||
host,
|
||||
make_bar,
|
||||
make_bar_chart,
|
||||
timedelta2human,
|
||||
)
|
||||
from nvitop.tui.screens.main.panels.base import BasePanel
|
||||
|
|
@ -286,7 +286,7 @@ class HostPanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
width_right = len(load_average) + 4
|
||||
width_left = self.width - 2 - width_right
|
||||
cpu_bar = '[ {} ]'.format(
|
||||
make_bar(
|
||||
make_bar_chart(
|
||||
'CPU',
|
||||
self.cpu_percent,
|
||||
width_left - 4,
|
||||
|
|
@ -294,14 +294,20 @@ class HostPanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
),
|
||||
)
|
||||
memory_bar = '[ {} ]'.format(
|
||||
make_bar(
|
||||
make_bar_chart(
|
||||
'MEM',
|
||||
self.virtual_memory.percent,
|
||||
width_left - 4,
|
||||
extra_text=f' USED: {bytes2human(self.virtual_memory.used, min_unit=GiB)}',
|
||||
),
|
||||
)
|
||||
swap_bar = '[ {} ]'.format(make_bar('SWP', self.swap_memory.percent, width_right - 4))
|
||||
swap_bar = '[ {} ]'.format(
|
||||
make_bar_chart(
|
||||
'SWP',
|
||||
self.swap_memory.percent,
|
||||
width_right - 4,
|
||||
),
|
||||
)
|
||||
self.addstr(self.y, self.x, f'{cpu_bar} ( {load_average} )')
|
||||
self.addstr(self.y + 1, self.x, f'{memory_bar} {swap_bar}')
|
||||
self.color_at(self.y, self.x, width=len(cpu_bar), fg='cyan', attr='bold')
|
||||
|
|
@ -425,7 +431,7 @@ class HostPanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
width_right = len(load_average) + 4
|
||||
width_left = self.width - 2 - width_right
|
||||
cpu_bar = '[ {} ]'.format(
|
||||
make_bar(
|
||||
make_bar_chart(
|
||||
'CPU',
|
||||
self.cpu_percent,
|
||||
width_left - 4,
|
||||
|
|
@ -433,14 +439,14 @@ class HostPanel(BasePanel): # pylint: disable=too-many-instance-attributes
|
|||
),
|
||||
)
|
||||
memory_bar = '[ {} ]'.format(
|
||||
make_bar(
|
||||
make_bar_chart(
|
||||
'MEM',
|
||||
self.virtual_memory.percent,
|
||||
width_left - 4,
|
||||
extra_text=f' USED: {bytes2human(self.virtual_memory.used, min_unit=GiB)}',
|
||||
),
|
||||
)
|
||||
swap_bar = '[ {} ]'.format(make_bar('SWP', self.swap_memory.percent, width_right - 4))
|
||||
swap_bar = '[ {} ]'.format(make_bar_chart('SWP', self.swap_memory.percent, width_right - 4))
|
||||
|
||||
lines = [
|
||||
'{} {}'.format(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue