| Conditions | 23 |
| Total Lines | 117 |
| Lines | 0 |
| Ratio | 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.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 -*- |
||
| 157 | def display(self, servers_list): |
||
| 158 | """Display the servers list. |
||
| 159 | |||
| 160 | Return: |
||
| 161 | True if the stats have been displayed |
||
| 162 | False if the stats have not been displayed (no server available) |
||
| 163 | """ |
||
| 164 | # Init the internal line/column for Glances Curses |
||
| 165 | self.init_line_column() |
||
| 166 | |||
| 167 | # Get the current screen size |
||
| 168 | screen_x = self.screen.getmaxyx()[1] |
||
| 169 | screen_y = self.screen.getmaxyx()[0] |
||
| 170 | |||
| 171 | # Init position |
||
| 172 | x = 0 |
||
| 173 | y = 0 |
||
| 174 | |||
| 175 | # Display top header |
||
| 176 | if len(servers_list) == 0: |
||
| 177 | if self.first_scan and not self.args.disable_autodiscover: |
||
| 178 | msg = 'Glances is scanning your network. Please wait...' |
||
| 179 | self.first_scan = False |
||
| 180 | else: |
||
| 181 | msg = 'No Glances server available' |
||
| 182 | elif len(servers_list) == 1: |
||
| 183 | msg = 'One Glances server available' |
||
| 184 | else: |
||
| 185 | msg = '{0} Glances servers available'.format(len(servers_list)) |
||
| 186 | if self.args.disable_autodiscover: |
||
| 187 | msg += ' ' + '(auto discover is disabled)' |
||
| 188 | self.term_window.addnstr(y, x, |
||
| 189 | msg, |
||
| 190 | screen_x - x, |
||
| 191 | self.colors_list['TITLE']) |
||
| 192 | |||
| 193 | if len(servers_list) == 0: |
||
| 194 | return False |
||
| 195 | |||
| 196 | # Display the Glances server list |
||
| 197 | # ================================ |
||
| 198 | |||
| 199 | # Table of table |
||
| 200 | # Item description: [stats_id, column name, column size] |
||
| 201 | column_def = [ |
||
| 202 | ['name', 'Name', 16], |
||
| 203 | ['alias', None, None], |
||
| 204 | ['load_min5', 'LOAD', 6], |
||
| 205 | ['cpu_percent', 'CPU%', 5], |
||
| 206 | ['mem_percent', 'MEM%', 5], |
||
| 207 | ['status', 'STATUS', 9], |
||
| 208 | ['ip', 'IP', 15], |
||
| 209 | # ['port', 'PORT', 5], |
||
| 210 | ['hr_name', 'OS', 16], |
||
| 211 | ] |
||
| 212 | y = 2 |
||
| 213 | |||
| 214 | # Display table header |
||
| 215 | xc = x + 2 |
||
| 216 | for cpt, c in enumerate(column_def): |
||
| 217 | if xc < screen_x and y < screen_y and c[1] is not None: |
||
| 218 | self.term_window.addnstr(y, xc, |
||
| 219 | c[1], |
||
| 220 | screen_x - x, |
||
| 221 | self.colors_list['BOLD']) |
||
| 222 | xc += c[2] + self.space_between_column |
||
| 223 | y += 1 |
||
| 224 | |||
| 225 | # If a servers has been deleted from the list... |
||
| 226 | # ... and if the cursor is in the latest position |
||
| 227 | if self.cursor > len(servers_list) - 1: |
||
| 228 | # Set the cursor position to the latest item |
||
| 229 | self.cursor = len(servers_list) - 1 |
||
| 230 | |||
| 231 | # Display table |
||
| 232 | line = 0 |
||
| 233 | for v in servers_list: |
||
| 234 | # Get server stats |
||
| 235 | server_stat = {} |
||
| 236 | for c in column_def: |
||
| 237 | try: |
||
| 238 | server_stat[c[0]] = v[c[0]] |
||
| 239 | except KeyError as e: |
||
| 240 | logger.debug( |
||
| 241 | "Cannot grab stats {0} from server (KeyError: {1})".format(c[0], e)) |
||
| 242 | server_stat[c[0]] = '?' |
||
| 243 | # Display alias instead of name |
||
| 244 | try: |
||
| 245 | if c[0] == 'alias' and v[c[0]] is not None: |
||
| 246 | server_stat['name'] = v[c[0]] |
||
| 247 | except KeyError: |
||
| 248 | pass |
||
| 249 | |||
| 250 | # Display line for server stats |
||
| 251 | cpt = 0 |
||
| 252 | xc = x |
||
| 253 | |||
| 254 | # Is the line selected ? |
||
| 255 | if line == self.cursor: |
||
| 256 | # Display cursor |
||
| 257 | self.term_window.addnstr( |
||
| 258 | y, xc, ">", screen_x - xc, self.colors_list['BOLD']) |
||
| 259 | |||
| 260 | # Display the line |
||
| 261 | xc += 2 |
||
| 262 | for c in column_def: |
||
| 263 | if xc < screen_x and y < screen_y and c[1] is not None: |
||
| 264 | # Display server stats |
||
| 265 | self.term_window.addnstr( |
||
| 266 | y, xc, format(server_stat[c[0]]), c[2], self.colors_list[v['status']]) |
||
| 267 | xc += c[2] + self.space_between_column |
||
| 268 | cpt += 1 |
||
| 269 | # Next line, next server... |
||
| 270 | y += 1 |
||
| 271 | line += 1 |
||
| 272 | |||
| 273 | return True |
||
| 274 |