Skip to content

Commit

Permalink
Merge pull request #42 from Eliot00/entry-type
Browse files Browse the repository at this point in the history
refactor(tui): reuse app's config for entry defines
  • Loading branch information
phodal committed Dec 29, 2021
2 parents 7f705d9 + 4302b14 commit ee3dfa6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
11 changes: 9 additions & 2 deletions quake_tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crossterm::event::{self, Event, KeyCode};
use quake_core::entry::entry_defines;
use quake_core::entry::entry_paths::EntryPaths;
use quake_core::usecases::entry_usecases;
use quake_core::QuakeConfig;
Expand Down Expand Up @@ -151,7 +152,13 @@ impl App {
let command: String = self.collect_command();
match command.as_str() {
"quit" => self.shutdown(),
"listAll" => self.main_widget = MainWidget::EntryTypes,
"listAll" => {
let entry_defines_path =
Path::new(&self.config.workspace).join(EntryPaths::entries_define());
self.main_widget = MainWidget::EntryTypes(entry_defines::entries_define_from_path(
&entry_defines_path,
));
}
"save" => self.save_entry(),
other => {
return action_result_to_main_widget(other, &self.config)
Expand Down
44 changes: 14 additions & 30 deletions quake_tui/src/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
use std::error::Error;
use std::fs;
use std::path::Path;

use quake_core::entry::entry_paths::EntryPaths;
use tui::buffer::Buffer;
use tui::layout::{Alignment, Corner, Rect};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans};
use tui::widgets::{Block, Borders, List, ListItem, Paragraph, Widget};

use quake_core::entry::EntryDefines;
use quake_core::QuakeConfig;
use quake_core::entry::EntryDefine;

#[derive(Clone, Debug, PartialEq)]
pub enum MainWidget {
Home,
EntryTypes,
EntryTypes(Vec<EntryDefine>),
Editor {
entry_type: String,
id: usize,
Expand Down Expand Up @@ -51,8 +45,18 @@ impl Widget for MainWidget {
.alignment(Alignment::Center);
p.render(area, buf);
}
MainWidget::EntryTypes => {
let entry_types: Vec<ListItem> = list_entry_types().unwrap_or_default();
MainWidget::EntryTypes(defines) => {
let entry_types: Vec<ListItem> = defines
.iter()
.map(|define| {
let entry_type = Spans::from(vec![Span::styled(
define.entry_type.clone(),
Style::default().fg(Color::Yellow),
)]);

ListItem::new(entry_type)
})
.collect();
let entry_types_list = List::new(entry_types)
.block(Block::default().borders(Borders::ALL).title("List"))
.start_corner(Corner::TopLeft);
Expand Down Expand Up @@ -107,23 +111,3 @@ impl Widget for CmdLine {
message.render(area, buf);
}
}

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(EntryPaths::entries_define());
let entry_defines: EntryDefines =
serde_yaml::from_str(&fs::read_to_string(entry_defines_path)?)?;

Ok(entry_defines
.entries
.iter()
.map(|define| {
let entry_type = Spans::from(vec![Span::styled(
define.entry_type.clone(),
Style::default().fg(Color::Yellow),
)]);

ListItem::new(vec![entry_type])
})
.collect())
}

0 comments on commit ee3dfa6

Please sign in to comment.