@@ -922,6 +922,32 @@ def display_popup(
922
922
923
923
return None
924
924
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 ]
928
+
929
+ if plugin_stats ['align' ] == 'right' :
930
+ # Right align (last column)
931
+ display_x = screen_x - self .get_stats_display_width (plugin_stats )
932
+ else :
933
+ display_x = self .column
934
+ if plugin_stats ['align' ] == 'bottom' :
935
+ # Bottom (last line)
936
+ display_y = screen_y - self .get_stats_display_height (plugin_stats )
937
+ else :
938
+ display_y = self .line
939
+
940
+ return display_y , display_x
941
+
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' ]
946
+
947
+ return any ([has_optional , has_additional ])
948
+
949
+ return neither_optional_nor_additional
950
+
925
951
def display_plugin (self , plugin_stats , display_optional = True , display_additional = True , max_y = 65535 , add_space = 0 ):
926
952
"""Display the plugin_stats on the screen.
927
953
@@ -934,89 +960,97 @@ def display_plugin(self, plugin_stats, display_optional=True, display_additional
934
960
# Exit if:
935
961
# - the plugin_stats message is empty
936
962
# - the display tag = False
937
- if plugin_stats is None or not plugin_stats ['msgdict' ] or not plugin_stats ['display' ]:
963
+ conditions = [plugin_stats is None , not plugin_stats ['msgdict' ], not plugin_stats ['display' ]]
964
+ if any (conditions ):
938
965
# Exit
939
966
return 0
940
967
941
- # Get the screen size
942
- screen_x = self .term_window .getmaxyx ()[1 ]
968
+ # Set the upper/left position of the message
969
+ display_y , display_x = self .set_upper_left_pos (plugin_stats )
970
+
971
+ 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 ),
976
+ }
977
+
978
+ init = {'x' : display_x , 'x max' : display_x , 'y' : display_y }
979
+ y , x , x_max = self .display_msg (plugin_stats , init , helper )
980
+
981
+ # Compute the next Glances column/line position
982
+ self .next_column = max (self .next_column , x_max + self .space_between_column )
983
+ self .next_line = max (self .next_line , y + self .space_between_line )
984
+
985
+ # Have empty lines after the plugins
986
+ self .next_line += add_space
987
+ return None
988
+
989
+ def y_overbound (self , max_y ):
943
990
screen_y = self .term_window .getmaxyx ()[0 ]
944
991
945
- # Set the upper/left position of the message
946
- if plugin_stats ['align' ] == 'right' :
947
- # Right align (last column)
948
- display_x = screen_x - self .get_stats_display_width (plugin_stats )
949
- else :
950
- display_x = self .column
951
- if plugin_stats ['align' ] == 'bottom' :
952
- # Bottom (last line)
953
- display_y = screen_y - self .get_stats_display_height (plugin_stats )
954
- else :
955
- display_y = self .line
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 ]
956
996
957
- # Display
958
- x = display_x
959
- x_max = x
960
- y = display_y
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' ]
961
1004
for m in plugin_stats ['msgdict' ]:
962
1005
# New line
963
1006
try :
964
1007
if m ['msg' ].startswith ('\n ' ):
965
- # Go to the next line
966
- y += 1
967
- # Return to the first column
968
- x = display_x
1008
+ y , x = helper ['goto next and ret first col' ](y )
969
1009
continue
970
1010
except Exception :
971
1011
# Avoid exception (see issue #1692)
972
1012
pass
973
- # Do not display outside the screen
974
- if x < 0 :
975
- continue
976
- if not m ['splittable' ] and (x + len (m ['msg' ]) > screen_x ):
1013
+ if helper ['x overbound?' ](m , x ) or helper ['neither opt nor add?' ](m ):
977
1014
continue
978
- if y < 0 or ( y + 1 > screen_y ) or ( y > max_y ):
1015
+ if helper [ 'y overbound?' ]( y ):
979
1016
break
980
- # If display_optional = False do not display optional stats
981
- if not display_optional and m ['optional' ]:
982
- continue
983
- # If display_additional = False do not display additional stats
984
- if not display_additional and m ['additional' ]:
985
- continue
986
- # Is it possible to display the stat with the current screen size
987
- # !!! Crash if not try/except... Why ???
988
- 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
- )
997
- except Exception :
998
- pass
999
- 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
1017
+ x , x_max = self .display_stats_with_current_size (m , y , x , x_max )
1012
1018
1013
- # Compute the next Glances column/line position
1014
- self .next_column = max (self .next_column , x_max + self .space_between_column )
1015
- self .next_line = max (self .next_line , y + self .space_between_line )
1019
+ return y , x , x_max
1016
1020
1017
- # Have empty lines after the plugins
1018
- self .next_line += add_space
1019
- return None
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
1020
1054
1021
1055
def clear (self ):
1022
1056
"""Erase the content of the screen.
0 commit comments