Skip to content

Commit 42af55b

Browse files
authored
Merge pull request nicolargo#2950 from ariel-anieli/revert-commit-01823df9
Refactorized `_GlancesCurses.display_plugin()`, version 2
2 parents 75fdc8f + 4bcbc12 commit 42af55b

File tree

1 file changed

+75
-84
lines changed

1 file changed

+75
-84
lines changed

glances/outputs/glances_curses.py

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -922,15 +922,15 @@ def display_popup(
922922

923923
return None
924924

925-
def set_upper_left_pos(self, plugin_stats):
926-
screen_x = self.term_window.getmaxyx()[1]
927-
screen_y = self.term_window.getmaxyx()[0]
925+
def setup_upper_left_pos(self, plugin_stats):
926+
screen_y, screen_x = self.term_window.getmaxyx()
928927

929928
if plugin_stats['align'] == 'right':
930929
# Right align (last column)
931930
display_x = screen_x - self.get_stats_display_width(plugin_stats)
932931
else:
933932
display_x = self.column
933+
934934
if plugin_stats['align'] == 'bottom':
935935
# Bottom (last line)
936936
display_y = screen_y - self.get_stats_display_height(plugin_stats)
@@ -939,14 +939,67 @@ def set_upper_left_pos(self, plugin_stats):
939939

940940
return display_y, display_x
941941

942-
def check_opt_and_add(self, display_optional, display_additional):
943-
def neither_optional_nor_additional(m):
944-
has_optional = not display_optional and m['optional']
945-
has_additional = not display_additional and m['additional']
942+
def get_next_x_and_x_max(self, m, x, x_max):
943+
# New column
944+
# Python 2: we need to decode to get real screen size because
945+
# UTF-8 special tree chars occupy several bytes.
946+
# Python 3: strings are strings and bytes are bytes, all is
947+
# good.
948+
try:
949+
x += len(u(m['msg']))
950+
except UnicodeDecodeError:
951+
# Quick and dirty hack for issue #745
952+
pass
953+
if x > x_max:
954+
x_max = x
955+
956+
return x, x_max
946957

947-
return any([has_optional, has_additional])
958+
def display_stats_with_current_size(self, m, y, x):
959+
screen_x = self.term_window.getmaxyx()[1]
960+
self.term_window.addnstr(
961+
y,
962+
x,
963+
m['msg'],
964+
# Do not display outside the screen
965+
screen_x - x,
966+
self.colors_list[m['decoration']],
967+
)
968+
969+
def display_stats(self, plugin_stats, init, helper):
970+
y, x, x_max = init
971+
for m in plugin_stats['msgdict']:
972+
# New line
973+
try:
974+
if m['msg'].startswith('\n'):
975+
y, x = helper['goto next, add first col'](y, x)
976+
continue
977+
except Exception:
978+
# Avoid exception (see issue #1692)
979+
pass
980+
# Do not display outside the screen
981+
if x < 0:
982+
continue
983+
if helper['x overbound?'](m, x):
984+
continue
985+
if helper['y overbound?'](y):
986+
break
987+
# If display_optional = False do not display optional stats
988+
if helper['display optional?'](m):
989+
continue
990+
# If display_additional = False do not display additional stats
991+
if helper['display additional?'](m):
992+
continue
993+
# Is it possible to display the stat with the current screen size
994+
# !!! Crash if not try/except... Why ???
995+
try:
996+
self.display_stats_with_current_size(m, y, x)
997+
except Exception:
998+
pass
999+
else:
1000+
x, x_max = self.get_next_x_and_x_max(m, x, x_max)
9481001

949-
return neither_optional_nor_additional
1002+
return y, x, x_max
9501003

9511004
def display_plugin(self, plugin_stats, display_optional=True, display_additional=True, max_y=65535, add_space=0):
9521005
"""Display the plugin_stats on the screen.
@@ -960,23 +1013,27 @@ def display_plugin(self, plugin_stats, display_optional=True, display_additional
9601013
# Exit if:
9611014
# - the plugin_stats message is empty
9621015
# - the display tag = False
963-
conditions = [plugin_stats is None, not plugin_stats['msgdict'], not plugin_stats['display']]
964-
if any(conditions):
1016+
if plugin_stats is None or not plugin_stats['msgdict'] or not plugin_stats['display']:
9651017
# Exit
9661018
return 0
9671019

1020+
# Get the screen size
1021+
screen_y, screen_x = self.term_window.getmaxyx()
1022+
9681023
# Set the upper/left position of the message
969-
display_y, display_x = self.set_upper_left_pos(plugin_stats)
1024+
display_y, display_x = self.setup_upper_left_pos(plugin_stats)
9701025

9711026
helper = {
972-
'goto next and ret first col': self.goto_next_and_ret_first_col(display_x),
973-
'neither opt nor add?': self.check_opt_and_add(display_optional, display_additional),
974-
'x overbound?': self.x_overbound,
975-
'y overbound?': self.y_overbound(max_y),
1027+
'goto next, add first col': lambda y, x: (y + 1, display_x),
1028+
'x overbound?': lambda m, x: not m['splittable'] and (x + len(m['msg']) > screen_x),
1029+
'y overbound?': lambda y: y < 0 or (y + 1 > screen_y) or (y > max_y),
1030+
'display optional?': lambda m: not display_optional and m['optional'],
1031+
'display additional?': lambda m: not display_additional and m['additional'],
9761032
}
9771033

978-
init = {'x': display_x, 'x max': display_x, 'y': display_y}
979-
y, x, x_max = self.display_msg(plugin_stats, init, helper)
1034+
# Display
1035+
init = display_y, display_x, display_x
1036+
y, x, x_max = self.display_stats(plugin_stats, init, helper)
9801037

9811038
# Compute the next Glances column/line position
9821039
self.next_column = max(self.next_column, x_max + self.space_between_column)
@@ -986,72 +1043,6 @@ def display_plugin(self, plugin_stats, display_optional=True, display_additional
9861043
self.next_line += add_space
9871044
return None
9881045

989-
def y_overbound(self, max_y):
990-
screen_y = self.term_window.getmaxyx()[0]
991-
992-
return lambda y: y < 0 or (y + 1 > screen_y) or (y > max_y)
993-
994-
def x_overbound(self, m, x):
995-
screen_x = self.term_window.getmaxyx()[1]
996-
997-
return x < 0 or not m['splittable'] and (x + len(m['msg']) > screen_x)
998-
999-
def goto_next_and_ret_first_col(self, display_x):
1000-
return lambda y: (y + 1, display_x)
1001-
1002-
def display_msg(self, plugin_stats, init, helper):
1003-
y, x, x_max = init['y'], init['x'], init['x max']
1004-
for m in plugin_stats['msgdict']:
1005-
# New line
1006-
try:
1007-
if m['msg'].startswith('\n'):
1008-
y, x = helper['goto next and ret first col'](y)
1009-
continue
1010-
except Exception:
1011-
# Avoid exception (see issue #1692)
1012-
pass
1013-
if helper['x overbound?'](m, x) or helper['neither opt nor add?'](m):
1014-
continue
1015-
if helper['y overbound?'](y):
1016-
break
1017-
x, x_max = self.display_stats_with_current_size(m, y, x, x_max)
1018-
1019-
return y, x, x_max
1020-
1021-
def display_stats_with_current_size(self, m, y, x, x_max):
1022-
# Is it possible to display the stat with the current screen size
1023-
# !!! Crash if not try/except... Why ???
1024-
screen_x = self.term_window.getmaxyx()[1]
1025-
try:
1026-
self.term_window.addnstr(
1027-
y,
1028-
x,
1029-
m['msg'],
1030-
# Do not display outside the screen
1031-
screen_x - x,
1032-
self.colors_list[m['decoration']],
1033-
)
1034-
except Exception:
1035-
pass
1036-
else:
1037-
return self.add_new_colum(m, x, x_max)
1038-
1039-
def add_new_colum(self, m, x, x_max):
1040-
# New column
1041-
# Python 2: we need to decode to get real screen size because
1042-
# UTF-8 special tree chars occupy several bytes.
1043-
# Python 3: strings are strings and bytes are bytes, all is
1044-
# good.
1045-
try:
1046-
x += len(u(m['msg']))
1047-
except UnicodeDecodeError:
1048-
# Quick and dirty hack for issue #745
1049-
pass
1050-
if x > x_max:
1051-
x_max = x
1052-
1053-
return x, x_max
1054-
10551046
def clear(self):
10561047
"""Erase the content of the screen.
10571048
The difference is that clear() also calls clearok(). clearok()

0 commit comments

Comments
 (0)