Skip to content

Commit

Permalink
feat(tui): execute quake action and into insert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot00 committed Dec 11, 2021
1 parent 0a22096 commit 4af0160
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
7 changes: 7 additions & 0 deletions quake_tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use quake_core::entry::EntryDefines;
use quake_core::quake::QuakeActionNode;
use quake_core::QuakeConfig;
use serde_yaml;
use std::error::Error;
Expand Down Expand Up @@ -45,12 +46,14 @@ impl Default for App {
pub enum Mode {
Command,
Normal,
Insert,
}

#[derive(Clone, Debug)]
pub enum MainWidget {
Home,
EntryTypes,
Editor(QuakeActionNode),
}

impl Widget for MainWidget {
Expand Down Expand Up @@ -89,6 +92,10 @@ impl Widget for MainWidget {

entry_types_list.render(area, buf);
}
MainWidget::Editor(_action) => {
let editor = Block::default().borders(Borders::ALL).title("Editor");
editor.render(area, buf);
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions quake_tui/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use crate::app::{App, MainWidget};
use crate::app::{App, MainWidget, Mode};
use quake_core::parser::quake::QuakeActionNode;

pub fn execute_command(command: &str, app: &mut App) -> Result<(), String> {
match command {
"quit" => app.shutdown(),
"listAll" => app.main_widget = MainWidget::EntryTypes,
_ => return Err(format!("Unknown command: {}", command)),
other => execute_action_command(other, app)?,
}
Ok(())
}

pub fn execute_action_command(command: &str, app: &mut App) -> Result<(), String> {
if let Ok(action) = QuakeActionNode::action_from_text(command) {
app.mode = Mode::Insert;
app.main_widget = MainWidget::Editor(action);
Ok(())
} else {
Err(format!("Unknown command: {}", command))
}
}

#[cfg(test)]
mod tests {
use super::execute_command;
Expand Down
7 changes: 6 additions & 1 deletion quake_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<(), B
KeyCode::Enter => {
let command: String = app.command.drain(..).collect();
execute_command(&command, &mut app)?;
app.mode = Mode::Normal;
}
KeyCode::Char(c) => {
app.command.push(c);
Expand All @@ -71,6 +70,12 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<(), B
}
_ => {}
},
Mode::Insert => match key.code {
KeyCode::Esc => {
app.mode = Mode::Normal;
}
_ => {}
},
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions quake_tui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.split(f.size());
let command_bar = Paragraph::new(app.command.as_ref())
.style(match app.mode {
Mode::Normal => Style::default(),
Mode::Command => Style::default().fg(Color::Yellow),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("Action"));
f.render_widget(command_bar, chunks[0]);
f.render_widget(app.main_widget.clone(), chunks[1]);

match app.mode {
Mode::Normal => {}
Mode::Command => f.set_cursor(
chunks[0].x + app.command.width() as u16 + 1,
chunks[0].y + 1,
),
Mode::Insert => {
f.set_cursor(chunks[1].x + 1, chunks[1].y + 1);
}
}

f.render_widget(app.main_widget.clone(), chunks[1]);
}

0 comments on commit 4af0160

Please sign in to comment.