mirror of
https://github.com/TxtDot/vigi.git
synced 2025-01-08 09:03:48 +03:00
feat: state, config, setup
This commit is contained in:
parent
e844525abf
commit
c206dbf40a
4 changed files with 152 additions and 2 deletions
|
@ -1,10 +1,20 @@
|
||||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
fs::{self, File},
|
||||||
|
io::Write,
|
||||||
|
sync::Mutex,
|
||||||
|
};
|
||||||
|
|
||||||
use dalet::{Argument, Body, Tag};
|
use dalet::{Argument, Body, Tag};
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
use types::VigiError;
|
mod utils;
|
||||||
|
|
||||||
|
use tauri::Manager;
|
||||||
|
use types::{State, VigiError};
|
||||||
|
use utils::{read_jsonl_tabs, read_or_create_current_tab_index, read_or_create_jsonl};
|
||||||
|
|
||||||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
@ -21,8 +31,65 @@ async fn process_input(input: String) -> Result<Vec<Tag>, VigiError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_handler(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error + 'static>> {
|
||||||
|
println!("---Setup---");
|
||||||
|
|
||||||
|
let app_handle = app.handle();
|
||||||
|
|
||||||
|
let state = app.state::<Mutex<State>>();
|
||||||
|
let mut state = state.lock().unwrap();
|
||||||
|
|
||||||
|
let config_dir = app_handle
|
||||||
|
.path_resolver()
|
||||||
|
.app_config_dir()
|
||||||
|
.unwrap_or(std::path::PathBuf::new().join("config"));
|
||||||
|
|
||||||
|
let local_data_dir = app_handle
|
||||||
|
.path_resolver()
|
||||||
|
.app_local_data_dir()
|
||||||
|
.unwrap_or(std::path::PathBuf::new().join("data"));
|
||||||
|
|
||||||
|
state.cache_dir = app_handle
|
||||||
|
.path_resolver()
|
||||||
|
.app_cache_dir()
|
||||||
|
.unwrap_or(std::path::PathBuf::new().join("cache"));
|
||||||
|
|
||||||
|
println!("Config dir: {}", config_dir.to_string_lossy());
|
||||||
|
println!("Local data dir: {}", local_data_dir.to_string_lossy());
|
||||||
|
println!("Cache dir: {}", state.cache_dir.to_string_lossy());
|
||||||
|
|
||||||
|
println!("Checking config dir");
|
||||||
|
|
||||||
|
// check if config/favorites.jsonl exists
|
||||||
|
if !config_dir.exists() {
|
||||||
|
println!(" Creating config dir");
|
||||||
|
fs::create_dir_all(&config_dir)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.favorites_tabs_path = config_dir.join("favorites.jsonl");
|
||||||
|
state.favorites_tabs = read_or_create_jsonl(&state.favorites_tabs_path);
|
||||||
|
|
||||||
|
println!("Checking local data dir");
|
||||||
|
if !local_data_dir.exists() {
|
||||||
|
println!(" Creating local data dir");
|
||||||
|
fs::create_dir_all(&local_data_dir)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.local_tabs_path = local_data_dir.join("tabs.jsonl");
|
||||||
|
state.tabs = read_or_create_jsonl(&state.local_tabs_path);
|
||||||
|
|
||||||
|
state.current_tab_index_path = local_data_dir.join("current_tab_index");
|
||||||
|
state.current_tab_index = read_or_create_current_tab_index(&state.current_tab_index_path);
|
||||||
|
|
||||||
|
println!("---Setup done---");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
|
.manage(Mutex::new(State::null()))
|
||||||
|
.setup(setup_handler)
|
||||||
.invoke_handler(tauri::generate_handler![process_input])
|
.invoke_handler(tauri::generate_handler![process_input])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
@ -5,3 +7,42 @@ pub enum VigiError {
|
||||||
NetworkError,
|
NetworkError,
|
||||||
ParseError,
|
ParseError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
pub current_tab_index_path: PathBuf,
|
||||||
|
pub local_tabs_path: PathBuf,
|
||||||
|
pub favorites_tabs_path: PathBuf,
|
||||||
|
|
||||||
|
pub cache_dir: PathBuf,
|
||||||
|
|
||||||
|
pub current_tab_index: usize,
|
||||||
|
pub tabs: Vec<Tab>,
|
||||||
|
pub favorites_tabs: Vec<Tab>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn null() -> Self {
|
||||||
|
Self {
|
||||||
|
current_tab_index_path: PathBuf::new(),
|
||||||
|
local_tabs_path: PathBuf::new(),
|
||||||
|
favorites_tabs_path: PathBuf::new(),
|
||||||
|
cache_dir: PathBuf::new(),
|
||||||
|
current_tab_index: 0,
|
||||||
|
tabs: Vec::new(),
|
||||||
|
favorites_tabs: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Tab {
|
||||||
|
ty: TabType,
|
||||||
|
name: String,
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub enum TabType {
|
||||||
|
HomePage,
|
||||||
|
Text,
|
||||||
|
}
|
||||||
|
|
42
src-tauri/src/utils.rs
Normal file
42
src-tauri/src/utils.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
use std::{
|
||||||
|
fs::{self, File},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::types::Tab;
|
||||||
|
|
||||||
|
pub fn read_jsonl_tabs(path: &PathBuf) -> Vec<Tab> {
|
||||||
|
fs::read_to_string(&path)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.lines()
|
||||||
|
.map(|line| serde_json::from_str(line).unwrap())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_or_create_jsonl(path: &PathBuf) -> Vec<Tab> {
|
||||||
|
println!(" Getting tabs from {}", path.to_string_lossy());
|
||||||
|
if path.exists() {
|
||||||
|
read_jsonl_tabs(path)
|
||||||
|
} else {
|
||||||
|
println!(" Creating {}", path.to_string_lossy());
|
||||||
|
File::create(path).unwrap();
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_or_create_current_tab_index(path: &PathBuf) -> usize {
|
||||||
|
println!(
|
||||||
|
" Getting current tab index from {}",
|
||||||
|
path.to_string_lossy()
|
||||||
|
);
|
||||||
|
if path.exists() {
|
||||||
|
fs::read_to_string(path)
|
||||||
|
.unwrap()
|
||||||
|
.parse::<usize>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
} else {
|
||||||
|
println!(" Creating {}", path.to_string_lossy());
|
||||||
|
fs::write(path, "0").unwrap();
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
|
@ -93,7 +93,7 @@ input::placeholder {
|
||||||
|
|
||||||
/* Track */
|
/* Track */
|
||||||
::-webkit-scrollbar-track {
|
::-webkit-scrollbar-track {
|
||||||
background: transparent;
|
@apply bg-transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handle */
|
/* Handle */
|
||||||
|
|
Loading…
Add table
Reference in a new issue