Refactorized `_GlancesCurses.display_plugin()`, version 2

Part of #2801. Broken down using:

1. `setup_upper_left_pos`
2. `get_next_x_and_x_max`
3. `display_stats_with_current_size`
4. `display_stats`

Signed-off-by: Ariel Otilibili <otilibil@eurecom.fr>
pull/2950/head
Ariel Otilibili 2024-09-17 17:16:39 +02:00
parent 400013bb4a
commit 4bcbc12cea
1 changed files with 91 additions and 66 deletions

View File

@ -922,6 +922,85 @@ class _GlancesCurses:
return None return None
def setup_upper_left_pos(self, plugin_stats):
screen_y, screen_x = self.term_window.getmaxyx()
if plugin_stats['align'] == 'right':
# Right align (last column)
display_x = screen_x - self.get_stats_display_width(plugin_stats)
else:
display_x = self.column
if plugin_stats['align'] == 'bottom':
# Bottom (last line)
display_y = screen_y - self.get_stats_display_height(plugin_stats)
else:
display_y = self.line
return display_y, display_x
def get_next_x_and_x_max(self, m, x, x_max):
# New column
# Python 2: we need to decode to get real screen size because
# UTF-8 special tree chars occupy several bytes.
# Python 3: strings are strings and bytes are bytes, all is
# good.
try:
x += len(u(m['msg']))
except UnicodeDecodeError:
# Quick and dirty hack for issue #745
pass
if x > x_max:
x_max = x
return x, x_max
def display_stats_with_current_size(self, m, y, x):
screen_x = self.term_window.getmaxyx()[1]
self.term_window.addnstr(
y,
x,
m['msg'],
# Do not display outside the screen
screen_x - x,
self.colors_list[m['decoration']],
)
def display_stats(self, plugin_stats, init, helper):
y, x, x_max = init
for m in plugin_stats['msgdict']:
# New line
try:
if m['msg'].startswith('\n'):
y, x = helper['goto next, add first col'](y, x)
continue
except Exception:
# Avoid exception (see issue #1692)
pass
# Do not display outside the screen
if x < 0:
continue
if helper['x overbound?'](m, x):
continue
if helper['y overbound?'](y):
break
# If display_optional = False do not display optional stats
if helper['display optional?'](m):
continue
# If display_additional = False do not display additional stats
if helper['display additional?'](m):
continue
# Is it possible to display the stat with the current screen size
# !!! Crash if not try/except... Why ???
try:
self.display_stats_with_current_size(m, y, x)
except Exception:
pass
else:
x, x_max = self.get_next_x_and_x_max(m, x, x_max)
return y, x, x_max
def display_plugin(self, plugin_stats, display_optional=True, display_additional=True, max_y=65535, add_space=0): def display_plugin(self, plugin_stats, display_optional=True, display_additional=True, max_y=65535, add_space=0):
"""Display the plugin_stats on the screen. """Display the plugin_stats on the screen.
@ -939,76 +1018,22 @@ class _GlancesCurses:
return 0 return 0
# Get the screen size # Get the screen size
screen_x = self.term_window.getmaxyx()[1] screen_y, screen_x = self.term_window.getmaxyx()
screen_y = self.term_window.getmaxyx()[0]
# Set the upper/left position of the message # Set the upper/left position of the message
if plugin_stats['align'] == 'right': display_y, display_x = self.setup_upper_left_pos(plugin_stats)
# Right align (last column)
display_x = screen_x - self.get_stats_display_width(plugin_stats) helper = {
else: 'goto next, add first col': lambda y, x: (y + 1, display_x),
display_x = self.column 'x overbound?': lambda m, x: not m['splittable'] and (x + len(m['msg']) > screen_x),
if plugin_stats['align'] == 'bottom': 'y overbound?': lambda y: y < 0 or (y + 1 > screen_y) or (y > max_y),
# Bottom (last line) 'display optional?': lambda m: not display_optional and m['optional'],
display_y = screen_y - self.get_stats_display_height(plugin_stats) 'display additional?': lambda m: not display_additional and m['additional'],
else: }
display_y = self.line
# Display # Display
x = display_x init = display_y, display_x, display_x
x_max = x y, x, x_max = self.display_stats(plugin_stats, init, helper)
y = display_y
for m in plugin_stats['msgdict']:
# New line
try:
if m['msg'].startswith('\n'):
# Go to the next line
y += 1
# Return to the first column
x = display_x
continue
except Exception:
# Avoid exception (see issue #1692)
pass
# Do not display outside the screen
if x < 0:
continue
if not m['splittable'] and (x + len(m['msg']) > screen_x):
continue
if y < 0 or (y + 1 > screen_y) or (y > max_y):
break
# If display_optional = False do not display optional stats
if not display_optional and m['optional']:
continue
# If display_additional = False do not display additional stats
if not display_additional and m['additional']:
continue
# Is it possible to display the stat with the current screen size
# !!! Crash if not try/except... Why ???
try:
self.term_window.addnstr(
y,
x,
m['msg'],
# Do not display outside the screen
screen_x - x,
self.colors_list[m['decoration']],
)
except Exception:
pass
else:
# New column
# Python 2: we need to decode to get real screen size because
# UTF-8 special tree chars occupy several bytes.
# Python 3: strings are strings and bytes are bytes, all is
# good.
try:
x += len(u(m['msg']))
except UnicodeDecodeError:
# Quick and dirty hack for issue #745
pass
if x > x_max:
x_max = x
# Compute the next Glances column/line position # Compute the next Glances column/line position
self.next_column = max(self.next_column, x_max + self.space_between_column) self.next_column = max(self.next_column, x_max + self.space_between_column)