Better scroll, fix exit code

This commit is contained in:
Thomas Forgione 2023-10-26 22:36:07 +02:00
parent e3d891c522
commit 6121dc4224
2 changed files with 58 additions and 46 deletions

View File

@ -135,15 +135,15 @@ impl<W: Write> Multiview<W> {
} }
/// Scrolls down the current selected tile. /// Scrolls down the current selected tile.
pub fn scroll_down(&mut self) { pub fn scroll_down(&mut self, step: isize) {
let tile = self.tile_mut(self.selected); let tile = self.tile_mut(self.selected);
tile.scroll_down(); tile.scroll_down(step);
} }
/// Scrolls up the current selected tile. /// Scrolls up the current selected tile.
pub fn scroll_up(&mut self) { pub fn scroll_up(&mut self, step: isize) {
let tile = self.tile_mut(self.selected); let tile = self.tile_mut(self.selected);
tile.scroll_up(); tile.scroll_up(step);
} }
/// Scrolls down to the bottom of the current selected tile. /// Scrolls down to the bottom of the current selected tile.
@ -222,12 +222,12 @@ impl<W: Write> Multiview<W> {
Msg::Stdout(coords, line) => self.push_stdout(coords, line), Msg::Stdout(coords, line) => self.push_stdout(coords, line),
Msg::Stderr(coords, line) => self.push_stderr(coords, line), Msg::Stderr(coords, line) => self.push_stderr(coords, line),
Msg::Click(x, y) => self.select_tile((x, y), self.term_size), Msg::Click(x, y) => self.select_tile((x, y), self.term_size),
Msg::ScrollDown => self.scroll_down(),
Msg::Restart => self.restart()?, Msg::Restart => self.restart()?,
Msg::RestartAll => self.restart_all()?, Msg::RestartAll => self.restart_all()?,
Msg::Kill => self.kill()?, Msg::Kill => self.kill()?,
Msg::KillAll => self.kill_all()?, Msg::KillAll => self.kill_all()?,
Msg::ScrollUp => self.scroll_up(), Msg::ScrollDown(step) => self.scroll_down(step),
Msg::ScrollUp(step) => self.scroll_up(step),
Msg::ScrollFullDown => self.scroll_full_down(), Msg::ScrollFullDown => self.scroll_full_down(),
Msg::ScrollFullUp => self.scroll_full_up(), Msg::ScrollFullUp => self.scroll_full_up(),
Msg::Exit => self.exit(), Msg::Exit => self.exit(),
@ -268,10 +268,10 @@ pub enum Msg {
KillAll, KillAll,
/// Scroll up one line. /// Scroll up one line.
ScrollUp, ScrollUp(isize),
/// Scroll down one line. /// Scroll down one line.
ScrollDown, ScrollDown(isize),
/// Scroll to the top of the log. /// Scroll to the top of the log.
ScrollFullUp, ScrollFullUp,
@ -349,13 +349,13 @@ pub fn main() -> io::Result<()> {
Event::Key(Key::Char('R')) => sender.send(Msg::RestartAll).unwrap(), Event::Key(Key::Char('R')) => sender.send(Msg::RestartAll).unwrap(),
Event::Key(Key::Char('k')) => sender.send(Msg::Kill).unwrap(), Event::Key(Key::Char('k')) => sender.send(Msg::Kill).unwrap(),
Event::Key(Key::Char('K')) => sender.send(Msg::KillAll).unwrap(), Event::Key(Key::Char('K')) => sender.send(Msg::KillAll).unwrap(),
Event::Key(Key::Down) => sender.send(Msg::ScrollDown).unwrap(), Event::Key(Key::Down) => sender.send(Msg::ScrollDown(1)).unwrap(),
Event::Key(Key::Up) => sender.send(Msg::ScrollUp).unwrap(), Event::Key(Key::Up) => sender.send(Msg::ScrollUp(1)).unwrap(),
Event::Key(Key::End) => sender.send(Msg::ScrollFullDown).unwrap(), Event::Key(Key::End) => sender.send(Msg::ScrollFullDown).unwrap(),
Event::Key(Key::Home) => sender.send(Msg::ScrollFullUp).unwrap(), Event::Key(Key::Home) => sender.send(Msg::ScrollFullUp).unwrap(),
Event::Mouse(MouseEvent::Press(p, x, y)) => match p { Event::Mouse(MouseEvent::Press(p, x, y)) => match p {
MouseButton::WheelUp => sender.send(Msg::ScrollUp).unwrap(), MouseButton::WheelUp => sender.send(Msg::ScrollUp(3)).unwrap(),
MouseButton::WheelDown => sender.send(Msg::ScrollDown).unwrap(), MouseButton::WheelDown => sender.send(Msg::ScrollDown(3)).unwrap(),
MouseButton::Left => sender.send(Msg::Click(x, y)).unwrap(), MouseButton::Left => sender.send(Msg::Click(x, y)).unwrap(),
_ => (), _ => (),
}, },

View File

@ -1,7 +1,7 @@
//! This module contains everything related to tiles. //! This module contains everything related to tiles.
use std::io::{self, Read}; use std::io::{self, Read};
use std::process::{Child, Stdio}; use std::process::Stdio;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::thread; use std::thread;
@ -96,7 +96,7 @@ impl TileBuilder {
scroll: 0, scroll: 0,
counting: true, counting: true,
column_number: 0, column_number: 0,
child: None, pty: None,
}) })
} }
} }
@ -140,8 +140,8 @@ pub struct Tile {
/// The number of the current column. /// The number of the current column.
pub column_number: u16, pub column_number: u16,
/// The PTY and the child process of the command running in the tile. /// The PTY of the command running in the tile.
pub child: Option<(Pty, Child)>, pub pty: Option<Pty>,
} }
impl Tile { impl Tile {
@ -195,23 +195,37 @@ impl Tile {
} }
} }
let code = child.wait().unwrap().code();
sender sender
.send(Msg::Stdout(coords, String::from("\n"))) .send(Msg::Stdout(coords, String::from("\n")))
.unwrap(); .unwrap();
let code = 0; let exit_string = match code {
Some(0) => format!(
let exit_string = format!( "{}{}Command finished successfully\r{}",
"{}{}Command exited with return code {}\r{}",
style::Bold, style::Bold,
if code == 0 { color::Green.fg_str(),
color::Green.fg_str()
} else {
color::Red.fg_str()
},
code,
style::Reset, style::Reset,
); ),
Some(x) => {
format!(
"{}{}Command failed with exit code {}\r{}",
style::Bold,
color::Red.fg_str(),
x,
style::Reset,
)
}
None => {
format!(
"{}{}Command was interrupted\r{}",
style::Bold,
color::Red.fg_str(),
style::Reset,
)
}
};
sender.send(Msg::Stdout(coords, exit_string)).unwrap(); sender.send(Msg::Stdout(coords, exit_string)).unwrap();
@ -245,7 +259,7 @@ impl Tile {
} }
}); });
self.child = Some((pty, child)); self.pty = Some(pty);
} }
/// Push content into the stdout of the tile. /// Push content into the stdout of the tile.
@ -558,18 +572,22 @@ impl Tile {
buffer.join("") buffer.join("")
} }
/// Scrolls up one line. /// Returns the max scroll value.
pub fn scroll_up(&mut self) { pub fn max_scroll(&self) -> isize {
if self.scroll > 0 { std::cmp::max(
self.scroll -= 1; 0,
self.stdout.len() as isize - self.inner_size.1 as isize - 1,
)
} }
/// Scrolls up one line.
pub fn scroll_up(&mut self, step: isize) {
self.scroll = std::cmp::max(0, self.scroll - step);
} }
/// Scrolls down one line. /// Scrolls down one line.
pub fn scroll_down(&mut self) { pub fn scroll_down(&mut self, step: isize) {
if self.scroll + (self.inner_size.1 as isize) < self.stdout.len() as isize - 1 { self.scroll = std::cmp::min(self.max_scroll(), self.scroll + step);
self.scroll += 1;
}
} }
/// Scrolls up one line. /// Scrolls up one line.
@ -579,18 +597,12 @@ impl Tile {
/// Scrolls down one line. /// Scrolls down one line.
pub fn scroll_full_down(&mut self) { pub fn scroll_full_down(&mut self) {
self.scroll = self.stdout.len() as isize - self.inner_size.1 as isize - 1; self.scroll = self.max_scroll()
if self.scroll < 0 {
self.scroll = 0;
}
} }
/// Kill the child command. /// Kill the child command.
pub fn kill(&mut self) -> io::Result<()> { pub fn kill(&mut self) -> io::Result<()> {
if let Some((_, child)) = self.child.as_mut() { self.pty = None;
child.kill()?;
}
Ok(()) Ok(())
} }