tui
This commit is contained in:
148
src/main.rs
148
src/main.rs
@ -1 +1,147 @@
|
||||
fn main() {}
|
||||
mod app;
|
||||
mod ipc;
|
||||
mod models;
|
||||
mod ui;
|
||||
|
||||
use app::{App, Mode};
|
||||
use crossterm::{
|
||||
event::{self, Event, KeyCode},
|
||||
execute,
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
|
||||
};
|
||||
use ratatui::{Terminal, backend::CrosstermBackend};
|
||||
use std::io;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
let mut app = App::new();
|
||||
let res = run_app(&mut terminal, &mut app);
|
||||
|
||||
disable_raw_mode()?;
|
||||
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||
terminal.show_cursor()?;
|
||||
|
||||
if let Err(err) = res {
|
||||
println!("{:?}", err);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>, app: &mut App) -> io::Result<()> {
|
||||
let mut last_key_was_d = false;
|
||||
let mut last_key_was_g = false;
|
||||
|
||||
loop {
|
||||
terminal.draw(|f| ui::render(f, app))?;
|
||||
|
||||
if let Event::Key(key) = event::read()? {
|
||||
match app.mode {
|
||||
Mode::Normal => match key.code {
|
||||
KeyCode::Char('j') => {
|
||||
app.next();
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('k') => {
|
||||
app.previous();
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('d') => {
|
||||
if last_key_was_d {
|
||||
app.delete_selected();
|
||||
last_key_was_d = false;
|
||||
} else {
|
||||
last_key_was_d = true;
|
||||
}
|
||||
last_key_was_g = false;
|
||||
}
|
||||
KeyCode::Char('u') => {
|
||||
app.undo_delete();
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('g') => {
|
||||
if last_key_was_g {
|
||||
if !app.filtered_items.is_empty() {
|
||||
app.list_state.select(Some(0));
|
||||
}
|
||||
last_key_was_g = false;
|
||||
} else {
|
||||
last_key_was_g = true;
|
||||
}
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('G') => {
|
||||
if !app.filtered_items.is_empty() {
|
||||
app.list_state.select(Some(app.filtered_items.len() - 1));
|
||||
}
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char(':') => {
|
||||
app.mode = Mode::Command;
|
||||
app.input_buffer.clear();
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('/') => {
|
||||
app.mode = Mode::Search;
|
||||
app.input_buffer.clear();
|
||||
app.update_search();
|
||||
last_key_was_d = false;
|
||||
}
|
||||
KeyCode::Char('q') => {
|
||||
app.should_quit = true;
|
||||
}
|
||||
_ => {
|
||||
last_key_was_d = false;
|
||||
}
|
||||
},
|
||||
|
||||
Mode::Command => match key.code {
|
||||
KeyCode::Esc => {
|
||||
app.mode = Mode::Normal;
|
||||
app.input_buffer.clear();
|
||||
app.update_search();
|
||||
}
|
||||
KeyCode::Char(c) => app.input_buffer.push(c),
|
||||
KeyCode::Backspace => {
|
||||
app.input_buffer.pop();
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if app.input_buffer == "q" {
|
||||
app.should_quit = true;
|
||||
}
|
||||
app.mode = Mode::Normal;
|
||||
app.input_buffer.clear();
|
||||
app.update_search();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
Mode::Search => match key.code {
|
||||
// ... ton code de recherche ...
|
||||
KeyCode::Esc | KeyCode::Enter => {
|
||||
app.mode = Mode::Normal;
|
||||
app.input_buffer.clear();
|
||||
app.update_search();
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
app.input_buffer.push(c);
|
||||
app.update_search();
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
app.input_buffer.pop();
|
||||
app.update_search();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if app.should_quit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user