| Conditions | 8 |
| Total Lines | 91 |
| Code Lines | 65 |
| 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:
| 1 | # -*- coding: utf-8 -*- |
||
| 140 | def msg_curse(self, args=None, max_width=None): |
||
| 141 | """Return the list to display in the curse interface.""" |
||
| 142 | # Init the return message |
||
| 143 | ret = [] |
||
| 144 | |||
| 145 | # Build the header message |
||
| 146 | ret.append(self.curse_add_line(self.view_data['version'], 'TITLE')) |
||
| 147 | ret.append(self.curse_add_line(self.view_data['psutil_version'])) |
||
| 148 | ret.append(self.curse_new_line()) |
||
| 149 | |||
| 150 | # Build the configuration file path |
||
| 151 | if 'configuration_file' in self.view_data: |
||
| 152 | ret.append(self.curse_add_line(self.view_data['configuration_file'])) |
||
| 153 | ret.append(self.curse_new_line()) |
||
| 154 | |||
| 155 | ret.append(self.curse_new_line()) |
||
| 156 | |||
| 157 | # key-shortcuts |
||
| 158 | # |
||
| 159 | # Collect all values after the 1st key-msg |
||
| 160 | # in a list of curse-lines. |
||
| 161 | # |
||
| 162 | shortcuts = [] |
||
| 163 | collecting = False |
||
| 164 | for k, v in iteritems(self.view_data): |
||
| 165 | if collecting: |
||
| 166 | pass |
||
| 167 | elif k == 'header_sort': |
||
| 168 | collecting = True |
||
| 169 | else: |
||
| 170 | continue |
||
| 171 | shortcuts.append(self.curse_add_line(v)) |
||
| 172 | # Divide shortcuts into 2 columns |
||
| 173 | # and if number of schortcuts is even, |
||
| 174 | # make the 1st column taller (len+1). |
||
| 175 | # |
||
| 176 | nlines = (len(shortcuts) + 1) // 2 |
||
| 177 | ret.extend( |
||
| 178 | msg |
||
| 179 | for triplet in zip( |
||
| 180 | iter(shortcuts[:nlines]), |
||
| 181 | chain(shortcuts[nlines:], iter(lambda: self.curse_add_line(''), None)), |
||
| 182 | iter(self.curse_new_line, None), |
||
| 183 | ) |
||
| 184 | for msg in triplet |
||
| 185 | ) |
||
| 186 | |||
| 187 | ret.append(self.curse_new_line()) |
||
| 188 | ret.append(self.curse_add_line('For an exhaustive list of key bindings:')) |
||
| 189 | ret.append(self.curse_new_line()) |
||
| 190 | ret.append(self.curse_add_line('https://glances.readthedocs.io/en/latest/cmds.html#interactive-commands')) |
||
| 191 | ret.append(self.curse_new_line()) |
||
| 192 | |||
| 193 | ret.append(self.curse_new_line()) |
||
| 194 | ret.append(self.curse_add_line('Colors binding:')) |
||
| 195 | ret.append(self.curse_new_line()) |
||
| 196 | for c in [ |
||
| 197 | 'DEFAULT', |
||
| 198 | 'UNDERLINE', |
||
| 199 | 'BOLD', |
||
| 200 | 'SORT', |
||
| 201 | 'OK', |
||
| 202 | 'MAX', |
||
| 203 | 'FILTER', |
||
| 204 | 'TITLE', |
||
| 205 | 'PROCESS', |
||
| 206 | 'PROCESS_SELECTED', |
||
| 207 | 'STATUS', |
||
| 208 | 'NICE', |
||
| 209 | 'CPU_TIME', |
||
| 210 | 'CAREFUL', |
||
| 211 | 'WARNING', |
||
| 212 | 'CRITICAL', |
||
| 213 | 'OK_LOG', |
||
| 214 | 'CAREFUL_LOG', |
||
| 215 | 'WARNING_LOG', |
||
| 216 | 'CRITICAL_LOG', |
||
| 217 | 'PASSWORD', |
||
| 218 | 'SELECTED', |
||
| 219 | 'INFO', |
||
| 220 | 'ERROR', |
||
| 221 | 'SEPARATOR', |
||
| 222 | ]: |
||
| 223 | ret.append(self.curse_add_line(c, decoration=c)) |
||
| 224 | if c == 'CPU_TIME': |
||
| 225 | ret.append(self.curse_new_line()) |
||
| 226 | else: |
||
| 227 | ret.append(self.curse_add_line(' ')) |
||
| 228 | |||
| 229 | # Return the message with decoration |
||
| 230 | return ret |
||
| 231 |