1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-30 04:29:10 +01:00

refactor; comment; remove anyhow and error propogation for now

This commit is contained in:
Caleb Bassi 2020-02-07 12:56:45 -08:00
parent 0ed8d75fa7
commit 00ae64e7b2
6 changed files with 78 additions and 75 deletions

7
Cargo.lock generated
View File

@ -8,11 +8,6 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "anyhow"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "arc-swap"
version = "0.4.4"
@ -1055,7 +1050,6 @@ dependencies = [
name = "ytop"
version = "0.4.1"
dependencies = [
"anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)",
"battery 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
"better-panic 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1077,7 +1071,6 @@ dependencies = [
[metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c"
"checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff"
"checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee"
"checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"

View File

@ -7,7 +7,6 @@ description = "A TUI system monitor written in Rust"
edition = "2018"
[dependencies]
anyhow = "1.0.26"
battery = "0.7.5"
better-panic = "0.2.0"
chrono = "0.4.10"

View File

@ -1,5 +1,3 @@
use std::io;
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::widgets::Widget as _;
@ -7,21 +5,23 @@ use tui::{Frame, Terminal};
use crate::app::{App, Widgets};
pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
terminal.draw(|mut frame| {
if let Some(statusbar) = app.statusbar.as_mut() {
let chunks = Layout::default()
.constraints([Constraint::Min(0), Constraint::Length(1)].as_ref())
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
statusbar.render(&mut frame, chunks[1]);
} else {
let chunks = Layout::default()
.constraints(vec![Constraint::Percentage(100)])
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
}
})
pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
terminal
.draw(|mut frame| {
if let Some(statusbar) = app.statusbar.as_mut() {
let chunks = Layout::default()
.constraints([Constraint::Min(0), Constraint::Length(1)].as_ref())
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
statusbar.render(&mut frame, chunks[1]);
} else {
let chunks = Layout::default()
.constraints(vec![Constraint::Percentage(100)])
.split(frame.size());
draw_widgets(&mut frame, &mut app.widgets, chunks[0]);
}
})
.unwrap();
}
pub fn draw_widgets<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets, area: Rect) {
@ -102,16 +102,18 @@ pub fn draw_bottom_row<B: Backend>(frame: &mut Frame<B>, widgets: &mut Widgets,
widgets.proc.render(frame, horizontal_chunks[1]);
}
pub fn draw_help_menu<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
terminal.draw(|mut frame| {
let rect = app.help_menu.get_rect(frame.size());
app.help_menu.render(&mut frame, rect);
})
pub fn draw_help_menu<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
terminal
.draw(|mut frame| {
let rect = app.help_menu.get_rect(frame.size());
app.help_menu.render(&mut frame, rect);
})
.unwrap();
}
// TODO: figure out how to draw the proc widget without clearing rest of the screen
pub fn draw_proc<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
draw(terminal, app)
pub fn draw_proc<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
draw(terminal, app);
// terminal.draw(|mut frame| {
// let chunks = if app.statusbar.is_some() {
// Layout::default()
@ -151,6 +153,6 @@ pub fn draw_proc<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::R
}
// TODO: figure out how to draw the graphs without clearing rest of the screen
pub fn draw_graphs<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<()> {
draw(terminal, app)
pub fn draw_graphs<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) {
draw(terminal, app);
}

View File

