FR: Sort Sensors my name in proper number order #3132

This commit is contained in:
nicolargo
2025-04-06 18:37:45 +02:00
parent c5954c9d99
commit fcbcbbfb4d
2 changed files with 11 additions and 2 deletions

View File

@@ -508,3 +508,12 @@ def replace_special_chars(input_string, by=' '):
"""Replace some special char by another in the input_string
Return: the string with the chars replaced"""
return input_string.replace('\r\n', by).replace('\n', by).replace('\t', by)
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
"""Return a text in a natural/human readable format."""
return [atoi(c) for c in re.split(r'(\d+)', text)]

View File

@@ -14,7 +14,7 @@ from typing import Any
import psutil
from glances.globals import to_fahrenheit
from glances.globals import natural_keys, to_fahrenheit
from glances.logger import logger
from glances.outputs.glances_unicode import unicode_message
from glances.plugins.plugin.model import GlancesPluginModel
@@ -144,7 +144,7 @@ class PluginModel(GlancesPluginModel):
# Remove duplicates thanks to https://stackoverflow.com/a/9427216/1919431
stats_transformed = [dict(t) for t in {tuple(d.items()) for d in stats_transformed}]
# Sort by label
return sorted(stats_transformed, key=lambda d: d['label'])
return sorted(stats_transformed, key=lambda d: natural_keys(d['label']))
@GlancesPluginModel._check_decorator
@GlancesPluginModel._log_result_decorator