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.
pub fn scroll_down(&mut self) {
pub fn scroll_down(&mut self, step: isize) {
let tile = self.tile_mut(self.selected);
tile.scroll_down();
tile.scroll_down(step);
}
/// 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);
tile.scroll_up();
tile.scroll_up(step);
}
/// 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::Stderr(coords, line) => self.push_stderr(coords, line),
Msg::Click(x, y) => self.select_tile((x, y), self.term_size),
Msg::ScrollDown => self.scroll_down(),
Msg::Restart => self.restart()?,
Msg::RestartAll => self.restart_all()?,
Msg::Kill => self.kill()?,
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::ScrollFullUp => self.scroll_full_up(),
Msg::Exit => self.exit(),
@ -268,10 +268,10 @@ pub enum Msg {
KillAll,
/// Scroll up one line.
ScrollUp,
ScrollUp(isize),
/// Scroll down one line.
ScrollDown,
ScrollDown(isize),
/// Scroll to the top of the log.
ScrollFullUp,
@ -349,13 +349,13 @@ pub fn main() -> io::Result<()> {
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::KillAll).unwrap(),
Event::Key(Key::Down) => sender.send(Msg::ScrollDown).unwrap(),
Event::Key(Key::Up) => sender.send(Msg::ScrollUp).unwrap(),
Event::Key(Key::Down) => sender.send(Msg::ScrollDown(1)).unwrap(),
Event::Key(Key::Up) => sender.send(Msg::ScrollUp(1)).unwrap(),
Event::Key(Key::End) => sender.send(Msg::ScrollFullDown).unwrap(),
Event::Key(Key::Home) => sender.send(Msg::ScrollFullUp).unwrap(),
Event::Mouse(MouseEvent::Press(p, x, y)) => match p {
MouseButton::WheelUp => sender.send(Msg::ScrollUp).unwrap(),
MouseButton::WheelDown => sender.send(Msg::ScrollDown).unwrap(),
MouseButton::WheelUp => sender.send(Msg::ScrollUp(3)).unwrap(),
MouseButton::WheelDown => sender.send(Msg::ScrollDown(3)).unwrap(),
MouseButton::Left => sender.send(Msg::Click(x, y)).unwrap(),
_ => (),
},

View File

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