Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tui): reuse app's config for entry defines #42

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}