@ -12,7 +12,6 @@ use std::path::Path;
use std::thread;
use std::time::{Duration, Instant};
use anyhow::Result;
use crossbeam_channel::{select, tick, unbounded, Receiver};
use crossterm::cursor;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent};
@ -30,36 +29,37 @@ use colorscheme::*;
use draw::*;
use update::*;
fn setup_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>> {
fn setup_terminal() {
let mut stdout = io::stdout();
execute!(stdout, terminal::EnterAlternateScreen)?;
execute!(stdout, cursor::Hide)?;
execute!(stdout, terminal::EnterAlternateScreen).unwrap();
execute!(stdout, cursor::Hide).unwrap();
// for TTYs
execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
// needed for when ytop is run in a TTY since TTYs don't actually have an alternate screen
// must be executed after attempting to enter the alternate screen so that it only clears the
// primary screen if we are running in a TTY
// if not running in a TTY, then we just end up clearing the alternate screen which should have
// no effect
execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();
terminal::enable_raw_mode()?;
let backend = CrosstermBackend::new(stdout);
let terminal = Terminal::new(backend)?;
Ok(terminal)
terminal::enable_raw_mode().unwrap();
}
fn cleanup_terminal() -> Result<()> {
fn cleanup_terminal() {
let mut stdout = io::stdout();
// for TTYs
execute!(stdout, cursor::MoveTo(0, 0))?;
execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
// needed for when ytop is run in a TTY since TTYs don't actually have an alternate screen
// must be executed before attempting to leave the alternate screen so that it only modifies the
// primary screen if we are running in a TTY
// if not running in a TTY, then we just end up modifying the alternate screen which should have
// no effect
execute!(stdout, cursor::MoveTo(0, 0)).unwrap();
execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();
execute!(stdout, terminal::LeaveAlternateScreen)?;
execute!(stdout, cursor::Show)?;
execute!(stdout, terminal::LeaveAlternateScreen).unwrap();
execute!(stdout, cursor::Show).unwrap();
terminal::disable_raw_mode()?;
Ok(())
terminal::disable_raw_mode().unwrap();
}
fn setup_ui_events() -> Receiver<Event> {
@ -71,13 +71,14 @@ fn setup_ui_events() -> Receiver<Event> {
receiver
}
fn setup_ctrl_c() -> Result<Receiver<()>, ctrlc::Error> {
fn setup_ctrl_c() -> Receiver<()> {
let (sender, receiver) = unbounded();
ctrlc::set_handler(move || {
sender.send(()).unwrap();
})?;
})
.unwrap();
Ok(receiver)
receiver
}
fn setup_logfile(logfile_path: &Path) {
@ -106,7 +107,7 @@ fn setup_logfile(logfile_path: &Path) {
fn setup_panic_hook() {
panic::set_hook(Box::new(|panic_info| {
cleanup_terminal().unwrap();
cleanup_terminal();
better_panic::Settings::auto().create_panic_handler()(panic_info);
}));
}
@ -130,17 +131,20 @@ fn main() {
let colorscheme = read_colorscheme(&app_dirs.config_dir, &args.colorscheme).unwrap();
let mut app = setup_app(&args, update_ratio, &colorscheme, program_name);
setup_logfile(&logfile_path);
let mut terminal = setup_terminal().unwrap();
let backend = CrosstermBackend::new(io::stdout());
let mut terminal = Terminal::new(backend).unwrap();
setup_panic_hook();
setup_terminal();
let mut update_seconds = Ratio::from_integer(0);
let ticker = setup_ticker(args.rate);
let ui_events_receiver = setup_ui_events();
let ctrl_c_events = setup_ctrl_c().unwrap();
let ctrl_c_events = setup_ctrl_c();
update_widgets(&mut app.widgets, update_seconds);
draw(&mut terminal, &mut app).unwrap();
draw(&mut terminal, &mut app);
let mut show_help_menu = false;
let mut paused = false;
@ -152,7 +156,6 @@ fn main() {
loop {
select! {
recv(ctrl_c_events) -> _ => {
cleanup_terminal().unwrap();
break;
}
recv(ticker) -> _ => {
@ -160,7 +163,7 @@ fn main() {
update_seconds = (update_seconds + update_ratio) % Ratio::from_integer(60);
update_widgets(&mut app.widgets, update_seconds);
if !show_help_menu {
draw(&mut terminal, &mut app).unwrap();
draw(&mut terminal, &mut app);
}
}
}
@ -174,15 +177,14 @@ fn main() {
if key_event.modifiers.is_empty() {
match key_event.code {
KeyCode::Char('q') => {
cleanup_terminal().unwrap();
break
},
KeyCode::Char('?') => {
show_help_menu = !show_help_menu;
if show_help_menu {
draw_help_menu(&mut terminal, &mut app).unwrap();
draw_help_menu(&mut terminal, &mut app);
} else {
draw(&mut terminal, &mut app).unwrap();
draw(&mut terminal, &mut app);
}
},
KeyCode::Char(' ') => {
@ -230,7 +232,7 @@ fn main() {
KeyCode::Esc => {
if show_help_menu {
show_help_menu = false;
draw(&mut terminal, &mut app).unwrap();
draw(&mut terminal, &mut app);
}
}
KeyCode::Tab => {
@ -258,7 +260,6 @@ fn main() {
} else if key_event.modifiers == KeyModifiers::CONTROL {
match key_event.code {
KeyCode::Char('c') => {
cleanup_terminal().unwrap();
break
},
KeyCode::Char('d') => {
@ -301,21 +302,23 @@ fn main() {
}
Event::Resize(_width, _height) => {
if show_help_menu {
draw_help_menu(&mut terminal, &mut app).unwrap();
draw_help_menu(&mut terminal, &mut app);
} else {
draw(&mut terminal, &mut app).unwrap();
draw(&mut terminal, &mut app);
}
}
}
if !show_help_menu {
if proc_modified {
draw_proc(&mut terminal, &mut app).unwrap();
draw_proc(&mut terminal, &mut app);
} else if graphs_modified {
draw_graphs(&mut terminal, &mut app).unwrap();
draw_graphs(&mut terminal, &mut app);
}
}
}
}
}
cleanup_terminal();
}

View File

@ -54,12 +54,14 @@ impl UpdatableWidget for DiskWidget<'_> {
let mut io_counters_perdisk = if cfg!(target_os = "linux") {
self.collector.disk_io_counters_per_partition().unwrap()
} else {
Default::default()
Default::default() // not implemented yet on macOS
};
// `.rev()` selects the correct mountpoint when the partition is mounted multiple times
// https://github.com/cjbassi/ytop/issues/25
self.partitions = disk::partitions_physical()
.unwrap()
.into_iter()
.rev() // fixes the mountpoint when the partition is mounted multiple times (#25)
.rev()
.map(|partition| {
let name = PathBuf::from(partition.device())
.file_name()
@ -134,6 +136,8 @@ impl Widget for DiskWidget<'_> {
)
.block(block::new(self.colorscheme, &self.title))
.header_style(self.colorscheme.text.modifier(Modifier::BOLD))
// TODO: this is only a temporary workaround until we fix the table column resizing
// https://github.com/cjbassi/ytop/issues/23
.widths(&if area.width > 55 {
vec![
// Constraint::Min(5),

View File

@ -350,6 +350,8 @@ impl Widget for ProcWidget<'_> {
)
.block(block::new(self.colorscheme, &self.title))
.header_style(self.colorscheme.text.modifier(Modifier::BOLD))
// TODO: this is only a temporary workaround until we fix the table column resizing
// https://github.com/cjbassi/ytop/issues/23
.widths(&[
Constraint::Length(6),
// Constraint::Min(5),