Conditions | 27 |
Total Lines | 144 |
Code Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like glances.outputs.glances_curses_browser.GlancesCursesBrowser.display() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
256 | def display(self, stats, cs_status=None): |
||
257 | """Display the servers list. |
||
258 | |||
259 | Return: |
||
260 | True if the stats have been displayed |
||
261 | False if the stats have not been displayed (no server available) |
||
262 | """ |
||
263 | # Init the internal line/column for Glances Curses |
||
264 | self.init_line_column() |
||
265 | |||
266 | # Get the current screen size |
||
267 | screen_x = self.screen.getmaxyx()[1] |
||
268 | screen_y = self.screen.getmaxyx()[0] |
||
269 | stats_max = screen_y - 3 |
||
270 | stats_len = len(stats) |
||
271 | |||
272 | self._page_max_lines = stats_max |
||
273 | self._page_max = int(math.ceil(stats_len / stats_max)) |
||
274 | # Init position |
||
275 | x = 0 |
||
276 | y = 0 |
||
277 | |||
278 | # Display top header |
||
279 | if stats_len == 0: |
||
280 | if self.first_scan and not self.args.disable_autodiscover: |
||
281 | msg = 'Glances is scanning your network. Please wait...' |
||
282 | self.first_scan = False |
||
283 | else: |
||
284 | msg = 'No Glances server available' |
||
285 | elif len(stats) == 1: |
||
286 | msg = 'One Glances server available' |
||
287 | else: |
||
288 | msg = '{} Glances servers available'.format(stats_len) |
||
289 | if self.args.disable_autodiscover: |
||
290 | msg += ' (auto discover is disabled)' |
||
291 | if screen_y > 1: |
||
292 | self.term_window.addnstr(y, x, |
||
293 | msg, |
||
294 | screen_x - x, |
||
295 | self.colors_list['TITLE']) |
||
296 | |||
297 | msg = '{}'.format(self._get_status_count(stats)) |
||
298 | self.term_window.addnstr(y + 1, x, |
||
299 | msg, |
||
300 | screen_x - x) |
||
301 | |||
302 | if stats_len > stats_max and screen_y > 2: |
||
303 | msg = '{} servers displayed.({}/{}) {}'.format(self.get_pagelines(stats), |
||
304 | self._current_page + 1, |
||
305 | self._page_max, |
||
306 | self._get_status_count(stats)) |
||
307 | self.term_window.addnstr(y + 1, x, |
||
308 | msg, |
||
309 | screen_x - x) |
||
310 | |||
311 | if stats_len == 0: |
||
312 | return False |
||
313 | |||
314 | # Display the Glances server list |
||
315 | # ================================ |
||
316 | |||
317 | # Table of table |
||
318 | # Item description: [stats_id, column name, column size] |
||
319 | column_def = [ |
||
320 | ['name', 'Name', 16], |
||
321 | ['alias', None, None], |
||
322 | ['load_min5', 'LOAD', 6], |
||
323 | ['cpu_percent', 'CPU%', 5], |
||
324 | ['mem_percent', 'MEM%', 5], |
||
325 | ['status', 'STATUS', 9], |
||
326 | ['ip', 'IP', 15], |
||
327 | # ['port', 'PORT', 5], |
||
328 | ['hr_name', 'OS', 16], |
||
329 | ] |
||
330 | y = 2 |
||
331 | |||
332 | # Display table header |
||
333 | xc = x + 2 |
||
334 | for cpt, c in enumerate(column_def): |
||
335 | if xc < screen_x and y < screen_y and c[1] is not None: |
||
336 | self.term_window.addnstr(y, xc, |
||
337 | c[1], |
||
338 | screen_x - x, |
||
339 | self.colors_list['BOLD']) |
||
340 | xc += c[2] + self.space_between_column |
||
341 | y += 1 |
||
342 | |||
343 | # If a servers has been deleted from the list... |
||
344 | # ... and if the cursor is in the latest position |
||
345 | if self.cursor > len(stats) - 1: |
||
346 | # Set the cursor position to the latest item |
||
347 | self.cursor = len(stats) - 1 |
||
348 | |||
349 | stats_list = self._get_stats(stats) |
||
350 | start_line = self._page_max_lines * self._current_page |
||
351 | end_line = start_line + self.get_pagelines(stats_list) |
||
352 | current_page = stats_list[start_line:end_line] |
||
353 | |||
354 | # Display table |
||
355 | line = 0 |
||
356 | for v in current_page: |
||
357 | # Limit the number of displayed server (see issue #1256) |
||
358 | if line >= stats_max: |
||
359 | continue |
||
360 | # Get server stats |
||
361 | server_stat = {} |
||
362 | for c in column_def: |
||
363 | try: |
||
364 | server_stat[c[0]] = v[c[0]] |
||
365 | except KeyError as e: |
||
366 | logger.debug( |
||
367 | "Cannot grab stats {} from server (KeyError: {})".format(c[0], e)) |
||
368 | server_stat[c[0]] = '?' |
||
369 | # Display alias instead of name |
||
370 | try: |
||
371 | if c[0] == 'alias' and v[c[0]] is not None: |
||
372 | server_stat['name'] = v[c[0]] |
||
373 | except KeyError: |
||
374 | pass |
||
375 | |||
376 | # Display line for server stats |
||
377 | cpt = 0 |
||
378 | xc = x |
||
379 | |||
380 | # Is the line selected ? |
||
381 | if line == self.cursor: |
||
382 | # Display cursor |
||
383 | self.term_window.addnstr( |
||
384 | y, xc, ">", screen_x - xc, self.colors_list['BOLD']) |
||
385 | |||
386 | # Display the line |
||
387 | xc += 2 |
||
388 | for c in column_def: |
||
389 | if xc < screen_x and y < screen_y and c[1] is not None: |
||
390 | # Display server stats |
||
391 | self.term_window.addnstr( |
||
392 | y, xc, format(server_stat[c[0]]), c[2], self.colors_list[v['status']]) |
||
393 | xc += c[2] + self.space_between_column |
||
394 | cpt += 1 |
||
395 | # Next line, next server... |
||
396 | y += 1 |
||
397 | line += 1 |
||
398 | |||
399 | return True |
||
400 |