Skip to content

Commit 5148eb5

Browse files
committed
top: tui impl info bar
1 parent 8c0bab5 commit 5148eb5

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/uu/top/src/top.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ impl ProcList {
6666
}
6767
}
6868

69+
pub(crate) struct InfoBar {
70+
pub title: String,
71+
pub content: String,
72+
}
73+
6974
#[uucore::main]
7075
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7176
let matches = uu_app().try_get_matches_from(args)?;
@@ -107,6 +112,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
107112
let data = Arc::new(RwLock::new((
108113
Header::new(&tui_stat.read().unwrap()),
109114
ProcList::new(&settings, &tui_stat.read().unwrap()),
115+
None,
110116
)));
111117

112118
// update
@@ -122,7 +128,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
122128
let header = Header::new(&tui_stat.read().unwrap());
123129
let proc_list = ProcList::new(&settings, &tui_stat.read().unwrap());
124130
tui_stat.write().unwrap().input_message = None;
125-
*data.write().unwrap() = (header, proc_list);
131+
let mut data = data.write().unwrap();
132+
data.0 = header;
133+
data.1 = proc_list;
126134
should_update.store(true, Ordering::Relaxed);
127135
}
128136
});
@@ -231,11 +239,7 @@ fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Ve
231239
}
232240
collected
233241
.into_iter()
234-
.map(|it| {
235-
it.into_iter()
236-
.map(|c| c.as_string(tui_stat))
237-
.collect()
238-
})
242+
.map(|it| it.into_iter().map(|c| c.as_string(tui_stat)).collect())
239243
.collect()
240244
}
241245

src/uu/top/src/tui/input.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::header::Header;
77
use crate::platform::get_numa_nodes;
88
use crate::tui::stat::{CpuValueMode, TuiStat};
99
use crate::Filter::{EUser, User};
10-
use crate::{selected_fields, try_into_uid, ProcList, Settings};
10+
use crate::{selected_fields, try_into_uid, InfoBar, ProcList, Settings};
1111
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
1212
use std::sync::atomic::{AtomicBool, Ordering};
1313
use std::sync::RwLock;
@@ -40,7 +40,7 @@ pub fn handle_input(
4040
e: Event,
4141
settings: &Settings,
4242
tui_stat: &RwLock<TuiStat>,
43-
data: &RwLock<(Header, ProcList)>,
43+
data: &RwLock<(Header, ProcList, Option<InfoBar>)>,
4444
should_update: &AtomicBool,
4545
) -> bool {
4646
let input_mode = { tui_stat.read().unwrap().input_mode };
@@ -338,7 +338,7 @@ fn handle_input_value(
338338
input_event: InputEvent,
339339
settings: &Settings,
340340
tui_stat: &RwLock<TuiStat>,
341-
data: &RwLock<(Header, ProcList)>,
341+
data: &RwLock<(Header, ProcList, Option<InfoBar>)>,
342342
should_update: &AtomicBool,
343343
) {
344344
match input_event {

src/uu/top/src/tui/mod.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::borrow::Cow;
1313
use crate::header::{format_memory, Header};
1414
use crate::tui::color::TuiColor;
1515
use crate::tui::stat::{CpuGraphMode, MemoryGraphMode, TuiStat};
16-
use crate::ProcList;
16+
use crate::{InfoBar, ProcList};
1717
use ratatui::prelude::*;
1818
use ratatui::widgets::{Cell, Paragraph, Row, Table, TableState};
1919
use std::cmp::min;
@@ -22,19 +22,21 @@ pub struct Tui<'a> {
2222
settings: &'a crate::Settings,
2323
header: &'a Header,
2424
proc_list: &'a ProcList,
25+
info_bar: &'a Option<InfoBar>,
2526
stat: &'a mut TuiStat,
2627
}
2728

2829
impl<'a> Tui<'a> {
2930
pub fn new(
3031
settings: &'a crate::Settings,
31-
data: &'a (Header, ProcList),
32+
data: &'a (Header, ProcList, Option<InfoBar>),
3233
stat: &'a mut TuiStat,
3334
) -> Self {
3435
Self {
3536
settings,
3637
header: &data.0,
3738
proc_list: &data.1,
39+
info_bar: &data.2,
3840
stat,
3941
}
4042
}
@@ -66,6 +68,19 @@ impl<'a> Tui<'a> {
6668
height
6769
}
6870

71+
fn calc_info_bar_height(&self, width: u16) -> u16 {
72+
if let Some(info_bar) = &self.info_bar {
73+
let lines: u16 = info_bar
74+
.content
75+
.lines()
76+
.map(|s| (s.len() as u16).div_ceil(width))
77+
.sum();
78+
lines + 1 // 1 for title
79+
} else {
80+
0
81+
}
82+
}
83+
6984
fn calc_list_coordinates(&self) -> (usize, usize) {
7085
let list_total = self.proc_list.collected.len();
7186
let list_offset = self.stat.list_offset;
@@ -506,6 +521,19 @@ impl<'a> Tui<'a> {
506521
let table = Table::new(rows, constraints.clone()).header(header);
507522
StatefulWidget::render(table, area, buf, &mut state);
508523
}
524+
525+
fn render_info_bar(&self, area: Rect, buf: &mut Buffer) {
526+
if let Some(info_bar) = self.info_bar.as_ref() {
527+
let constraints = [Constraint::Length(1), Constraint::Min(1)];
528+
let layout = Layout::new(Direction::Vertical, constraints).split(area);
529+
Line::from(Span::styled(
530+
format!("{:<width$}", &info_bar.title, width = area.width as usize),
531+
Style::default().bg_secondary(self.stat.colorful),
532+
))
533+
.render(layout[0], buf);
534+
Span::raw(&info_bar.content).render(layout[1], buf);
535+
}
536+
}
509537
}
510538

511539
impl Widget for Tui<'_> {
@@ -524,6 +552,7 @@ impl Widget for Tui<'_> {
524552
Constraint::Length(self.calc_header_height()),
525553
Constraint::Length(1),
526554
Constraint::Min(0),
555+
Constraint::Length(self.calc_info_bar_height(area.width)),
527556
],
528557
)
529558
.split(area);
@@ -536,5 +565,6 @@ impl Widget for Tui<'_> {
536565
list_area.height = list_height;
537566
}
538567
self.render_list(list_area, buf);
568+
self.render_info_bar(layout[3], buf);
539569
}
540570
}

0 commit comments

Comments
 (0)