@@ -20,29 +20,69 @@ const nameFilter = (input, proc) => {
2020 return proc . name . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) ;
2121} ;
2222
23+ const preferNotMatching = matches => ( a , b ) => {
24+ const aMatches = matches ( a ) ;
25+ return matches ( b ) === aMatches ? 0 : ( aMatches ? 1 : - 1 ) ;
26+ } ;
27+
28+ const deprioritizedProcesses = new Set ( [ 'iTerm' , 'iTerm2' , 'fkill' ] ) ;
29+ const isDeprioritizedProcess = proc => deprioritizedProcesses . has ( proc . name ) ;
30+ const preferNotDeprioritized = preferNotMatching ( isDeprioritizedProcess ) ;
31+ const preferHighPerformanceImpact = ( a , b ) => numSort . desc ( a . cpu + a . memory , b . cpu + b . memory ) ;
32+ const preferLowAlphanumericNames = ( a , b ) => a . name . localeCompare ( b . name ) ;
33+
34+ const preferHeurisicallyInterestingProcesses = ( a , b ) => {
35+ let result ;
36+
37+ result = preferNotDeprioritized ( a , b ) ;
38+ if ( result !== 0 ) {
39+ return result ;
40+ }
41+
42+ result = preferHighPerformanceImpact ( a , b ) ;
43+ if ( result !== 0 ) {
44+ return result ;
45+ }
46+
47+ return preferLowAlphanumericNames ( a , b ) ;
48+ } ;
49+
2350const filterProcesses = ( input , processes , flags ) => {
2451 const filters = {
2552 name : proc => input ? nameFilter ( input , proc ) : true ,
2653 verbose : proc => input ? ( process . platform === 'win32' ? proc . name : proc . cmd ) . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) : true
2754 } ;
2855
56+ const memoryThreshold = flags . verbose ? 0.0 : 1.0 ;
57+ const cpuThreshold = flags . verbose ? 0.0 : 3.0 ;
58+
2959 return processes
3060 . filter ( proc => ! (
3161 proc . name . endsWith ( '-helper' ) ||
3262 proc . name . endsWith ( 'Helper' ) ||
3363 proc . name . endsWith ( 'HelperApp' )
3464 ) )
3565 . filter ( flags . verbose ? filters . verbose : filters . name )
36- . sort ( ( a , b ) => numSort . asc ( a . pid , b . pid ) )
66+ . sort ( preferHeurisicallyInterestingProcesses )
3767 . map ( proc => {
68+ const renderPercentage = percents => {
69+ const digits = Math . floor ( percents * 10 ) . toString ( ) . padStart ( 2 , '0' ) ;
70+ const whole = digits . substr ( 0 , digits . length - 1 ) ;
71+ const fraction = digits . substr ( digits . length - 1 ) ;
72+ return fraction === '0' ? `${ whole } %` : `${ whole } .${ fraction } %` ;
73+ } ;
74+
3875 const lineLength = process . stdout . columns || 80 ;
39- const ports = proc . ports . slice ( 0 , 4 ) . map ( x => `:${ x } ` ) . join ( ' ' ) . trim ( ) ;
40- const margins = commandLineMargins + proc . pid . toString ( ) . length + ports . length ;
76+ const ports = proc . ports . length === 0 ? '' : ( ' ' + proc . ports . slice ( 0 , 4 ) . map ( x => `:${ x } ` ) . join ( ' ' ) ) ;
77+ const memory = ( proc . memory !== undefined && ( proc . memory > memoryThreshold ) ) ? ` 🐏${ renderPercentage ( proc . memory ) } ` : '' ;
78+ const cpu = ( proc . cpu !== undefined && ( proc . cpu > cpuThreshold ) ) ? `🚦${ renderPercentage ( proc . cpu ) } ` : '' ;
79+ const margins = commandLineMargins + proc . pid . toString ( ) . length + ports . length + memory . length + cpu . length ;
4180 const length = lineLength - margins ;
4281 const name = cliTruncate ( flags . verbose && process . platform !== 'win32' ? proc . cmd : proc . name , length , { position : 'middle' } ) ;
82+ const spacer = lineLength === process . stdout . columns ? '' . padEnd ( length - name . length ) : '' ;
4383
4484 return {
45- name : `${ name } ${ chalk . dim ( proc . pid ) } ${ chalk . dim . magenta ( ports ) } ` ,
85+ name : `${ name } ${ chalk . dim ( proc . pid ) } ${ spacer } ${ chalk . dim ( ports ) } ${ cpu } ${ memory } ` ,
4686 value : proc . pid
4787 } ;
4888 } ) ;
@@ -107,6 +147,7 @@ const init = async flags => {
107147 pidFromPort . list ( ) ,
108148 psList ( { all : false } )
109149 ] ) ;
150+
110151 const procs = processes . map ( proc => ( { ...proc , ports : getPortsFromPid ( proc . pid , pids ) } ) ) ;
111152 listProcesses ( procs , flags ) ;
112153} ;
0 commit comments