Skip to content

Commit fcbcbbf

Browse files
committed
FR: Sort Sensors my name in proper number order #3132
1 parent c5954c9 commit fcbcbbf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

glances/globals.py

+9
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,12 @@ def replace_special_chars(input_string, by=' '):
508508
"""Replace some special char by another in the input_string
509509
Return: the string with the chars replaced"""
510510
return input_string.replace('\r\n', by).replace('\n', by).replace('\t', by)
511+
512+
513+
def atoi(text):
514+
return int(text) if text.isdigit() else text
515+
516+
517+
def natural_keys(text):
518+
"""Return a text in a natural/human readable format."""
519+
return [atoi(c) for c in re.split(r'(\d+)', text)]

glances/plugins/sensors/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import psutil
1616

17-
from glances.globals import to_fahrenheit
17+
from glances.globals import natural_keys, to_fahrenheit
1818
from glances.logger import logger
1919
from glances.outputs.glances_unicode import unicode_message
2020
from glances.plugins.plugin.model import GlancesPluginModel
@@ -144,7 +144,7 @@ def __transform_sensors(self, threads_stats):
144144
# Remove duplicates thanks to https://stackoverflow.com/a/9427216/1919431
145145
stats_transformed = [dict(t) for t in {tuple(d.items()) for d in stats_transformed}]
146146
# Sort by label
147-
return sorted(stats_transformed, key=lambda d: d['label'])
147+
return sorted(stats_transformed, key=lambda d: natural_keys(d['label']))
148148

149149
@GlancesPluginModel._check_decorator
150150
@GlancesPluginModel._log_result_decorator

0 commit comments

Comments
 (0)