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