Add sqlite and config support

This commit is contained in:
2025-10-09 11:33:06 +08:00
parent 5053d19d73
commit 10b89aee17
14 changed files with 758 additions and 33 deletions

View File

@@ -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());
}
}