Skip to content

Commit

Permalink
Merge 9259738 into e2c19d6
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot00 committed Dec 13, 2021
2 parents e2c19d6 + 9259738 commit cb30619
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
47 changes: 41 additions & 6 deletions quake_tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use quake_core::entry::EntryDefines;
use quake_core::quake::QuakeActionNode;
use quake_core::usecases::entry_usecases;
use quake_core::QuakeConfig;
use serde_yaml;
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use tui::buffer::Buffer;
use tui::layout::{Alignment, Corner, Rect};
use tui::style::{Color, Modifier, Style};
Expand All @@ -16,11 +18,14 @@ pub struct App {
pub command: String,
pub main_widget: MainWidget,
pub state: AppState,
pub config: QuakeConfig,
}

impl App {
pub fn new() -> App {
App::default()
pub fn new(config: QuakeConfig) -> App {
let mut app = App::default();
app.config = config;
app
}

pub fn running(&self) -> bool {
Expand All @@ -30,6 +35,19 @@ impl App {
pub fn shutdown(&mut self) {
self.state.running = false;
}

pub fn save_entry(&mut self) {
if let MainWidget::Editor(ref action, ref string) = self.main_widget {
entry_usecases::create_entry(&self.config.workspace, &action.object, &action.text)
.and_then(|(_, file)| {
let type_path = PathBuf::from(&self.config.workspace).join(&action.object);
let mut fields = HashMap::new();
fields.insert("content".to_string(), string.clone());
entry_usecases::update_entry_fields(type_path, &action.object, file.id, &fields)
})
.unwrap();
}
}
}

impl Default for App {
Expand All @@ -39,6 +57,7 @@ impl Default for App {
command: "".to_string(),
main_widget: MainWidget::Home,
state: AppState::default(),
config: QuakeConfig::default(),
}
}
}
Expand All @@ -53,7 +72,7 @@ pub enum Mode {
pub enum MainWidget {
Home,
EntryTypes,
Editor(QuakeActionNode),
Editor(QuakeActionNode, String),
}

impl Widget for MainWidget {
Expand Down Expand Up @@ -92,14 +111,30 @@ impl Widget for MainWidget {

entry_types_list.render(area, buf);
}
MainWidget::Editor(_action) => {
let editor = Block::default().borders(Borders::ALL).title("Editor");
MainWidget::Editor(_, string) => {
let editor = Paragraph::new(string.as_ref())
.block(Block::default().borders(Borders::ALL).title("Editro"));
editor.render(area, buf);
}
}
}
}

impl MainWidget {
pub fn get_input(&self) -> &str {
match self {
Self::Editor(_, string) => &string,
_ => "",
}
}

pub fn collect_input(&mut self, c: char) {
if let Self::Editor(_, ref mut string) = self {
string.push(c);
}
}
}

fn list_entry_types() -> Result<Vec<ListItem<'static>>, Box<dyn Error>> {
let config: QuakeConfig = serde_yaml::from_str(fs::read_to_string(".quake.yaml")?.as_str())?;
let entry_defines_path = Path::new(&config.workspace).join("entries-define.yaml");
Expand Down
7 changes: 4 additions & 3 deletions quake_tui/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn execute_command(command: &str, app: &mut App) -> Result<(), String> {
match command {
"quit" => app.shutdown(),
"listAll" => app.main_widget = MainWidget::EntryTypes,
"save" => app.save_entry(),
other => execute_action_command(other, app)?,
}
Ok(())
Expand All @@ -13,7 +14,7 @@ pub fn execute_command(command: &str, app: &mut App) -> Result<(), String> {
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);
app.main_widget = MainWidget::Editor(action, "".to_string());
Ok(())
} else {
Err(format!("Unknown command: {}", command))
Expand All @@ -27,7 +28,7 @@ mod tests {

#[test]
fn test_command_quit() {
let mut app = App::new();
let mut app = App::default();

assert!(app.running());
execute_command("quit", &mut app).unwrap();
Expand All @@ -36,7 +37,7 @@ mod tests {

#[test]
fn test_unknown_command() {
let mut app = App::new();
let mut app = App::default();

let result = execute_command("nonexistent", &mut app);
assert_eq!(result, Err("Unknown command: nonexistent".to_string()));
Expand Down
8 changes: 7 additions & 1 deletion quake_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use quake_core::QuakeConfig;
use std::error::Error;
use std::fs;
use std::io;
use tui::backend::{Backend, CrosstermBackend};
use tui::Terminal;
Expand All @@ -22,7 +24,8 @@ pub fn tui_main_loop() -> Result<(), Box<dyn Error>> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

let app = App::new();
let config: QuakeConfig = serde_yaml::from_str(fs::read_to_string(".quake.yaml")?.as_str())?;
let app = App::new(config);
let res = run_app(&mut terminal, app);

disable_raw_mode()?;
Expand Down Expand Up @@ -74,6 +77,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> Result<(), B
KeyCode::Esc => {
app.mode = Mode::Normal;
}
KeyCode::Char(c) => {
app.main_widget.collect_input(c);
}
_ => {}
},
}
Expand Down
5 changes: 4 additions & 1 deletion quake_tui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
chunks[0].y + 1,
),
Mode::Insert => {
f.set_cursor(chunks[1].x + 1, chunks[1].y + 1);
f.set_cursor(
chunks[1].x + app.main_widget.get_input().width() as u16 + 1,
chunks[1].y + 1,
);
}
}
}

0 comments on commit cb30619

Please sign in to comment.