33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6+ use crate :: tui:: stat:: TuiStat ;
7+ use crate :: Settings ;
68use std:: {
79 ffi:: OsString ,
810 fs:: File ,
@@ -19,7 +21,10 @@ pub fn sysinfo() -> &'static RwLock<System> {
1921 SYSINFO . get_or_init ( || RwLock :: new ( System :: new_all ( ) ) )
2022}
2123
22- pub ( crate ) fn pickers ( fields : & [ String ] ) -> Vec < Box < dyn Fn ( u32 ) -> String > > {
24+ type Stat < ' a > = ( & ' a Settings , & ' a TuiStat ) ;
25+ type Picker = Box < dyn Fn ( u32 , Stat ) -> String > ;
26+
27+ pub ( crate ) fn pickers ( fields : & [ String ] ) -> Vec < Picker > {
2328 fields
2429 . iter ( )
2530 . map ( |field| match field. as_str ( ) {
@@ -41,7 +46,7 @@ pub(crate) fn pickers(fields: &[String]) -> Vec<Box<dyn Fn(u32) -> String>> {
4146}
4247
4348#[ inline]
44- fn helper ( f : impl Fn ( u32 ) -> String + ' static ) -> Box < dyn Fn ( u32 ) -> String > {
49+ fn helper ( f : impl Fn ( u32 , Stat ) -> String + ' static ) -> Picker {
4550 Box :: new ( f)
4651}
4752
@@ -55,11 +60,11 @@ fn format_memory(memory_b: u64) -> String {
5560 }
5661}
5762
58- fn todo ( _pid : u32 ) -> String {
63+ fn todo ( _pid : u32 , _stat : Stat ) -> String {
5964 "TODO" . into ( )
6065}
6166
62- fn cpu ( pid : u32 ) -> String {
67+ fn cpu ( pid : u32 , _stat : Stat ) -> String {
6368 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
6469 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
6570 return "0.0" . into ( ) ;
@@ -70,11 +75,11 @@ fn cpu(pid: u32) -> String {
7075 format ! ( "{usage:.2}" )
7176}
7277
73- fn pid ( pid : u32 ) -> String {
78+ fn pid ( pid : u32 , _stat : Stat ) -> String {
7479 pid. to_string ( )
7580}
7681
77- fn user ( pid : u32 ) -> String {
82+ fn user ( pid : u32 , _stat : Stat ) -> String {
7883 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
7984 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
8085 return "0.0" . into ( ) ;
@@ -89,7 +94,7 @@ fn user(pid: u32) -> String {
8994}
9095
9196#[ cfg( target_os = "linux" ) ]
92- fn pr ( pid : u32 ) -> String {
97+ fn pr ( pid : u32 , _stat : Stat ) -> String {
9398 use uucore:: libc:: * ;
9499 let policy = unsafe { sched_getscheduler ( pid as i32 ) } ;
95100 if policy == -1 {
@@ -111,8 +116,8 @@ fn pr(pid: u32) -> String {
111116}
112117
113118#[ cfg( not( target_os = "linux" ) ) ]
114- fn pr ( pid : u32 ) -> String {
115- todo ( pid)
119+ fn pr ( pid : u32 , stat : Stat ) -> String {
120+ todo ( pid, stat )
116121}
117122
118123#[ cfg( not( target_os = "windows" ) ) ]
@@ -134,18 +139,18 @@ fn get_nice(pid: u32) -> i32 {
134139}
135140
136141#[ cfg( not( target_os = "windows" ) ) ]
137- fn ni ( pid : u32 ) -> String {
142+ fn ni ( pid : u32 , _stat : Stat ) -> String {
138143 format ! ( "{}" , get_nice( pid) )
139144}
140145
141146// TODO: Implement this function for Windows
142147#[ cfg( target_os = "windows" ) ]
143- fn ni ( _pid : u32 ) -> String {
148+ fn ni ( _pid : u32 , _stat : Stat ) -> String {
144149 "0" . into ( )
145150}
146151
147152#[ cfg( target_os = "linux" ) ]
148- fn virt ( pid : u32 ) -> String {
153+ fn virt ( pid : u32 , _stat : Stat ) -> String {
149154 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
150155 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
151156 return "0.0" . into ( ) ;
@@ -154,12 +159,12 @@ fn virt(pid: u32) -> String {
154159}
155160
156161#[ cfg( not( target_os = "linux" ) ) ]
157- fn virt ( _pid : u32 ) -> String {
158- "TODO" . into ( )
162+ fn virt ( pid : u32 , stat : Stat ) -> String {
163+ todo ( pid , stat )
159164}
160165
161166#[ cfg( target_os = "linux" ) ]
162- fn res ( pid : u32 ) -> String {
167+ fn res ( pid : u32 , _stat : Stat ) -> String {
163168 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
164169 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
165170 return "0.0" . into ( ) ;
@@ -168,12 +173,12 @@ fn res(pid: u32) -> String {
168173}
169174
170175#[ cfg( not( target_os = "linux" ) ) ]
171- fn res ( _pid : u32 ) -> String {
172- "TODO" . into ( )
176+ fn res ( pid : u32 , stat : Stat ) -> String {
177+ todo ( pid , stat )
173178}
174179
175180#[ cfg( target_os = "linux" ) ]
176- fn shr ( pid : u32 ) -> String {
181+ fn shr ( pid : u32 , _stat : Stat ) -> String {
177182 let file_path = format ! ( "/proc/{pid}/statm" ) ;
178183 let Ok ( file) = File :: open ( file_path) else {
179184 return "0.0" . into ( ) ;
@@ -189,11 +194,11 @@ fn shr(pid: u32) -> String {
189194}
190195
191196#[ cfg( not( target_os = "linux" ) ) ]
192- fn shr ( _pid : u32 ) -> String {
193- "TODO" . into ( )
197+ fn shr ( pid : u32 , stat : Stat ) -> String {
198+ todo ( pid , stat )
194199}
195200
196- fn s ( pid : u32 ) -> String {
201+ fn s ( pid : u32 , _stat : Stat ) -> String {
197202 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
198203 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
199204 return "?" . into ( ) ;
@@ -208,7 +213,7 @@ fn s(pid: u32) -> String {
208213 . to_string ( )
209214}
210215
211- fn time_plus ( pid : u32 ) -> String {
216+ fn time_plus ( pid : u32 , _stat : Stat ) -> String {
212217 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
213218 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
214219 return "0:00.00" . into ( ) ;
@@ -226,7 +231,7 @@ fn time_plus(pid: u32) -> String {
226231 format ! ( "{hour}:{min:0>2}.{sec:0>2}" )
227232}
228233
229- fn mem ( pid : u32 ) -> String {
234+ fn mem ( pid : u32 , _stat : Stat ) -> String {
230235 let binding = sysinfo ( ) . read ( ) . unwrap ( ) ;
231236 let Some ( proc) = binding. process ( Pid :: from_u32 ( pid) ) else {
232237 return "0.0" . into ( ) ;
@@ -238,7 +243,8 @@ fn mem(pid: u32) -> String {
238243 )
239244}
240245
241- fn command ( pid : u32 ) -> String {
246+ fn command ( pid : u32 , stat : Stat ) -> String {
247+ let full_command_line = stat. 1 . full_command_line ;
242248 let f = |cmd : & [ OsString ] | -> String {
243249 let binding = cmd
244250 . iter ( )
@@ -250,6 +256,7 @@ fn command(pid: u32) -> String {
250256 let result: String = trimmed. into ( ) ;
251257
252258 if cfg ! ( target_os = "linux" ) && result. is_empty ( ) {
259+ // actually executable name
253260 let path = PathBuf :: from_str ( & format ! ( "/proc/{pid}/status" ) ) . unwrap ( ) ;
254261 if let Ok ( file) = File :: open ( path) {
255262 let content = read_to_string ( file) . unwrap ( ) ;
@@ -276,8 +283,17 @@ fn command(pid: u32) -> String {
276283 } ;
277284
278285 proc. exe ( )
279- . and_then ( |it| it. iter ( ) . next_back ( ) )
280- . map ( |it| it. to_str ( ) . unwrap ( ) )
281- . unwrap_or ( & f ( proc. cmd ( ) ) )
282- . into ( )
286+ . and_then ( |it| {
287+ if full_command_line {
288+ it. iter ( ) . next_back ( )
289+ } else {
290+ it. file_name ( )
291+ }
292+ } )
293+ . map ( |it| it. to_str ( ) . unwrap ( ) . to_string ( ) )
294+ . unwrap_or ( if full_command_line {
295+ f ( proc. cmd ( ) )
296+ } else {
297+ proc. name ( ) . to_str ( ) . unwrap ( ) . to_string ( )
298+ } )
283299}
0 commit comments