don't show empty elems and mouse support
This commit is contained in:
parent
4a7817a239
commit
0457d63bd0
6 changed files with 173 additions and 41 deletions
104
src/tui/mod.rs
104
src/tui/mod.rs
|
|
@ -1,13 +1,15 @@
|
|||
use crossterm::{
|
||||
event::{DisableMouseCapture, EnableMouseCapture, MouseButton, MouseEventKind},
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
use ratatui_themes::{Theme, ThemeName};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
fs::{self, DirEntry},
|
||||
io,
|
||||
io::{self, Stdout},
|
||||
ops::ControlFlow,
|
||||
path::{Path, PathBuf},
|
||||
process::exit,
|
||||
rc::Rc,
|
||||
time::Instant,
|
||||
};
|
||||
use tui_widget_list::{ListBuilder, ListView};
|
||||
|
||||
|
|
@ -23,11 +25,12 @@ use crate::tui::{
|
|||
widgets::styled::IntoStyled,
|
||||
};
|
||||
use ratatui::{
|
||||
DefaultTerminal,
|
||||
DefaultTerminal, Terminal,
|
||||
buffer::Buffer,
|
||||
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
|
||||
layout::{Constraint, HorizontalAlignment, Layout, Rect},
|
||||
style::{self, Style},
|
||||
prelude::CrosstermBackend,
|
||||
style::Style,
|
||||
text::Line,
|
||||
widgets::{
|
||||
Block, Clear, List, ListItem, ListState, Padding, Paragraph, StatefulWidget, Widget, Wrap,
|
||||
|
|
@ -75,11 +78,29 @@ perform action on selected target:
|
|||
alt-i inline
|
||||
";
|
||||
|
||||
fn setup_terminal() -> io::Result<Terminal<CrosstermBackend<Stdout>>> {
|
||||
let mut stdout = io::stdout();
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
{
|
||||
use ::std::io::Write;
|
||||
crossterm::queue!(stdout, EnterAlternateScreen, EnableMouseCapture)
|
||||
.and_then(|()| ::std::io::Write::flush(stdout.by_ref()))
|
||||
}?;
|
||||
Terminal::new(CrosstermBackend::new(stdout))
|
||||
}
|
||||
|
||||
fn teardown_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> io::Result<()> {
|
||||
let mut stdout = io::stdout();
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
crossterm::execute!(stdout, LeaveAlternateScreen, DisableMouseCapture,)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(logs_dir: PathBuf, compiler_root: Option<PathBuf>, theme: ThemeName) {
|
||||
let terminal = ratatui::init();
|
||||
let mut terminal = setup_terminal().unwrap();
|
||||
let theme = Theme::new(theme);
|
||||
let app_result = App::new(logs_dir, compiler_root, theme).run(terminal);
|
||||
ratatui::restore();
|
||||
let app_result = App::new(logs_dir, compiler_root, theme).run(&mut terminal);
|
||||
teardown_terminal(&mut terminal).unwrap();
|
||||
|
||||
if let Err(e) = app_result {
|
||||
eprintln!("error in tui: {e:?}");
|
||||
|
|
@ -92,6 +113,7 @@ enum Tab {
|
|||
files: Vec<DirEntry>,
|
||||
state: ListState,
|
||||
last_height: usize,
|
||||
last_offset: usize,
|
||||
},
|
||||
LogViewer(LogViewer),
|
||||
Empty,
|
||||
|
|
@ -171,6 +193,7 @@ impl App {
|
|||
files,
|
||||
state: ListState::default(),
|
||||
last_height: 0,
|
||||
last_offset: 0,
|
||||
},
|
||||
Err(_) => Tab::Empty,
|
||||
}
|
||||
|
|
@ -192,6 +215,7 @@ impl App {
|
|||
files,
|
||||
state,
|
||||
last_height,
|
||||
last_offset: _,
|
||||
} => match key.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => state.select_next(),
|
||||
KeyCode::Char('k') | KeyCode::Up => state.select_previous(),
|
||||
|
|
@ -337,7 +361,7 @@ impl App {
|
|||
ratatui::restore();
|
||||
let self_pid = nix::unistd::getpid();
|
||||
let _ = nix::sys::signal::kill(self_pid, nix::sys::signal::SIGTSTP);
|
||||
*terminal = ratatui::init();
|
||||
*terminal = setup_terminal().unwrap();
|
||||
}
|
||||
KeyCode::Char('?') => {
|
||||
if matches!(self.current_tab(), Tab::Help) {
|
||||
|
|
@ -357,20 +381,62 @@ impl App {
|
|||
ControlFlow::Continue(())
|
||||
}
|
||||
|
||||
fn run(mut self, mut terminal: DefaultTerminal) -> io::Result<()> {
|
||||
fn run(mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
|
||||
loop {
|
||||
self.last_error.check_expired();
|
||||
|
||||
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
|
||||
|
||||
if let Event::Key(key) = event::read()? {
|
||||
// to initialize, but we then do get manually for borrow reasons
|
||||
self.current_tab();
|
||||
match event::read()? {
|
||||
Event::Key(key) => {
|
||||
// to initialize, but we then do get manually for borrow reasons
|
||||
self.current_tab();
|
||||
|
||||
// always active
|
||||
if self.handle_generic_keycode(key, &mut terminal).is_break() {
|
||||
break Ok(());
|
||||
// always active
|
||||
if self.handle_generic_keycode(key, terminal).is_break() {
|
||||
break Ok(());
|
||||
}
|
||||
}
|
||||
Event::FocusGained => {}
|
||||
Event::FocusLost => {}
|
||||
Event::Mouse(mouse_event) => {
|
||||
if let MouseEventKind::Up(MouseButton::Left) = mouse_event.kind {
|
||||
if let Tab::LogViewer(lv) = self.current_tab() {
|
||||
lv.click(mouse_event.row);
|
||||
} else if let Tab::FileChooser {
|
||||
files,
|
||||
state,
|
||||
last_height: _,
|
||||
last_offset,
|
||||
} = self.current_tab()
|
||||
{
|
||||
if mouse_event.row as usize > *last_offset {
|
||||
let row = mouse_event.row as usize - *last_offset;
|
||||
if row < files.len() {
|
||||
if state.selected() == Some(row)
|
||||
&& let Some(selected) = files.get(row)
|
||||
{
|
||||
match LogfileReader::new(&selected.path()) {
|
||||
Ok(i) => {
|
||||
self.current_file = Some(i.clone());
|
||||
self.replace_tab(Tab::LogViewer(LogViewer::new(
|
||||
i.iter(),
|
||||
)));
|
||||
}
|
||||
Err(_) => {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
state.select(Some(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Paste(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -489,6 +555,7 @@ impl Widget for &mut App {
|
|||
files,
|
||||
state,
|
||||
last_height,
|
||||
last_offset,
|
||||
} => {
|
||||
let list = List::new(files.iter().map(|file| {
|
||||
ListItem::new(file.file_name().to_string_lossy().into_owned())
|
||||
|
|
@ -497,10 +564,12 @@ impl Widget for &mut App {
|
|||
.highlight_style(styles.highlighted);
|
||||
|
||||
*last_height = main_area.height as usize;
|
||||
*last_offset = main_area.y as usize;
|
||||
|
||||
StatefulWidget::render(list, main_area, buf, state);
|
||||
}
|
||||
Tab::LogViewer(lv) => {
|
||||
lv.last_offset = main_area.y as usize;
|
||||
lv.update_num_items(main_area.height as usize);
|
||||
|
||||
let (items, selected_offset) = lv
|
||||
|
|
@ -549,6 +618,9 @@ impl Widget for &mut App {
|
|||
.render(main_area, buf);
|
||||
|
||||
let items = lv.footer_fields();
|
||||
lv.last_fields_offset = footer_area.y as usize;
|
||||
lv.last_fields_height = items.len();
|
||||
|
||||
let width = 20;
|
||||
let builder = ListBuilder::new(|cx| {
|
||||
let Some((k, v)) = &items.get(cx.index) else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue