Skip to content

Commit 4bcbc12

Browse files
committed
Refactorized _GlancesCurses.display_plugin(), version 2
Part of nicolargo#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>
1 parent 400013b commit 4bcbc12

File tree

1 file changed

+77
-52
lines changed

1 file changed

+77
-52
lines changed

glances/outputs/glances_curses.py

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -922,93 +922,118 @@ def display_popup(
922922

923923
return None
924924

925-
def display_plugin(self, plugin_stats, display_optional=True, display_additional=True, max_y=65535, add_space=0):
926-
"""Display the plugin_stats on the screen.
927-
928-
:param plugin_stats:
929-
:param display_optional: display the optional stats if True
930-
:param display_additional: display additional stats if True
931-
:param max_y: do not display line > max_y
932-
:param add_space: add x space (line) after the plugin
933-
"""
934-
# Exit if:
935-
# - the plugin_stats message is empty
936-
# - the display tag = False
937-
if plugin_stats is None or not plugin_stats['msgdict'] or not plugin_stats['display']:
938-
# Exit
939-
return 0
925+
def setup_upper_left_pos(self, plugin_stats):
926+
screen_y, screen_x = self.term_window.getmaxyx()
940927

941-
# Get the screen size
942-
screen_x = self.term_window.getmaxyx()[1]
943-
screen_y = self.term_window.getmaxyx()[0]
944-
945-
# Set the upper/left position of the message
946928
if plugin_stats['align'] == 'right':
947929
# Right align (last column)
948930
display_x = screen_x - self.get_stats_display_width(plugin_stats)
949931
else:
950932
display_x = self.column
933+
951934
if plugin_stats['align'] == 'bottom':
952935
# Bottom (last line)
953936
display_y = screen_y - self.get_stats_display_height(plugin_stats)
954937
else:
955938
display_y = self.line
956939

957-
# Display
958-
x = display_x
959-
x_max = x
960-
y = display_y
940+
return display_y, display_x
941+
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
957+
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
961971
for m in plugin_stats['msgdict']:
962972
# New line
963973
try:
964974
if m['msg'].startswith('\n'):
965-
# Go to the next line
966-
y += 1
967-
# Return to the first column
968-
x = display_x
975+
y, x = helper['goto next, add first col'](y, x)
969976
continue
970977
except Exception:
971978
# Avoid exception (see issue #1692)
972979
pass
973980
# Do not display outside the screen
974981
if x < 0:
975982
continue
976-
if not m['splittable'] and (x + len(m['msg']) > screen_x):
983+
if helper['x overbound?'](m, x):
977984
continue
978-
if y < 0 or (y + 1 > screen_y) or (y > max_y):
985+
if helper['y overbound?'](y):
979986
break
980987
# If display_optional = False do not display optional stats
981-
if not display_optional and m['optional']:
988+
if helper['display optional?'](m):
982989
continue
983990
# If display_additional = False do not display additional stats
984-
if not display_additional and m['additional']:
991+
if helper['display additional?'](m):
985992
continue
986993
# Is it possible to display the stat with the current screen size
987994
# !!! Crash if not try/except... Why ???
988995
try:
989-
self.term_window.addnstr(
990-
y,
991-
x,
992-
m['msg'],
993-
# Do not display outside the screen
994-
screen_x - x,
995-
self.colors_list[m['decoration']],
996-
)
996+
self.display_stats_with_current_size(m, y, x)
997997
except Exception:
998998
pass
999999
else:
1000-
# New column
1001-
# Python 2: we need to decode to get real screen size because
1002-
# UTF-8 special tree chars occupy several bytes.
1003-
# Python 3: strings are strings and bytes are bytes, all is
1004-
# good.
1005-
try:
1006-
x += len(u(m['msg']))
1007-
except UnicodeDecodeError:
1008-
# Quick and dirty hack for issue #745
1009-
pass
1010-
if x > x_max:
1011-
x_max = x
1000+
x, x_max = self.get_next_x_and_x_max(m, x, x_max)
1001+
1002+
return y, x, x_max
1003+
1004+
def display_plugin(self, plugin_stats, display_optional=True, display_additional=True, max_y=65535, add_space=0):
1005+
"""Display the plugin_stats on the screen.
1006+
1007+
:param plugin_stats:
1008+
:param display_optional: display the optional stats if True
1009+
:param display_additional: display additional stats if True
1010+
:param max_y: do not display line > max_y
1011+
:param add_space: add x space (line) after the plugin
1012+
"""
1013+
# Exit if:
1014+
# - the plugin_stats message is empty
1015+
# - the display tag = False
1016+
if plugin_stats is None or not plugin_stats['msgdict'] or not plugin_stats['display']:
1017+
# Exit
1018+
return 0
1019+
1020+
# Get the screen size
1021+
screen_y, screen_x = self.term_window.getmaxyx()
1022+
1023+
# Set the upper/left position of the message
1024+
display_y, display_x = self.setup_upper_left_pos(plugin_stats)
1025+
1026+
helper = {
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'],
1032+
}
1033+
1034+
# Display
1035+
init = display_y, display_x, display_x
1036+
y, x, x_max = self.display_stats(plugin_stats, init, helper)
10121037

10131038
# Compute the next Glances column/line position
10141039
self.next_column = max(self.next_column, x_max + self.space_between_column)

0 commit comments

Comments
 (0)