Basic search bar input

devel
~erin 2023-03-21 22:13:11 -04:00
parent 6fa50943a8
commit 8997dd64c1
Signed by: erin
GPG Key ID: 9A8E308CEFA37A47
2 changed files with 63 additions and 9 deletions

View File

@ -21,7 +21,7 @@ use tui::{
use paris::error;
mod structs;
use structs::{App, StatefulList};
use structs::{App, StatefulList, InputMode};
fn main() -> Result<(), Box<dyn Error>> {
// setup terminal
@ -62,12 +62,30 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App, tick_rate: Dura
.unwrap_or_else(|| Duration::from_secs(0));
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(()),
KeyCode::Char('h') => app.items.unselect(),
KeyCode::Char('j') => app.items.next(),
KeyCode::Char('k') => app.items.previous(),
_ => {}
match app.input_mode {
InputMode::Normal => match key.code {
KeyCode::Char('q') => return Ok(()),
KeyCode::Char('h') => app.items.unselect(),
KeyCode::Char('j') => app.items.next(),
KeyCode::Char('k') => app.items.previous(),
KeyCode::Char('i') => app.input_mode = InputMode::Editing,
_ => {}
},
InputMode::Editing => match key.code {
KeyCode::Enter => {
app.messages.push(app.input.drain(..).collect());
},
KeyCode::Char(c) => {
app.input.push(c);
},
KeyCode::Backspace => {
app.input.pop();
},
KeyCode::Esc => {
app.input_mode = InputMode::Normal;
},
_ => {}
},
}
}
}
@ -123,6 +141,12 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)].as_ref())
.split(chunks[0]);
// Song info & Search bar
let song_search = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(80), Constraint::Percentage(20)].as_ref())
.margin(2)
.split(top_chunks[1]);
// Cover art block
let image = Block::default()
@ -200,9 +224,28 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.style(Style::default())
.alignment(Alignment::Center)
.wrap(Wrap { trim: true } );
f.render_widget(song, top_chunks[1]);
f.render_widget(song, song_search[0]);
// Search/sort
// Search
let input = Paragraph::new(app.input.as_ref())
.style(match app.input_mode {
InputMode::Normal => Style::default(),
InputMode::Editing => Style::default().fg(Color::Yellow),
})
.block(Block::default().borders(Borders::ALL).title("Search"));
f.render_widget(input, song_search[1]);
match app.input_mode {
InputMode::Normal =>
{}
InputMode::Editing => {
f.set_cursor(
song_search[1].x + app.input.len() as u16 + 1,
song_search[1].y + 1,
)
}
}
// Sort & View
let bottom_chunks = Layout::default()
.direction(Direction::Horizontal)
.margin(1)

View File

@ -61,6 +61,9 @@ impl<T> StatefulList<T> {
pub struct App<'a> {
pub items: StatefulList<(&'a str, usize)>,
pub mpd_client: Client,
pub input: String,
pub input_mode: InputMode,
pub messages: Vec<String>,
}
impl<'a> App<'a> {
@ -78,6 +81,14 @@ impl<'a> App<'a> {
App {
items: StatefulList::with_items(vec![("Item0", 1), ("Item1", 2), ("Item2", 3)]),
mpd_client: client,
input: String::new(),
input_mode: InputMode::Normal,
messages: Vec::new(),
}
}
}
pub enum InputMode {
Normal,
Editing,
}