Skip to content

Commit 1b3fdf7

Browse files
committed
top: fix panic caused by picker
1 parent dbad086 commit 1b3fdf7

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

src/uu/top/src/picker.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,20 @@ fn command(pid: u32) -> String {
250250
let result: String = trimmed.into();
251251

252252
if cfg!(target_os = "linux") && result.is_empty() {
253-
{
254-
match PathBuf::from_str(&format!("/proc/{pid}/status")) {
255-
Ok(path) => {
256-
let file = File::open(path).unwrap();
257-
let content = read_to_string(file).unwrap();
258-
let line = content
259-
.lines()
260-
.collect::<Vec<_>>()
261-
.first()
262-
.unwrap()
263-
.split(':')
264-
.collect::<Vec<_>>();
265-
266-
line[1].trim().to_owned()
267-
}
268-
Err(_) => String::new(),
269-
}
253+
let path = PathBuf::from_str(&format!("/proc/{pid}/status")).unwrap();
254+
if let Ok(file) = File::open(path) {
255+
let content = read_to_string(file).unwrap();
256+
let line = content
257+
.lines()
258+
.collect::<Vec<_>>()
259+
.first()
260+
.unwrap()
261+
.split(':')
262+
.collect::<Vec<_>>();
263+
264+
line[1].trim().to_owned()
265+
} else {
266+
String::new()
270267
}
271268
} else {
272269
result

src/uu/top/src/top.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
105105
Settings { filter, ..settings }
106106
};
107107

108-
let settings = Arc::new(RwLock::new(settings));
108+
let settings = Arc::new(settings);
109109
let tui_stat = Arc::new(RwLock::new(TuiStat::new()));
110110
let should_update = Arc::new(AtomicBool::new(true));
111111
let data = Arc::new(RwLock::new((
112112
Header::new(&tui_stat.read().unwrap()),
113-
ProcList::new(&settings.read().unwrap()),
113+
ProcList::new(&settings),
114114
)));
115115

116116
// update
117117
{
118118
let should_update = should_update.clone();
119119
let tui_stat = tui_stat.clone();
120-
let settings = settings.clone();
121120
let data = data.clone();
121+
let settings = settings.clone();
122122
thread::spawn(move || loop {
123123
let delay = { tui_stat.read().unwrap().delay };
124124
sleep(delay);
125125
{
126126
let header = Header::new(&tui_stat.read().unwrap());
127-
let proc_list = ProcList::new(&settings.read().unwrap());
127+
let proc_list = ProcList::new(&settings);
128128
*data.write().unwrap() = (header, proc_list);
129129
should_update.store(true, Ordering::Relaxed);
130130
}
@@ -134,7 +134,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
134134
let mut terminal = ratatui::init();
135135
terminal.draw(|frame| {
136136
Tui::new(
137-
&settings.read().unwrap(),
137+
&settings,
138138
&data.read().unwrap(),
139139
&mut tui_stat.write().unwrap(),
140140
)
@@ -209,7 +209,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
209209
if should_update.load(Ordering::Relaxed) {
210210
terminal.draw(|frame| {
211211
Tui::new(
212-
&settings.read().unwrap(),
212+
&settings,
213213
&data.read().unwrap(),
214214
&mut tui_stat.write().unwrap(),
215215
)

tests/by-util/test_top.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fn test_conflict_arg() {
1717
new_ucmd!().arg("-p=0").arg("-U=0").fails().code_is(1);
1818
}
1919

20+
// // The tests below are disabled because they are not for the TUI mode, which is the default
21+
// // TODO: make them work in TUI mode
2022
// #[test]
2123
// fn test_flag_user() {
2224
// let check = |output: &str| {

0 commit comments

Comments
 (0)