Skip to content

Commit 842fbb5

Browse files
committed
top: tui c (full command line)
1 parent 9b86686 commit 842fbb5

File tree

5 files changed

+102
-71
lines changed

5 files changed

+102
-71
lines changed

src/uu/top/src/picker.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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;
68
use 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
}

src/uu/top/src/top.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ pub(crate) struct ProcList {
5959
}
6060

6161
impl ProcList {
62-
pub fn new(settings: &Settings) -> Self {
62+
pub fn new(settings: &Settings, tui_stat: &TuiStat) -> Self {
6363
let fields = selected_fields();
64-
let collected = collect(settings, &fields);
64+
let collected = collect(settings, &fields, tui_stat);
6565

6666
Self { fields, collected }
6767
}
@@ -109,7 +109,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
109109
let should_update = Arc::new(AtomicBool::new(true));
110110
let data = Arc::new(RwLock::new((
111111
Header::new(&tui_stat.read().unwrap()),
112-
ProcList::new(&settings),
112+
ProcList::new(&settings, &tui_stat.read().unwrap()),
113113
)));
114114

115115
// update
@@ -123,7 +123,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
123123
sleep(delay);
124124
{
125125
let header = Header::new(&tui_stat.read().unwrap());
126-
let proc_list = ProcList::new(&settings);
126+
let proc_list = ProcList::new(&settings, &tui_stat.read().unwrap());
127127
tui_stat.write().unwrap().input_error = None;
128128
*data.write().unwrap() = (header, proc_list);
129129
should_update.store(true, Ordering::Relaxed);
@@ -143,7 +143,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
143143
loop {
144144
if let Ok(true) = event::poll(Duration::from_millis(20)) {
145145
if let Ok(e) = event::read() {
146-
if handle_input(e, &tui_stat, &data, &should_update)? {
146+
if handle_input(e, &settings, &tui_stat, &data, &should_update) {
147147
break;
148148
}
149149
}
@@ -197,7 +197,7 @@ fn selected_fields() -> Vec<String> {
197197
.collect()
198198
}
199199

200-
fn collect(settings: &Settings, fields: &[String]) -> Vec<Vec<String>> {
200+
fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Vec<String>> {
201201
let pickers = pickers(fields);
202202

203203
let pids = sysinfo()
@@ -215,7 +215,7 @@ fn collect(settings: &Settings, fields: &[String]) -> Vec<Vec<String>> {
215215
.map(|it| {
216216
pickers
217217
.iter()
218-
.map(move |picker| picker(it))
218+
.map(move |picker| picker(it, (settings, tui_stat)))
219219
.collect::<Vec<_>>()
220220
})
221221
.collect()

src/uu/top/src/tui/color.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This file is part of the uutils procps package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
16
use ratatui::prelude::Stylize;
27
use ratatui::style::{Color, Styled};
38

0 commit comments

Comments
 (0)