@@ -52,6 +52,43 @@ class Toplist {
52
52
}
53
53
54
54
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
+
55
92
function updateProcessList ( ) {
56
93
window . si . processes ( ) . then ( data => {
57
94
if ( window . settings . excludeThreadsFromToplist === true ) {
@@ -68,16 +105,64 @@ class Toplist {
68
105
} ) ;
69
106
}
70
107
108
+ data . list . forEach ( proc => {
109
+ proc . runtime = new Date ( Date . now ( ) - Date . parse ( proc . started ) ) ;
110
+ } ) ;
111
+
71
112
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
+ } ) ;
74
160
75
161
document . querySelectorAll ( "#processList > tr" ) . forEach ( el => {
76
162
el . remove ( ) ;
77
163
} ) ;
78
164
79
165
list . forEach ( proc => {
80
- let runtime = new Date ( Date . now ( ) - Date . parse ( proc . started ) ) ;
81
166
let el = document . createElement ( "tr" ) ;
82
167
el . innerHTML = `<td class="pid">${ proc . pid } </td>
83
168
<td class="name">${ proc . name } </td>
@@ -86,7 +171,7 @@ class Toplist {
86
171
<td class="mem">${ Math . round ( proc . pmem * 10 ) / 10 } %</td>
87
172
<td class="state">${ proc . state } </td>
88
173
<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>` ;
90
175
document . getElementById ( "processList" ) . append ( el ) ;
91
176
} ) ;
92
177
} ) ;
@@ -113,13 +198,28 @@ class Toplist {
113
198
</thead>
114
199
<tbody id=\"processList\">
115
200
</tbody>
116
- </table>` ,
201
+ </table>` ,
117
202
}
118
203
) ;
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
+
119
219
updateProcessList ( ) ;
120
220
window . keyboard . attach ( ) ;
121
221
window . term [ window . currentTerm ] . term . focus ( ) ;
122
- setInterval ( updateProcessList , 2000 ) ;
222
+ setInterval ( updateProcessList , 1000 ) ;
123
223
}
124
224
}
125
225
0 commit comments