@@ -140,71 +140,75 @@ def update(self):
140
140
141
141
return self .stats
142
142
143
- def msg_curse (self , args = None , max_width = None ):
144
- """Return the dict to display in the curse interface."""
145
- # Init the return message
146
- ret = []
147
-
148
- # Only process if stats exist...
149
- if not self .stats or not self .args .percpu or self .is_disabled ():
150
- return ret
151
-
152
- # Define the headers based on OS
153
- header = ['user' , 'system' ]
143
+ def define_headers_from_os (self ):
144
+ base = ['user' , 'system' ]
154
145
155
146
if LINUX :
156
- header . extend ( ['iowait' , 'idle' , 'irq' , 'nice' , 'steal' , 'guest' ])
147
+ extension = ['iowait' , 'idle' , 'irq' , 'nice' , 'steal' , 'guest' ]
157
148
elif MACOS :
158
- header . extend ( ['idle' , 'nice' ])
149
+ extension = ['idle' , 'nice' ]
159
150
elif BSD :
160
- header . extend ( ['idle' , 'irq' , 'nice' ])
151
+ extension = ['idle' , 'irq' , 'nice' ]
161
152
elif WINDOWS :
162
- header . extend ( ['dpc' , 'interrupt' ])
153
+ extension = ['dpc' , 'interrupt' ]
163
154
164
- # Build the string message
155
+ return base + extension
156
+
157
+ def maybe_build_string_msg (self , header , return_ ):
165
158
if self .is_disabled ('quicklook' ):
166
159
msg = '{:5}' .format ('CPU' )
167
- ret .append (self .curse_add_line (msg , "TITLE" ))
160
+ return_ .append (self .curse_add_line (msg , "TITLE" ))
168
161
header .insert (0 , 'total' )
169
162
170
- # Per CPU stats displayed per line
163
+ return (header , return_ )
164
+
165
+ def display_cpu_stats_per_line (self , header , return_ ):
171
166
for stat in header :
172
167
msg = f'{ stat :>7} '
173
- ret .append (self .curse_add_line (msg ))
168
+ return_ .append (self .curse_add_line (msg ))
174
169
175
- # Manage the maximum number of CPU to display (related to enhancement request #2734)
170
+ return return_
171
+
172
+ def manage_max_cpu_to_display (self ):
176
173
if len (self .stats ) > self .max_cpu_display :
177
- # If the number of CPU is > max_cpu_display then sort and display top 'n'
174
+ # sort and display top 'n'
178
175
percpu_list = sorted (self .stats , key = lambda x : x ['total' ], reverse = True )
179
176
else :
180
177
percpu_list = self .stats
181
178
182
- # Per CPU stats displayed per column
183
- for cpu in percpu_list [0 : self .max_cpu_display ]:
184
- ret .append (self .curse_new_line ())
185
- if self .is_disabled ('quicklook' ):
186
- try :
187
- cpu_id = cpu [cpu ['key' ]]
188
- if cpu_id < 10 :
189
- msg = f'CPU{ cpu_id :1} '
190
- else :
191
- msg = f'{ cpu_id :4} '
192
- except TypeError :
193
- # TypeError: string indices must be integers (issue #1027)
194
- msg = '{:4} ' .format ('?' )
195
- ret .append (self .curse_add_line (msg ))
196
- for stat in header :
197
- try :
198
- msg = f'{ cpu [stat ]:6.1f} %'
199
- except TypeError :
200
- msg = '{:>6}%' .format ('?' )
201
- ret .append (self .curse_add_line (msg , self .get_alert (cpu [stat ], header = stat )))
179
+ return percpu_list
202
180
203
- # Add a new line with sum of all others CPU
181
+ def display_cpu_header_in_columns (self , cpu , return_ ):
182
+ return_ .append (self .curse_new_line ())
183
+ if self .is_disabled ('quicklook' ):
184
+ try :
185
+ cpu_id = cpu [cpu ['key' ]]
186
+ if cpu_id < 10 :
187
+ msg = f'CPU{ cpu_id :1} '
188
+ else :
189
+ msg = f'{ cpu_id :4} '
190
+ except TypeError :
191
+ # TypeError: string indices must be integers (issue #1027)
192
+ msg = '{:4} ' .format ('?' )
193
+ return_ .append (self .curse_add_line (msg ))
194
+
195
+ return return_
196
+
197
+ def display_cpu_stats_in_columns (self , cpu , header , return_ ):
198
+ for stat in header :
199
+ try :
200
+ msg = f'{ cpu [stat ]:6.1f} %'
201
+ except TypeError :
202
+ msg = '{:>6}%' .format ('?' )
203
+ return_ .append (self .curse_add_line (msg , self .get_alert (cpu [stat ], header = stat )))
204
+
205
+ return return_
206
+
207
+ def summarize_all_cpus_not_displayed (self , percpu_list , header , return_ ):
204
208
if len (self .stats ) > self .max_cpu_display :
205
- ret .append (self .curse_new_line ())
209
+ return_ .append (self .curse_new_line ())
206
210
if self .is_disabled ('quicklook' ):
207
- ret .append (self .curse_add_line ('CPU* ' ))
211
+ return_ .append (self .curse_add_line ('CPU* ' ))
208
212
209
213
for stat in header :
210
214
percpu_stats = [i [stat ] for i in percpu_list [0 : self .max_cpu_display ]]
@@ -213,6 +217,39 @@ def msg_curse(self, args=None, max_width=None):
213
217
msg = f'{ cpu_stat :6.1f} %'
214
218
except TypeError :
215
219
msg = '{:>6}%' .format ('?' )
216
- ret .append (self .curse_add_line (msg , self .get_alert (cpu_stat , header = stat )))
220
+ return_ .append (self .curse_add_line (msg , self .get_alert (cpu_stat , header = stat )))
221
+
222
+ return return_
223
+
224
+ def msg_curse (self , args = None , max_width = None ):
225
+ """Return the dict to display in the curse interface."""
226
+
227
+ # Init the return message
228
+ return_ = []
229
+
230
+ # Only process if stats exist...
231
+ missing = [not self .stats , not self .args .percpu , self .is_disabled ()]
232
+ if any (missing ):
233
+ return return_
234
+
235
+ # Define the headers based on OS
236
+ header = self .define_headers_from_os ()
237
+
238
+ # Build the string message
239
+ header , return_ = self .maybe_build_string_msg (header , return_ )
240
+
241
+ # Per CPU stats displayed per line
242
+ return_ = self .display_cpu_stats_per_line (header , return_ )
243
+
244
+ # Manage the maximum number of CPU to display (related to enhancement request #2734)
245
+ percpu_list = self .manage_max_cpu_to_display ()
246
+
247
+ # Per CPU stats displayed per column
248
+ for cpu in percpu_list [0 : self .max_cpu_display ]:
249
+ header_added = self .display_cpu_header_in_columns (cpu , return_ )
250
+ stats_added = self .display_cpu_stats_in_columns (cpu , header , header_added )
251
+
252
+ # Add a new line with sum of all others CPU
253
+ return_ = self .summarize_all_cpus_not_displayed (percpu_list , header , stats_added )
217
254
218
- return ret
255
+ return return_
0 commit comments