Split boilerplate and platform-independent field comparison

This acheives two things:
- Allows for simple tie-breaking if values compare equal (needed to make sorting the tree-view stable)
- Allows for platform-dependent overriding of the sort-order for specific fields

Also fixes a small oversight on DragonFlyBSD when default-sorting.
This commit is contained in:
Benny Baumann 2020-12-18 22:12:26 +01:00 committed by BenBE
parent 2327260ee8
commit 77db240b48
8 changed files with 23 additions and 10 deletions

View File

@ -495,7 +495,6 @@ long Process_pidCompare(const void* v1, const void* v2) {
long Process_compare(const void* v1, const void* v2) {
const Process *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
int r;
if (Settings_getActiveDirection(settings) == 1) {
p1 = (const Process*)v1;
@ -507,7 +506,19 @@ long Process_compare(const void* v1, const void* v2) {
ProcessField key = Settings_getActiveSortKey(settings);
switch (key) {
long result = Process_compareByKey(p1, p2, key);
// Implement tie-breaker (needed to make tree mode more stable)
if (!result)
result = SPACESHIP_NUMBER(p1->pid, p2->pid);
return result;
}
long Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key) {
int r;
switch ((int) key) {
case PERCENT_CPU:
case PERCENT_NORM_CPU:
return SPACESHIP_NUMBER(p2->percent_cpu, p1->percent_cpu);
@ -557,6 +568,6 @@ long Process_compare(const void* v1, const void* v2) {
case USER:
return SPACESHIP_NULLSTR(p1->user, p2->user);
default:
return Process_compareByKey(p1, p2, key);
return SPACESHIP_NUMBER(p1->pid, p2->pid);
}
}

View File

@ -142,7 +142,7 @@ typedef struct ProcessClass_ {
#define As_Process(this_) ((const ProcessClass*)((this_)->super.klass))
#define Process_getCommand(this_) (As_Process(this_)->getCommandStr ? As_Process(this_)->getCommandStr((const Process*)(this_)) : ((const Process*)(this_))->comm)
#define Process_compareByKey(p1_, p2_, key_) (As_Process(p1_)->compareByKey ? (As_Process(p1_)->compareByKey(p1_, p2_, key_)) : SPACESHIP_NUMBER(p1->pid, p2->pid))
#define Process_compareByKey(p1_, p2_, key_) (As_Process(p1_)->compareByKey ? (As_Process(p1_)->compareByKey(p1_, p2_, key_)) : Process_compareByKey_Base(p1_, p2_, key_))
static inline pid_t Process_getParentPid(const Process* this) {
return this->tgid == this->pid ? this->ppid : this->tgid;
@ -199,4 +199,6 @@ bool Process_sendSignal(Process* this, Arg sgn);
long Process_pidCompare(const void* v1, const void* v2);
long Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField key);
#endif

View File

@ -92,7 +92,7 @@ static long DarwinProcess_compareByKey(const Process* v1, const Process* v2, Pro
case TRANSLATED:
return SPACESHIP_NUMBER(p1->translated, p2->translated);
default:
return SPACESHIP_NUMBER(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}

View File

@ -120,7 +120,7 @@ long DragonFlyBSDProcess_compareByKey(const Process* v1, const Process* v2, Proc
case JAIL:
return SPACESHIP_NULLSTR(p1->jname, p2->jname);
default:
return SPACESHIP_NULLSTR(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}

View File

@ -121,7 +121,7 @@ static long FreeBSDProcess_compareByKey(const Process* v1, const Process* v2, Pr
case TTY_NR:
return SPACESHIP_NULLSTR(p1->ttyPath, p2->ttyPath);
default:
return SPACESHIP_NUMBER(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}

View File

@ -857,7 +857,7 @@ static long LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proc
case CWD:
return SPACESHIP_NULLSTR(p1->cwd, p2->cwd);
default:
return SPACESHIP_NUMBER(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}

View File

@ -222,7 +222,7 @@ static long OpenBSDProcess_compareByKey(const Process* v1, const Process* v2, Pr
switch ((int) key) {
// add OpenBSD-specific fields here
default:
return SPACESHIP_NUMBER(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}

View File

@ -142,7 +142,7 @@ long SolarisProcess_compareByKey(const void* v1, const void* v2, ProcessField ke
case LWPID:
return SPACESHIP_NUMBER(p1->lwpid, p2->lwpid);
default:
return SPACESHIP_NUMBER(v1->pid, v2->pid);
return Process_compareByKey_Base(v1, v2, key);
}
}