This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
201 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
use crate::clip; | ||
use crate::{clip, setting::Config}; | ||
|
||
// remember to call `.manage(MyState::default())` | ||
#[tauri::command] | ||
#[allow(dead_code)] | ||
pub async fn get_selection_text() -> Result<String, String> { | ||
clip::read_text() | ||
} | ||
|
||
#[tauri::command] | ||
#[allow(dead_code)] | ||
pub async fn write_config(data: String) -> Result<(), String> { | ||
println!("data:{:?}", data); | ||
Config::cover(data); | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
#[allow(dead_code)] | ||
pub async fn read_config() -> Result<Config, String> { | ||
Ok(Config::read()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::path::{Path, PathBuf}; | ||
|
||
use anyhow::Result; | ||
|
||
use tauri_plugin_log::{ | ||
fern::colors::{Color, ColoredLevelConfig}, | ||
LogTarget, | ||
}; | ||
|
||
// ~/.popup-translation | ||
pub fn app_root() -> PathBuf { | ||
tauri::api::path::home_dir() | ||
.unwrap() | ||
.join(".popup-translation") | ||
} | ||
|
||
pub fn exists(path: &Path) -> bool { | ||
Path::new(path).exists() | ||
} | ||
|
||
pub fn create_file(path: &Path) -> Result<std::fs::File> { | ||
if let Some(p) = path.parent() { | ||
std::fs::create_dir_all(p)? | ||
} | ||
std::fs::File::create(path).map_err(Into::into) | ||
} | ||
|
||
fn config_path() -> PathBuf { | ||
app_root().join("config.json") | ||
} | ||
|
||
#[derive(Default, serde::Deserialize, serde::Serialize, Debug)] | ||
pub struct Config { | ||
pub keys: KeyInfo, | ||
} | ||
|
||
#[derive(Default, serde::Deserialize, serde::Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct KeyInfo { | ||
pub chat_gpt: String, | ||
pub youdao: String, | ||
pub google: String, | ||
} | ||
|
||
impl Config { | ||
pub fn read() -> Self { | ||
if !exists(&config_path()) { | ||
log::info!("config.json not found"); | ||
Self::default().write(); | ||
} | ||
|
||
match std::fs::read_to_string(config_path()) { | ||
Ok(v) => serde_json::from_str(&v).unwrap_or_else(|err| { | ||
log::error!("[read] config parse error: {}", err); | ||
Self::default() | ||
}), | ||
Err(err) => { | ||
log::error!("config.json read error: {}", err); | ||
Self::default() | ||
} | ||
} | ||
} | ||
|
||
pub fn write(self) -> Self { | ||
let path = &config_path(); | ||
if !exists(path) { | ||
create_file(path).unwrap(); | ||
log::info!("confg.json created"); | ||
} | ||
if let Ok(v) = serde_json::to_string_pretty(&self) { | ||
std::fs::write(path, v).unwrap_or_else(|err| { | ||
log::error!("config.json write error: {}", err); | ||
Self::default().write(); | ||
}); | ||
} else { | ||
log::error!("config.json serialize error"); | ||
} | ||
|
||
self | ||
} | ||
|
||
pub fn cover(data: String) -> Self { | ||
log::debug!("config.json cover new config: {}", data); | ||
log::debug!( | ||
"config.json cover old config: {}", | ||
serde_json::to_string_pretty(&Self::read()).unwrap() | ||
); | ||
serde_json::from_str(&data) | ||
.unwrap_or_else(|err| { | ||
log::error!("[cover] config parse error: {}", err); | ||
Self::default() | ||
}) | ||
.write() | ||
} | ||
} | ||
|
||
pub fn log_builder() -> tauri_plugin_log::Builder { | ||
tauri_plugin_log::Builder::default() | ||
.targets([ | ||
// LogTarget::LogDir, | ||
// LOG PATH: ~/.popup-translation/log | ||
LogTarget::Folder(app_root().join("log")), | ||
LogTarget::Stdout, | ||
LogTarget::Webview, | ||
]) | ||
.level(log::LevelFilter::Debug) | ||
.with_colors(ColoredLevelConfig { | ||
error: Color::Red, | ||
warn: Color::Yellow, | ||
debug: Color::Blue, | ||
info: Color::BrightGreen, | ||
trace: Color::Cyan, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.