Add sqlite and config support
This commit is contained in:
130
src/app.rs
130
src/app.rs
@@ -1,27 +1,74 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use crate::event::{Event, EventHandler};
|
||||
use ratatui::widgets::Block;
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph};
|
||||
use ratatui::{DefaultTerminal};
|
||||
use std::time::Duration;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::KeyCode::Char;
|
||||
use ratatui::layout::Alignment;
|
||||
use diesel::{AggregateExpressionMethods, Connection, SqliteConnection};
|
||||
use directories::BaseDirs;
|
||||
use dotenvy::dotenv;
|
||||
use lazy_static::lazy_static;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::prelude::{Widget};
|
||||
use ratatui::style::{Color, Style};
|
||||
use ratatui::text::{Line, Span, Text};
|
||||
use crate::config::types::ApplicationConfig;
|
||||
|
||||
const APP_DIR_NAME: &str = "sus_manager";
|
||||
lazy_static! {
|
||||
static ref BASE_DIRS: BaseDirs = BaseDirs::new().unwrap();
|
||||
static ref APP_CONFIG_DIR: PathBuf = BASE_DIRS.config_dir().to_path_buf()
|
||||
.join(APP_DIR_NAME);
|
||||
static ref APP_DATA_DIR: PathBuf = BASE_DIRS.data_dir().to_path_buf()
|
||||
.join(APP_DIR_NAME);
|
||||
static ref APP_CONIFG_FILE_PATH: PathBuf = APP_CONFIG_DIR.clone()
|
||||
.join("config.ini");
|
||||
}
|
||||
|
||||
pub(crate) struct App {
|
||||
running: bool,
|
||||
events: EventHandler,
|
||||
db_connection: SqliteConnection,
|
||||
app_config: ApplicationConfig,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> App {
|
||||
let app_conf =
|
||||
if APP_CONIFG_FILE_PATH.exists() { ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH) }
|
||||
else { ApplicationConfig::new() };
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
Self::initialize(&db_conn);
|
||||
App {
|
||||
running: true,
|
||||
events: EventHandler::new(Duration::from_millis(250)),
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
fn initialize(db_conn: &SqliteConnection) {
|
||||
if !APP_CONFIG_DIR.exists() {
|
||||
std::fs::create_dir_all(APP_CONFIG_DIR.as_path()).unwrap();
|
||||
}
|
||||
if !APP_DATA_DIR.exists() {
|
||||
std::fs::create_dir_all(APP_DATA_DIR.as_path()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn establish_db_connection(application_config: ApplicationConfig) -> SqliteConnection {
|
||||
let database_url = application_config.basic_config.db_path;
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
pub async fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
loop {
|
||||
terminal.draw(|frame| App::render(self, frame))?;
|
||||
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
|
||||
let event = self.events.next().await?;
|
||||
self.update(event)?;
|
||||
if !self.running {
|
||||
@@ -31,7 +78,7 @@ impl App {
|
||||
}
|
||||
|
||||
fn update(&mut self, event: Event) -> Result<()> {
|
||||
if let Event::Key(key) = event {
|
||||
if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) {
|
||||
match key.code {
|
||||
Char('q') => self.quit(),
|
||||
_ => {}
|
||||
@@ -39,17 +86,66 @@ impl App {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn quit(&mut self) {
|
||||
self.running = false;
|
||||
}
|
||||
impl Widget for &mut App {
|
||||
fn render(self, area: Rect, buf: &mut Buffer)
|
||||
where Self: Sized
|
||||
{
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(3),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
self.render_header(chunks[0], buf);
|
||||
self.render_game_list(chunks[1], buf);
|
||||
self.render_footer(chunks[2], buf);
|
||||
|
||||
fn render(&mut self, frame: &mut Frame) {
|
||||
frame.render_widget(
|
||||
Block::bordered()
|
||||
.title("Sus Manager")
|
||||
.title_alignment(Alignment::Center),
|
||||
frame.area()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// render widgets
|
||||
impl App {
|
||||
fn render_game_list(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let game_list = Block::new()
|
||||
.title(Line::raw("Games"))
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
|
||||
game_list.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_header(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let title = Paragraph::new(
|
||||
Text::styled(
|
||||
"SuS Manager",
|
||||
Style::default().fg(Color::Green),
|
||||
)
|
||||
);
|
||||
title.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_footer(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let navigation_text = vec![
|
||||
Span::styled("(q) quit / (a) add folders", Style::default().fg(Color::Green)),
|
||||
];
|
||||
let line = Line::from(navigation_text);
|
||||
let footer = Paragraph::new(line);
|
||||
footer.render(area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// event handlers
|
||||
impl App {
|
||||
fn quit(&mut self) {
|
||||
self.running = false;
|
||||
self.app_config
|
||||
.clone()
|
||||
.write_to_file(APP_CONIFG_FILE_PATH.to_path_buf());
|
||||
}
|
||||
}
|
||||
36
src/config/mod.rs
Normal file
36
src/config/mod.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::path::{PathBuf};
|
||||
use ini::Ini;
|
||||
use crate::config::types::{ApplicationConfig, BasicConfig};
|
||||
|
||||
pub mod types;
|
||||
|
||||
impl ApplicationConfig {
|
||||
pub fn from_file(path: &PathBuf) -> Self {
|
||||
let conf = Ini::load_from_file(path).unwrap();
|
||||
let basic_conf_section = conf.section(Some("Basic")).unwrap();
|
||||
let basic_conf = BasicConfig {
|
||||
db_path: basic_conf_section.get("DBPath").unwrap().to_string(),
|
||||
tick_rate: basic_conf_section.get("TickRate").unwrap().parse().unwrap(),
|
||||
};
|
||||
Self {
|
||||
basic_config: basic_conf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
basic_config: BasicConfig {
|
||||
db_path: "games.db".to_string(),
|
||||
tick_rate: 250
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_to_file(self, path: PathBuf) {
|
||||
let mut conf = Ini::new();
|
||||
conf.with_section(Some("Basic"))
|
||||
.set("DBPath", self.basic_config.db_path)
|
||||
.set("TickRate", self.basic_config.tick_rate.to_string());
|
||||
conf.write_to_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
10
src/config/types.rs
Normal file
10
src/config/types.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ApplicationConfig {
|
||||
pub(crate) basic_config: BasicConfig,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct BasicConfig {
|
||||
pub(crate) db_path: String,
|
||||
pub(crate) tick_rate: u64
|
||||
}
|
||||
@@ -22,7 +22,7 @@ pub(crate) struct EventHandler {
|
||||
impl EventHandler {
|
||||
pub fn new(tick_rate: Duration) -> Self {
|
||||
let mut interval = tokio::time::interval(tick_rate);
|
||||
let mut event_reader = crossterm::event::EventStream::new();
|
||||
let mut event_reader = event::EventStream::new();
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
|
||||
let _tx = tx.clone();
|
||||
@@ -37,8 +37,7 @@ impl EventHandler {
|
||||
tx.send(Event::Error).unwrap()
|
||||
}
|
||||
else if let Some(Ok(event)) = maybe_event &&
|
||||
let event::Event::Key(key) = event &&
|
||||
key.kind == event::KeyEventKind::Press
|
||||
let event::Event::Key(key) = event
|
||||
{
|
||||
tx.send(Event::Key(key)).unwrap()
|
||||
}
|
||||
|
||||
0
src/helpers/mod.rs
Normal file
0
src/helpers/mod.rs
Normal file
@@ -1,5 +1,9 @@
|
||||
mod app;
|
||||
mod event;
|
||||
mod schema;
|
||||
mod types;
|
||||
mod config;
|
||||
mod helpers;
|
||||
|
||||
use color_eyre::Result;
|
||||
use tokio;
|
||||
@@ -8,7 +12,7 @@ use tokio;
|
||||
async fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let terminal = ratatui::init();
|
||||
let mut app = app::App::new();
|
||||
let app = app::App::new();
|
||||
let result = app.run(terminal).await;
|
||||
ratatui::restore();
|
||||
result
|
||||
|
||||
7
src/schema.rs
Normal file
7
src/schema.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
dl_games (serial_number) {
|
||||
serial_number -> Text,
|
||||
}
|
||||
}
|
||||
14
src/types/game.rs
Normal file
14
src/types/game.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use diesel::{Queryable, Selectable};
|
||||
use ratatui::widgets::ListState;
|
||||
|
||||
pub(crate) struct GameList<T> {
|
||||
games: Vec<T>,
|
||||
state: ListState,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::schema::dl_games)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub(crate) struct DLSiteGame {
|
||||
serial_number: String
|
||||
}
|
||||
1
src/types/mod.rs
Normal file
1
src/types/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod game;
|
||||
Reference in New Issue
Block a user