-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Julien-R44/feat/search
Added fuzzy search mode + some refacto
- Loading branch information
Showing
19 changed files
with
562 additions
and
363 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,42 @@ | ||
use crossterm::event::{self, Event, KeyCode}; | ||
|
||
use crate::app::{App, ConfigDisplayMode}; | ||
use crate::app::{App, AppState}; | ||
|
||
pub fn handle_inputs(app: &mut App) -> Result<(), Box<dyn std::error::Error>> { | ||
if let Event::Key(key) = event::read()? { | ||
match app.state { | ||
AppState::Normal => handle_input_normal_mode(app, key.code), | ||
AppState::Searching => handle_input_search_mode(app, key.code), | ||
}; | ||
|
||
match key.code { | ||
KeyCode::Tab => { | ||
app.selected_group = (app.selected_group + 1) % app.scs.groups.len(); | ||
} | ||
KeyCode::Down => { | ||
let items = &app.scs.groups[app.selected_group].items; | ||
let i = match app.host_state.selected() { | ||
Some(i) => { | ||
if i >= items.len() - 1 { | ||
0 | ||
} else { | ||
i + 1 | ||
} | ||
} | ||
None => 0, | ||
}; | ||
app.host_state.select(Some(i)); | ||
} | ||
KeyCode::Up => { | ||
let items = &app.scs.groups[app.selected_group].items; | ||
let i = match app.host_state.selected() { | ||
Some(i) => { | ||
if i == 0 { | ||
items.len() - 1 | ||
} else { | ||
i - 1 | ||
} | ||
} | ||
None => 0, | ||
}; | ||
app.host_state.select(Some(i)); | ||
} | ||
KeyCode::PageDown => { | ||
app.config_paragraph_offset += 1; | ||
} | ||
KeyCode::PageUp => { | ||
app.config_paragraph_offset = | ||
(app.config_paragraph_offset as i64 - 1).max(0) as u16; | ||
} | ||
KeyCode::Char('c') => { | ||
app.config_display_mode = match app.config_display_mode { | ||
ConfigDisplayMode::Global => ConfigDisplayMode::Selected, | ||
ConfigDisplayMode::Selected => ConfigDisplayMode::Global, | ||
}; | ||
} | ||
KeyCode::Char('h') => { | ||
app.show_help = !app.show_help; | ||
} | ||
KeyCode::Char('q') => app.should_quit = true, | ||
KeyCode::Tab => app.change_selected_group(), | ||
KeyCode::Down => app.change_selected_item(true), | ||
KeyCode::Up => app.change_selected_item(false), | ||
KeyCode::PageDown => app.scroll_config_paragraph(1), | ||
KeyCode::PageUp => app.scroll_config_paragraph(-1), | ||
KeyCode::Enter => app.should_spawn_ssh = true, | ||
_ => {} | ||
} | ||
}; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn handle_input_search_mode(app: &mut App, key: KeyCode) { | ||
match key { | ||
KeyCode::Esc => app.state = AppState::Normal, | ||
KeyCode::Char(c) => app.searcher.add_char(c), | ||
KeyCode::Backspace => app.searcher.del_char(), | ||
_ => {} | ||
} | ||
} | ||
|
||
fn handle_input_normal_mode(app: &mut App, key: KeyCode) { | ||
match key { | ||
KeyCode::Char('c') => app.toggle_config_display_mode(), | ||
KeyCode::Char('h') => app.show_help = !app.show_help, | ||
KeyCode::Char('s') => app.state = AppState::Searching, | ||
KeyCode::Char('q') => app.should_quit = true, | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::io::Stdout; | ||
|
||
use tui::{ | ||
backend::CrosstermBackend, | ||
layout::{Constraint, Direction, Layout, Rect}, | ||
Frame, | ||
}; | ||
|
||
use crate::app::App; | ||
|
||
pub struct AppLayout { | ||
pub chunks_top: Vec<Rect>, | ||
pub chunks_bot: Vec<Rect>, | ||
} | ||
|
||
pub fn create_layout(app: &App, frame: &mut Frame<CrosstermBackend<Stdout>>) -> AppLayout { | ||
let base_chunk = Layout::default() | ||
.direction(Direction::Vertical) | ||
.margin(1) | ||
.horizontal_margin(4) | ||
.constraints([Constraint::Length(3), Constraint::Percentage(90)].as_ref()) | ||
.split(frame.size()); | ||
|
||
let chunks_top = Layout::default() | ||
.direction(Direction::Horizontal) | ||
.margin(0) | ||
.constraints( | ||
[ | ||
Constraint::Percentage(80), | ||
Constraint::Length(2), | ||
Constraint::Length(10), | ||
] | ||
.as_ref(), | ||
) | ||
.split(base_chunk[0]); | ||
|
||
let constraints = match app.show_help { | ||
false => { | ||
vec![ | ||
Constraint::Percentage(50), | ||
Constraint::Length(2), | ||
Constraint::Percentage(50), | ||
] | ||
} | ||
true => { | ||
vec![ | ||
Constraint::Percentage(40), | ||
Constraint::Length(2), | ||
Constraint::Percentage(30), | ||
Constraint::Length(2), | ||
Constraint::Percentage(30), | ||
] | ||
} | ||
}; | ||
|
||
let chunks_bot = Layout::default() | ||
.direction(Direction::Horizontal) | ||
.margin(1) | ||
.horizontal_margin(0) | ||
.constraints(constraints.as_ref()) | ||
.split(base_chunk[1]); | ||
|
||
AppLayout { | ||
chunks_bot, | ||
chunks_top, | ||
} | ||
} |
Oops, something went wrong.