Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit a329b36

Browse files
authored
Improvements on process list modal (#925)
1 parent 70dbb3f commit a329b36

File tree

2 files changed

+110
-6
lines changed

2 files changed

+110
-6
lines changed

src/assets/css/mod_processlist.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ table#processContainer td.header {
1111
text-align: center;
1212
}
1313

14+
table#processContainer td.header:hover {
15+
cursor: pointer;
16+
}
17+
1418
table#processContainer td.pid {
1519
width: 5vw;
1620
}

src/classes/toplist.class.js

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,43 @@ class Toplist {
5252
}
5353

5454
processList(){
55+
let sortKey;
56+
let ascending = false;
57+
58+
function setSortKey(fieldName){
59+
if (sortKey === fieldName){
60+
if (ascending){
61+
sortKey = undefined;
62+
ascending = false;
63+
}
64+
else{
65+
ascending = true;
66+
}
67+
}
68+
else {
69+
sortKey = fieldName;
70+
ascending = false;
71+
}
72+
}
73+
74+
function formatRuntime(ms){
75+
const msInDay = 24 * 60 * 60 * 1000;
76+
let days = Math.floor(ms / msInDay);
77+
let remainingMS = ms % msInDay;
78+
79+
const msInHour = 60 * 60 * 1000;
80+
let hours = Math.floor(remainingMS / msInHour);
81+
remainingMS = ms % msInHour;
82+
83+
let msInMin = 60 * 1000;
84+
let minutes = Math.floor(remainingMS / msInMin);
85+
remainingMS = ms % msInMin;
86+
87+
let seconds = Math.floor(remainingMS / 1000);
88+
89+
return `${days < 10 ? "0" : ""}${days}:${hours < 10 ? "0" : ""}${hours}:${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
90+
}
91+
5592
function updateProcessList(){
5693
window.si.processes().then(data => {
5794
if (window.settings.excludeThreadsFromToplist === true) {
@@ -68,16 +105,64 @@ class Toplist {
68105
});
69106
}
70107

108+
data.list.forEach(proc => {
109+
proc.runtime = new Date(Date.now() - Date.parse(proc.started));
110+
});
111+
71112
let list = data.list.sort((a, b) => {
72-
return ((b.pcpu-a.pcpu)*100 + b.pmem-a.pmem);
73-
});//.splice(0, 30);
113+
switch (sortKey) {
114+
case "PID":
115+
if (ascending) return a.pid - b.pid;
116+
else return b.pid - a.pid;
117+
case "Name":
118+
if (ascending){
119+
if (a.name > b.name) return -1;
120+
if (a.name < b.name) return 1;
121+
return 0;
122+
}
123+
else {
124+
if (a.name < b.name) return -1;
125+
if (a.name > b.name) return 1;
126+
return 0;
127+
}
128+
case "User":
129+
if (ascending){
130+
if (a.user > b.user) return -1;
131+
if (a.user < b.user) return 1;
132+
return 0;
133+
}
134+
else {
135+
if (a.user < b.user) return -1;
136+
if (a.user > b.user) return 1;
137+
return 0;
138+
}
139+
case "CPU":
140+
if (ascending) return a.pcpu - b.pcpu;
141+
else return b.pcpu - a.pcpu;
142+
case "Memory":
143+
if (ascending) return a.pmem - b.pmem;
144+
else return b.pmem - a.pmem;
145+
case "State":
146+
if (a.state < b.state) return -1;
147+
if (a.state > b.state) return 1;
148+
return 0;
149+
case "Started":
150+
if (ascending) return Date.parse(a.started) - Date.parse(b.started);
151+
else return Date.parse(b.started) - Date.parse(a.started);
152+
case "Runtime":
153+
if (ascending) return a.runtime - b.runtime;
154+
else return b.runtime - a.runtime;
155+
default:
156+
// default to the same sorting as the toplist
157+
return ((b.pcpu-a.pcpu)*100 + b.pmem-a.pmem);
158+
}
159+
});
74160

75161
document.querySelectorAll("#processList > tr").forEach(el => {
76162
el.remove();
77163
});
78164

79165
list.forEach(proc => {
80-
let runtime = new Date(Date.now() - Date.parse(proc.started));
81166
let el = document.createElement("tr");
82167
el.innerHTML = `<td class="pid">${proc.pid}</td>
83168
<td class="name">${proc.name}</td>
@@ -86,7 +171,7 @@ class Toplist {
86171
<td class="mem">${Math.round(proc.pmem*10)/10}%</td>
87172
<td class="state">${proc.state}</td>
88173
<td class="started">${proc.started}</td>
89-
<td class="runtime">${runtime.getHours()}:${runtime.getMinutes()}:${runtime.getSeconds()}</td>`;
174+
<td class="runtime">${formatRuntime(proc.runtime)}</td>`;
90175
document.getElementById("processList").append(el);
91176
});
92177
});
@@ -113,13 +198,28 @@ class Toplist {
113198
</thead>
114199
<tbody id=\"processList\">
115200
</tbody>
116-
</table>`,
201+
</table>`,
117202
}
118203
);
204+
205+
let headers = document.getElementsByClassName("header");
206+
for (let header of headers){
207+
let title = header.textContent;
208+
header.addEventListener("click", () => {
209+
for (let header of headers) {
210+
header.textContent = header.textContent.replace('\u25B2', "").replace('\u25BC', "");
211+
}
212+
setSortKey(title);
213+
if (sortKey){
214+
header.textContent = `${title}${ascending ? '\u25B2' : '\u25BC'}`;
215+
}
216+
});
217+
}
218+
119219
updateProcessList();
120220
window.keyboard.attach();
121221
window.term[window.currentTerm].term.focus();
122-
setInterval(updateProcessList, 2000);
222+
setInterval(updateProcessList, 1000);
123223
}
124224
}
125225

0 commit comments

Comments
 (0)