Skip to content

Commit

Permalink
fix chart size
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Feb 4, 2024
1 parent 4cee047 commit 6cd4029
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/sugar/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Functions about console."""
import os


def get_terminal_size():
"""Return the height (number of lines) of the terminal using os module."""
size = os.get_terminal_size()
try:
height = size.lines
except OSError:
# Default to 24 lines if the terminal size cannot be determined.
height = 24

try:
width = size.columns
except OSError:
# Default to 24 lines if the terminal size cannot be determined.
height = 80
return width, height
21 changes: 17 additions & 4 deletions src/sugar/plugins/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from textual.widget import Widget
from textual.widgets import Header

from sugar.console import get_terminal_size
from sugar.inspect import get_container_name, get_container_stats
from sugar.logs import KxgrErrorType, KxgrLogs
from sugar.plugins.base import SugarDockerCompose
Expand Down Expand Up @@ -54,11 +55,9 @@ def __init__(
def create_chart(self):
"""Create a new chart."""
self.fig_mem = plotille.Figure()
self.fig_mem.width = 50
self.fig_mem.height = 5
self.fig_cpu = plotille.Figure()
self.fig_cpu.width = 50
self.fig_cpu.height = 5

self.resize_chart()

self.chart_colors: tuple[Iterable, Iterable] = {
'mem': tee(self.fig_mem._color_seq),
Expand All @@ -84,6 +83,18 @@ def create_chart(self):
label=name,
)

def resize_chart(self):
"""Resize chart."""
console_width, console_height = get_terminal_size()

chart_height = min((console_height - 20) // 2, 10)
chart_width = console_width - 30

self.fig_mem.width = chart_width
self.fig_mem.height = chart_height
self.fig_cpu.width = chart_width
self.fig_cpu.height = chart_height

def reset_chart(self):
"""Reset chart state."""
self.fig_mem._plots.clear()
Expand All @@ -92,6 +103,8 @@ def reset_chart(self):
self.fig_mem._color_seq = tee(self.chart_colors['mem'][0])[1]
self.fig_cpu._color_seq = tee(self.chart_colors['cpu'][0])[1]

self.resize_chart()

def reset_data(self):
"""Generate a clean data."""
current_time = datetime.datetime.now()
Expand Down

0 comments on commit 6cd4029

Please sign in to comment.