2424
2525
2626def _text_width (text ):
27- return max (len (_TEXT_RE .sub ("" , x )) for x in text .split ("\n " ))
27+ return max (len (_TEXT_RE .sub ("" , x )) + 2 for x in text .split ("\n " ))
2828
2929
3030def _just (text , width ):
@@ -39,7 +39,7 @@ def _justify_rows(text):
3939
4040
4141class Layout (object ):
42- def enable (self , display ):
42+ def pre_display (self , display ):
4343 pass
4444
4545 def press_escape (self , display ):
@@ -55,7 +55,7 @@ def render(self, display, now):
5555
5656
5757class KeyLayout (TextLayout ):
58- def enable (self , display ):
58+ def pre_display (self , display ):
5959 self .previous_screen = display .screen
6060
6161 def press_escape (self , display ):
@@ -88,7 +88,7 @@ def get_lines(self):
8888
8989
9090class StatLayout (Layout ):
91- def enable (self , display ):
91+ def pre_display (self , display ):
9292 self ._calc_x_positions (display )
9393
9494 def get_rows (self , display , stat , now ):
@@ -97,11 +97,10 @@ def get_rows(self, display, stat, now):
9797 def _calc_x_positions (self , display ):
9898 x = 0
9999 widths = []
100- midline = False
101100 for idx , (cname , _ , col_width , just ) in enumerate (self .columns ):
102- charwidth = max (
103- _text_width ( cname ), int (display ._winsize [1 ] * col_width )
104- )
101+ text_width = _text_width ( cname )
102+ layout_width = int (display ._winsize [1 ] * col_width )
103+ charwidth = max ( text_width , layout_width )
105104 if just == "L" :
106105 widths .append ((x , charwidth ))
107106 x += charwidth
@@ -114,15 +113,7 @@ def _calc_x_positions(self, display):
114113 for (r_cname , _ , r_col_width , _ ) in self .columns [idx :]
115114 )
116115 x = display ._winsize [1 ] - width
117- if not midline :
118- midline = True
119- midline_at = widths [- 1 ][0 ] + widths [- 1 ][1 ]
120- midline_over = midline_at - x
121- if midline_over > 0 :
122- widths [- 1 ] = (
123- widths [- 1 ][0 ],
124- widths [- 1 ][1 ] - midline_over ,
125- )
116+
126117 widths .append ((x , charwidth ))
127118
128119 self ._x_positions = widths
@@ -139,7 +130,12 @@ def _render_row(self, display, row, y):
139130 ]
140131 )
141132 x , charwidth = next (x_positions )
142- display ._render_str (y , x , elem ) # , max_=charwidth - 1)
133+ display ._render_str (
134+ y ,
135+ x ,
136+ elem ,
137+ center_within_width = charwidth if justify == "R" else None ,
138+ )
143139
144140 def render (self , display , now ):
145141 stat = display .stat
@@ -181,9 +177,21 @@ def render(self, display, now):
181177 cname , fmt , width , justify = col
182178 x , charwidth = next (x_positions )
183179 rows = _justify_rows (cname )
184- display ._render_str (top , x , rows [0 ], "Cb" )
180+ display ._render_str (
181+ top ,
182+ x ,
183+ rows [0 ],
184+ "Cb" ,
185+ center_within_width = charwidth if justify == "R" else None ,
186+ )
185187 if len (rows ) > 1 :
186- display ._render_str (top + 1 , x , rows [1 ], "Cb" )
188+ display ._render_str (
189+ top + 1 ,
190+ x ,
191+ rows [1 ],
192+ "Cb" ,
193+ center_within_width = charwidth if justify == "R" else None ,
194+ )
187195
188196 rows = self .get_rows (display , stat , now )
189197
@@ -199,7 +207,7 @@ class ProgStatsLayout(StatLayout):
199207 ("processes\n curr / max" , "%4d/%4d" , 0.15 , "R" ),
200208 ("connections\n curr / max / int" , "%4d/%4d/%4d" , 0.15 , "R" ),
201209 ("checkouts\n curr / max / int" , "%4d/%4d/%4d" , 0.15 , "R" ),
202- ("checkouts\n /sec" , "%.2f" , 0.15 , "R" ),
210+ ("checkouts\n /sec" , "%.2f" , 0.10 , "R" ),
203211 ]
204212
205213 def row_for_hostprog (self , hostprog , now ):
@@ -249,14 +257,7 @@ def get_rows(self, display, stat, now):
249257
250258
251259class HostStatsLayout (ProgStatsLayout ):
252- columns = [
253- ("hostname\n (#R&[dis]#G&connected#d&)" , "%s" , 0.12 , "L" ),
254- ("last msg\n secs / int" , "%s/%3d" , 0.15 , "R" ),
255- ("processes\n curr / max" , "%4d/%4d" , 0.15 , "R" ),
256- ("connections\n curr / max / int" , "%4d/%4d/%4d" , 0.15 , "R" ),
257- ("checkouts\n curr / max / int" , "%4d/%4d/%4d" , 0.15 , "R" ),
258- ("checkouts\n /sec" , "%3.2f" , 0.15 , "R" ),
259- ]
260+ columns = ProgStatsLayout .columns [0 :1 ] + ProgStatsLayout .columns [2 :]
260261
261262 def get_rows (self , display , stat , now ):
262263 rows = []
@@ -286,10 +287,9 @@ def _refresh_winsize(self, screen=None):
286287 ):
287288 curses .resize_term (* self ._winsize )
288289 if screen :
290+ screen .pre_display (self )
289291 self .screen = screen
290292
291- self .screen .enable (self )
292-
293293 if screen :
294294 self ._render (time .time ())
295295
@@ -360,9 +360,23 @@ def _get_color(self, color):
360360 self ._color_pairs [color ] = mapped
361361 return mapped
362362
363- def _render_str (self , y , x , text , default_color = "D" , max_ = None ):
363+ def _render_str (
364+ self ,
365+ y ,
366+ x ,
367+ text ,
368+ default_color = "D" ,
369+ max_ = None ,
370+ center_within_width = None ,
371+ ):
372+ text_width = _text_width (text )
364373 if x < 0 :
365- x = self ._winsize [1 ] - _text_width (text )
374+ x = self ._winsize [1 ] - text_width
375+ elif (
376+ center_within_width is not None
377+ and center_within_width > text_width
378+ ):
379+ x += (center_within_width - text_width ) // 2
366380
367381 current_color = dflt = self ._get_color (default_color )
368382 if max_ :
0 commit comments