Set correct default sorting direction

Respect the field option defaultSortDesc for the default screen sort
direction, e.g. for CPU%.
This commit is contained in:
Christian Göttsche 2021-12-16 16:09:32 +01:00 committed by cgzones
parent 1ef8c0e12f
commit 5b78ad2d53
3 changed files with 10 additions and 10 deletions

View File

@ -149,7 +149,7 @@ static void addNewScreen(Panel* super) {
ScreensPanel* const this = (ScreensPanel*) super; ScreensPanel* const this = (ScreensPanel*) super;
const char* name = "New"; const char* name = "New";
ScreenSettings* ss = Settings_newScreen(this->settings, name, "PID Command"); ScreenSettings* ss = Settings_newScreen(this->settings, &(const ScreenDefaults){ .name = name, .columns = "PID Command", .sortKey = "PID" });
ScreenListItem* item = ScreenListItem_new(name, ss); ScreenListItem* item = ScreenListItem_new(name, ss);
int idx = Panel_getSelectedIndex(super); int idx = Panel_getSelectedIndex(super);
Panel_insert(super, idx + 1, (Object*) item); Panel_insert(super, idx + 1, (Object*) item);

View File

@ -262,22 +262,23 @@ static void ScreenSettings_readFields(ScreenSettings* ss, Hashtable* columns, co
String_freeArray(ids); String_freeArray(ids);
} }
ScreenSettings* Settings_newScreen(Settings* this, const char* name, const char* line) { ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults) {
int sortKey = defaults->sortKey ? toFieldIndex(this->dynamicColumns, defaults->sortKey) : PID;
ScreenSettings* ss = xMalloc(sizeof(ScreenSettings)); ScreenSettings* ss = xMalloc(sizeof(ScreenSettings));
*ss = (ScreenSettings) { *ss = (ScreenSettings) {
.name = xStrdup(name), .name = xStrdup(defaults->name),
.fields = xCalloc(LAST_PROCESSFIELD, sizeof(ProcessField)), .fields = xCalloc(LAST_PROCESSFIELD, sizeof(ProcessField)),
.flags = 0, .flags = 0,
.direction = 1, .direction = (Process_fields[sortKey].defaultSortDesc) ? -1 : 1,
.treeDirection = 1, .treeDirection = 1,
.sortKey = PID, .sortKey = sortKey,
.treeSortKey = PID, .treeSortKey = PID,
.treeView = false, .treeView = false,
.treeViewAlwaysByPID = false, .treeViewAlwaysByPID = false,
.allBranchesCollapsed = false, .allBranchesCollapsed = false,
}; };
ScreenSettings_readFields(ss, this->dynamicColumns, line); ScreenSettings_readFields(ss, this->dynamicColumns, defaults->columns);
this->screens[this->nScreens] = ss; this->screens[this->nScreens] = ss;
this->nScreens++; this->nScreens++;
this->screens = xRealloc(this->screens, sizeof(ScreenSettings*) * (this->nScreens + 1)); this->screens = xRealloc(this->screens, sizeof(ScreenSettings*) * (this->nScreens + 1));
@ -296,8 +297,7 @@ static ScreenSettings* Settings_defaultScreens(Settings* this) {
return this->screens[0]; return this->screens[0];
for (unsigned int i = 0; i < Platform_numberOfDefaultScreens; i++) { for (unsigned int i = 0; i < Platform_numberOfDefaultScreens; i++) {
const ScreenDefaults* defaults = &Platform_defaultScreens[i]; const ScreenDefaults* defaults = &Platform_defaultScreens[i];
ScreenSettings* settings = Settings_newScreen(this, defaults->name, defaults->columns); Settings_newScreen(this, defaults);
settings->sortKey = toFieldIndex(this->dynamicColumns, defaults->sortKey);
} }
return this->screens[0]; return this->screens[0];
} }
@ -466,7 +466,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
this->topologyAffinity = !!atoi(option[1]); this->topologyAffinity = !!atoi(option[1]);
#endif #endif
} else if (strncmp(option[0], "screen:", 7) == 0) { } else if (strncmp(option[0], "screen:", 7) == 0) {
screen = Settings_newScreen(this, option[0] + 7, option[1]); screen = Settings_newScreen(this, &(const ScreenDefaults){ .name = option[0] + 7, .columns = option[1] });
} else if (String_eq(option[0], ".sort_key")) { } else if (String_eq(option[0], ".sort_key")) {
if (screen) if (screen)
screen->sortKey = toFieldIndex(this->dynamicColumns, option[1]); screen->sortKey = toFieldIndex(this->dynamicColumns, option[1]);

View File

@ -116,7 +116,7 @@ int Settings_write(const Settings* this, bool onCrash);
Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns); Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns);
ScreenSettings* Settings_newScreen(Settings* this, const char* name, const char* line); ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults);
void ScreenSettings_delete(ScreenSettings* this); void ScreenSettings_delete(ScreenSettings* this);