mirror of
https://github.com/uutils/procps.git
synced 2026-05-06 06:06:43 -04:00
top: tui impl info bar
This commit is contained in:
+10
-6
@@ -66,6 +66,11 @@ impl ProcList {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct InfoBar {
|
||||
pub title: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let matches = uu_app().try_get_matches_from(args)?;
|
||||
@@ -107,6 +112,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let data = Arc::new(RwLock::new((
|
||||
Header::new(&tui_stat.read().unwrap()),
|
||||
ProcList::new(&settings, &tui_stat.read().unwrap()),
|
||||
None,
|
||||
)));
|
||||
|
||||
// update
|
||||
@@ -122,7 +128,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let header = Header::new(&tui_stat.read().unwrap());
|
||||
let proc_list = ProcList::new(&settings, &tui_stat.read().unwrap());
|
||||
tui_stat.write().unwrap().input_message = None;
|
||||
*data.write().unwrap() = (header, proc_list);
|
||||
let mut data = data.write().unwrap();
|
||||
data.0 = header;
|
||||
data.1 = proc_list;
|
||||
should_update.store(true, Ordering::Relaxed);
|
||||
}
|
||||
});
|
||||
@@ -231,11 +239,7 @@ fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Ve
|
||||
}
|
||||
collected
|
||||
.into_iter()
|
||||
.map(|it| {
|
||||
it.into_iter()
|
||||
.map(|c| c.as_string(tui_stat))
|
||||
.collect()
|
||||
})
|
||||
.map(|it| it.into_iter().map(|c| c.as_string(tui_stat)).collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::header::Header;
|
||||
use crate::platform::get_numa_nodes;
|
||||
use crate::tui::stat::{CpuValueMode, TuiStat};
|
||||
use crate::Filter::{EUser, User};
|
||||
use crate::{selected_fields, try_into_uid, ProcList, Settings};
|
||||
use crate::{selected_fields, try_into_uid, InfoBar, ProcList, Settings};
|
||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::RwLock;
|
||||
@@ -40,7 +40,7 @@ pub fn handle_input(
|
||||
e: Event,
|
||||
settings: &Settings,
|
||||
tui_stat: &RwLock<TuiStat>,
|
||||
data: &RwLock<(Header, ProcList)>,
|
||||
data: &RwLock<(Header, ProcList, Option<InfoBar>)>,
|
||||
should_update: &AtomicBool,
|
||||
) -> bool {
|
||||
let input_mode = { tui_stat.read().unwrap().input_mode };
|
||||
@@ -338,7 +338,7 @@ fn handle_input_value(
|
||||
input_event: InputEvent,
|
||||
settings: &Settings,
|
||||
tui_stat: &RwLock<TuiStat>,
|
||||
data: &RwLock<(Header, ProcList)>,
|
||||
data: &RwLock<(Header, ProcList, Option<InfoBar>)>,
|
||||
should_update: &AtomicBool,
|
||||
) {
|
||||
match input_event {
|
||||
|
||||
@@ -13,7 +13,7 @@ use std::borrow::Cow;
|
||||
use crate::header::{format_memory, Header};
|
||||
use crate::tui::color::TuiColor;
|
||||
use crate::tui::stat::{CpuGraphMode, MemoryGraphMode, TuiStat};
|
||||
use crate::ProcList;
|
||||
use crate::{InfoBar, ProcList};
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Cell, Paragraph, Row, Table, TableState};
|
||||
use std::cmp::min;
|
||||
@@ -22,19 +22,21 @@ pub struct Tui<'a> {
|
||||
settings: &'a crate::Settings,
|
||||
header: &'a Header,
|
||||
proc_list: &'a ProcList,
|
||||
info_bar: &'a Option<InfoBar>,
|
||||
stat: &'a mut TuiStat,
|
||||
}
|
||||
|
||||
impl<'a> Tui<'a> {
|
||||
pub fn new(
|
||||
settings: &'a crate::Settings,
|
||||
data: &'a (Header, ProcList),
|
||||
data: &'a (Header, ProcList, Option<InfoBar>),
|
||||
stat: &'a mut TuiStat,
|
||||
) -> Self {
|
||||
Self {
|
||||
settings,
|
||||
header: &data.0,
|
||||
proc_list: &data.1,
|
||||
info_bar: &data.2,
|
||||
stat,
|
||||
}
|
||||
}
|
||||
@@ -66,6 +68,19 @@ impl<'a> Tui<'a> {
|
||||
height
|
||||
}
|
||||
|
||||
fn calc_info_bar_height(&self, width: u16) -> u16 {
|
||||
if let Some(info_bar) = &self.info_bar {
|
||||
let lines: u16 = info_bar
|
||||
.content
|
||||
.lines()
|
||||
.map(|s| (s.len() as u16).div_ceil(width))
|
||||
.sum();
|
||||
lines + 1 // 1 for title
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_list_coordinates(&self) -> (usize, usize) {
|
||||
let list_total = self.proc_list.collected.len();
|
||||
let list_offset = self.stat.list_offset;
|
||||
@@ -506,6 +521,19 @@ impl<'a> Tui<'a> {
|
||||
let table = Table::new(rows, constraints.clone()).header(header);
|
||||
StatefulWidget::render(table, area, buf, &mut state);
|
||||
}
|
||||
|
||||
fn render_info_bar(&self, area: Rect, buf: &mut Buffer) {
|
||||
if let Some(info_bar) = self.info_bar.as_ref() {
|
||||
let constraints = [Constraint::Length(1), Constraint::Min(1)];
|
||||
let layout = Layout::new(Direction::Vertical, constraints).split(area);
|
||||
Line::from(Span::styled(
|
||||
format!("{:<width$}", &info_bar.title, width = area.width as usize),
|
||||
Style::default().bg_secondary(self.stat.colorful),
|
||||
))
|
||||
.render(layout[0], buf);
|
||||
Span::raw(&info_bar.content).render(layout[1], buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Tui<'_> {
|
||||
@@ -524,6 +552,7 @@ impl Widget for Tui<'_> {
|
||||
Constraint::Length(self.calc_header_height()),
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(self.calc_info_bar_height(area.width)),
|
||||
],
|
||||
)
|
||||
.split(area);
|
||||
@@ -536,5 +565,6 @@ impl Widget for Tui<'_> {
|
||||
list_area.height = list_height;
|
||||
}
|
||||
self.render_list(list_area, buf);
|
||||
self.render_info_bar(layout[3], buf);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user