allow for usage of history, config, ai outside of main

master
Tymon 1 year ago
parent e96adf88f6
commit 7911e0b390

@ -3,12 +3,14 @@ name = "aigui"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["gui"]
gui = ["dep:eframe"]
[dependencies]
anyhow = "1.0.70"
chrono = { version = "0.4.24", features = ["serde"] }
eframe = "0.21.3"
eframe = { version = "0.21.3", optional = true }
egui-notify = "0.6.0"
flume = "0.10.14"
once_cell = "1.17.1"

@ -66,12 +66,15 @@ impl eframe::App for App {
eprintln!("{}", recieved.output);
}
match write_history(HistoryEntry {
date: Local::now(),
selected_ai: self.selected_ai.name.clone(),
params: self.params.clone(),
result: self.output.clone(),
}) {
match write_history(
HistoryEntry {
date: Local::now(),
selected_ai: self.selected_ai.name.clone(),
params: self.params.clone(),
result: self.output.clone(),
},
CONFIG.app.history_dir.clone().unwrap(),
) {
Ok(_) => {}
Err(error) => {
self.toasts
@ -197,7 +200,7 @@ impl eframe::App for App {
ui.with_layout(Layout::right_to_left(Align::Max), |ui| {
ui.add_visible_ui(CONFIG.app.history_dir.is_some(), |ui| {
if ui.button("History").clicked() {
match read_all_history() {
match read_all_history(CONFIG.app.history_dir.clone().unwrap()) {
Ok(history) => self.history = history,
Err(error) => {
self.toasts.error(error.to_string());

@ -4,6 +4,7 @@ use once_cell::sync::Lazy;
use ron::Value;
use serde::Deserialize;
#[cfg(feature = "gui")]
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
let config = fs::read_to_string("./config.ron").expect("Failed to read config.ron");

@ -4,7 +4,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::fs;
use crate::{ai::Params, config::CONFIG};
use crate::ai::Params;
#[derive(Serialize, Deserialize, Clone)]
pub struct HistoryEntry {
@ -14,11 +14,8 @@ pub struct HistoryEntry {
pub result: String,
}
pub fn write_history(entry: HistoryEntry) -> Result<()> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
fs::create_dir_all(history_dir)?;
pub fn write_history(entry: HistoryEntry, history_dir: String) -> Result<()> {
fs::create_dir_all(&history_dir)?;
let json = serde_json::to_string(&entry)?;
@ -30,9 +27,7 @@ pub fn write_history(entry: HistoryEntry) -> Result<()> {
Ok(())
}
pub fn read_all_history() -> Result<Vec<HistoryEntry>> {
// should be safe to unwrap as this function will never be called without it
let history_dir = CONFIG.app.history_dir.as_ref().unwrap();
pub fn read_all_history(history_dir: String) -> Result<Vec<HistoryEntry>> {
let (tx, rx) = flume::unbounded();
fs::read_dir(history_dir)

@ -1,15 +1,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod ai;
pub mod ai;
mod app;
mod config;
mod history;
pub mod config;
pub mod history;
use once_cell::sync::Lazy;
use crate::app::App;
use crate::config::CONFIG;
#[cfg(feature = "gui")]
#[tokio::main]
async fn main() {
// Forces once_cell to init the config