Add dlsite cli add folder
This commit is contained in:
81
src/app.rs
81
src/app.rs
@@ -1,13 +1,12 @@
|
||||
use std::path::PathBuf;
|
||||
use crate::event::{Event, EventHandler};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph};
|
||||
use ratatui::{DefaultTerminal};
|
||||
use std::time::Duration;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyCode::Char;
|
||||
use diesel::{Connection, SqliteConnection};
|
||||
use lazy_static::lazy_static;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::prelude::{Widget};
|
||||
@@ -16,11 +15,24 @@ use ratatui::text::{Line, Span, Text};
|
||||
use crate::config::types::ApplicationConfig;
|
||||
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
|
||||
enum AppState {
|
||||
Running,
|
||||
Exiting,
|
||||
Input
|
||||
}
|
||||
|
||||
enum AppPopUp {
|
||||
AddFolder,
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) struct App {
|
||||
running: bool,
|
||||
state: AppState,
|
||||
popup: AppPopUp,
|
||||
events: EventHandler,
|
||||
db_connection: SqliteConnection,
|
||||
app_config: ApplicationConfig,
|
||||
current_event: Option<Event>
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -31,10 +43,12 @@ impl App {
|
||||
Self::initialize();
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
Self {
|
||||
running: true,
|
||||
state: AppState::Running,
|
||||
popup: AppPopUp::None,
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf
|
||||
app_config: app_conf,
|
||||
current_event: None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,16 +72,29 @@ impl App {
|
||||
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
|
||||
let event = self.events.next().await?;
|
||||
self.update(event)?;
|
||||
if !self.running {
|
||||
if matches!(self.state, AppState::Exiting) {
|
||||
break Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, event: Event) -> Result<()> {
|
||||
if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) {
|
||||
self.current_event = Some(event.clone());
|
||||
if let Event::Key(key) = event && matches!(self.state, AppState::Input) {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.state = AppState::Running;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if let Event::Key(key) = event &&
|
||||
event::KeyEventKind::is_press(&key.kind) &&
|
||||
!matches!(self.state, AppState::Input) {
|
||||
match key.code {
|
||||
Char('q') => self.quit()?,
|
||||
Char('a') => self.folder_popup(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -84,7 +111,7 @@ impl Widget for &mut App {
|
||||
.constraints([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
@@ -92,6 +119,17 @@ impl Widget for &mut App {
|
||||
self.render_game_list(chunks[1], buf);
|
||||
self.render_footer(chunks[2], buf);
|
||||
|
||||
let popup_area = Rect {
|
||||
x: area.width / 4,
|
||||
y: area.height / 3,
|
||||
width: area.width / 2,
|
||||
height: area.height / 3,
|
||||
};
|
||||
|
||||
match self.popup {
|
||||
AppPopUp::AddFolder => self.render_folder_popup(popup_area, buf),
|
||||
AppPopUp::None => {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,23 +155,40 @@ impl App {
|
||||
}
|
||||
|
||||
fn render_footer(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let navigation_text = vec![
|
||||
let mut navigation_text = vec![
|
||||
Span::styled("(q) quit / (a) add folders", Style::default().fg(Color::Green)),
|
||||
];
|
||||
if matches!(self.state, AppState::Input) {
|
||||
navigation_text[0] = Span::styled("Input Mode", Style::default().fg(Color::Green));
|
||||
}
|
||||
let line = Line::from(navigation_text);
|
||||
let footer = Paragraph::new(line);
|
||||
footer.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_folder_popup(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// event handlers
|
||||
impl App {
|
||||
fn quit(&mut self) -> Result<()> {
|
||||
self.running = false;
|
||||
self.app_config
|
||||
.clone()
|
||||
.write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
if matches!(self.popup, AppPopUp::None) {
|
||||
self.state = AppState::Exiting;
|
||||
self.app_config
|
||||
.clone()
|
||||
.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
}
|
||||
else {
|
||||
self.popup = AppPopUp::None;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn folder_popup(&mut self) {
|
||||
self.popup = AppPopUp::AddFolder;
|
||||
self.state = AppState::Input;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user