@@ -922,93 +922,118 @@ def display_popup(
922
922
923
923
return None
924
924
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 ()
940
927
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
946
928
if plugin_stats ['align' ] == 'right' :
947
929
# Right align (last column)
948
930
display_x = screen_x - self .get_stats_display_width (plugin_stats )
949
931
else :
950
932
display_x = self .column
933
+
951
934
if plugin_stats ['align' ] == 'bottom' :
952
935
# Bottom (last line)
953
936
display_y = screen_y - self .get_stats_display_height (plugin_stats )
954
937
else :
955
938
display_y = self .line
956
939
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
961
971
for m in plugin_stats ['msgdict' ]:
962
972
# New line
963
973
try :
964
974
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 )
969
976
continue
970
977
except Exception :
971
978
# Avoid exception (see issue #1692)
972
979
pass
973
980
# Do not display outside the screen
974
981
if x < 0 :
975
982
continue
976
- if not m [ 'splittable' ] and ( x + len ( m [ 'msg' ]) > screen_x ):
983
+ if helper [ 'x overbound?' ]( m , x ):
977
984
continue
978
- if y < 0 or ( y + 1 > screen_y ) or ( y > max_y ):
985
+ if helper [ 'y overbound?' ]( y ):
979
986
break
980
987
# If display_optional = False do not display optional stats
981
- if not display_optional and m [ ' optional' ] :
988
+ if helper [ 'display optional?' ]( m ) :
982
989
continue
983
990
# If display_additional = False do not display additional stats
984
- if not display_additional and m [ ' additional' ] :
991
+ if helper [ 'display additional?' ]( m ) :
985
992
continue
986
993
# Is it possible to display the stat with the current screen size
987
994
# !!! Crash if not try/except... Why ???
988
995
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 )
997
997
except Exception :
998
998
pass
999
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
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 )
1012
1037
1013
1038
# Compute the next Glances column/line position
1014
1039
self .next_column = max (self .next_column , x_max + self .space_between_column )
0 commit comments