From 5b6a69444ea268ee00c704ae5b34ba6162f77305 Mon Sep 17 00:00:00 2001 From: lencx Date: Sat, 21 Jan 2023 13:25:20 +0800 Subject: [PATCH] chore: fmt --- src-tauri/rustfmt.toml | 16 +++++-- src-tauri/src/app/cmd.rs | 14 ++++-- src-tauri/src/app/menu.rs | 92 ++++++++++++++++++++++++++++--------- src-tauri/src/app/window.rs | 35 ++++++++------ src-tauri/src/conf.rs | 8 +++- src-tauri/src/main.rs | 10 +++- src-tauri/src/utils.rs | 34 ++++++++++---- 7 files changed, 156 insertions(+), 53 deletions(-) diff --git a/src-tauri/rustfmt.toml b/src-tauri/rustfmt.toml index 7c27671..37a4436 100644 --- a/src-tauri/rustfmt.toml +++ b/src-tauri/rustfmt.toml @@ -1,4 +1,14 @@ -edition = "2021" -max_width = 120 +max_width = 100 +hard_tabs = false tab_spaces = 2 -newline_style = "Auto" \ No newline at end of file +newline_style = "Auto" +use_small_heuristics = "Default" +reorder_imports = true +reorder_modules = true +remove_nested_parens = true +edition = "2021" +merge_derives = true +use_try_shorthand = false +use_field_init_shorthand = false +force_explicit_abi = true +# imports_granularity = "Crate" \ No newline at end of file diff --git a/src-tauri/src/app/cmd.rs b/src-tauri/src/app/cmd.rs index 80b0d04..4b87e4f 100644 --- a/src-tauri/src/app/cmd.rs +++ b/src-tauri/src/app/cmd.rs @@ -267,7 +267,9 @@ pub fn download_list(pathname: &str, dir: &str, filename: Option, id: Op #[command] pub async fn sync_prompts(app: AppHandle, time: u64) -> Option> { - let res = utils::get_data(GITHUB_PROMPTS_CSV_URL, Some(&app)).await.unwrap(); + let res = utils::get_data(GITHUB_PROMPTS_CSV_URL, Some(&app)) + .await + .unwrap(); if let Some(v) = res { let data = parse_prompt(v) @@ -289,7 +291,9 @@ pub async fn sync_prompts(app: AppHandle, time: u64) -> Option> let model = utils::chat_root().join("chat.model.json"); let model_cmd = utils::chat_root().join("chat.model.cmd.json"); - let chatgpt_prompts = utils::chat_root().join("cache_model").join("chatgpt_prompts.json"); + let chatgpt_prompts = utils::chat_root() + .join("cache_model") + .join("chatgpt_prompts.json"); if !utils::exists(&model) { fs::write( @@ -304,7 +308,11 @@ pub async fn sync_prompts(app: AppHandle, time: u64) -> Option> } // chatgpt_prompts.json - fs::write(chatgpt_prompts, serde_json::to_string_pretty(&data).unwrap()).unwrap(); + fs::write( + chatgpt_prompts, + serde_json::to_string_pretty(&data).unwrap(), + ) + .unwrap(); let cmd_data = cmd_list(); // chat.model.cmd.json diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index 25a3789..9d7d440 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -4,8 +4,8 @@ use crate::{ utils, }; use tauri::{ - AppHandle, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, SystemTrayMenu, - SystemTrayMenuItem, WindowMenuEvent, + AppHandle, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, + SystemTrayMenu, SystemTrayMenuItem, WindowMenuEvent, }; use tauri_plugin_positioner::{on_tray_event, Position, WindowExt}; @@ -33,7 +33,8 @@ pub fn init() -> Menu { ]), ); - let stay_on_top = CustomMenuItem::new("stay_on_top".to_string(), "Stay On Top").accelerator("CmdOrCtrl+T"); + let stay_on_top = + CustomMenuItem::new("stay_on_top".to_string(), "Stay On Top").accelerator("CmdOrCtrl+T"); let stay_on_top_menu = if chat_conf.stay_on_top { stay_on_top.selected() } else { @@ -98,7 +99,11 @@ pub fn init() -> Menu { } else { theme_light.selected() }) - .add_item(if is_dark { theme_dark.selected() } else { theme_dark }) + .add_item(if is_dark { + theme_dark.selected() + } else { + theme_dark + }) .add_item(if is_system { theme_system.selected() } else { @@ -162,13 +167,21 @@ pub fn init() -> Menu { "View", Menu::new() .add_item(CustomMenuItem::new("go_back".to_string(), "Go Back").accelerator("CmdOrCtrl+Left")) - .add_item(CustomMenuItem::new("go_forward".to_string(), "Go Forward").accelerator("CmdOrCtrl+Right")) - .add_item(CustomMenuItem::new("scroll_top".to_string(), "Scroll to Top of Screen").accelerator("CmdOrCtrl+Up")) .add_item( - CustomMenuItem::new("scroll_bottom".to_string(), "Scroll to Bottom of Screen").accelerator("CmdOrCtrl+Down"), + CustomMenuItem::new("go_forward".to_string(), "Go Forward").accelerator("CmdOrCtrl+Right"), + ) + .add_item( + CustomMenuItem::new("scroll_top".to_string(), "Scroll to Top of Screen") + .accelerator("CmdOrCtrl+Up"), + ) + .add_item( + CustomMenuItem::new("scroll_bottom".to_string(), "Scroll to Bottom of Screen") + .accelerator("CmdOrCtrl+Down"), ) .add_native_item(MenuItem::Separator) - .add_item(CustomMenuItem::new("reload".to_string(), "Refresh the Screen").accelerator("CmdOrCtrl+R")), + .add_item( + CustomMenuItem::new("reload".to_string(), "Refresh the Screen").accelerator("CmdOrCtrl+R"), + ), ); let window_menu = Submenu::new( @@ -183,11 +196,15 @@ pub fn init() -> Menu { let help_menu = Submenu::new( "Help", Menu::new() - .add_item(CustomMenuItem::new("chatgpt_log".to_string(), "ChatGPT Log")) + .add_item(CustomMenuItem::new( + "chatgpt_log".to_string(), + "ChatGPT Log", + )) .add_item(CustomMenuItem::new("update_log".to_string(), "Update Log")) .add_item(CustomMenuItem::new("report_bug".to_string(), "Report Bug")) .add_item( - CustomMenuItem::new("dev_tools".to_string(), "Toggle Developer Tools").accelerator("CmdOrCtrl+Shift+I"), + CustomMenuItem::new("dev_tools".to_string(), "Toggle Developer Tools") + .accelerator("CmdOrCtrl+Shift+I"), ), ); @@ -232,7 +249,10 @@ pub fn menu_handler(event: WindowMenuEvent) { "popup_search" => { let chat_conf = conf::ChatConfJson::get_chat_conf(); let popup_search = !chat_conf.popup_search; - menu_handle.get_item(menu_id).set_selected(popup_search).unwrap(); + menu_handle + .get_item(menu_id) + .set_selected(popup_search) + .unwrap(); ChatConfJson::amend(&serde_json::json!({ "popup_search": popup_search }), None).unwrap(); cmd::window_reload(app.clone(), "core"); cmd::window_reload(app, "tray"); @@ -253,10 +273,16 @@ pub fn menu_handler(event: WindowMenuEvent) { }, ); } - "hide_dock_icon" => ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": true }), Some(app)).unwrap(), + "hide_dock_icon" => { + ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": true }), Some(app)).unwrap() + } "titlebar" => { let chat_conf = conf::ChatConfJson::get_chat_conf(); - ChatConfJson::amend(&serde_json::json!({ "titlebar": !chat_conf.titlebar }), None).unwrap(); + ChatConfJson::amend( + &serde_json::json!({ "titlebar": !chat_conf.titlebar }), + None, + ) + .unwrap(); tauri::api::process::restart(&app.env()); } "system_tray" => { @@ -279,15 +305,24 @@ pub fn menu_handler(event: WindowMenuEvent) { } let auto_update = match menu_id { "update_silent" => { - menu_handle.get_item("update_silent").set_selected(true).unwrap(); + menu_handle + .get_item("update_silent") + .set_selected(true) + .unwrap(); "Silent" } "update_disable" => { - menu_handle.get_item("update_disable").set_selected(true).unwrap(); + menu_handle + .get_item("update_disable") + .set_selected(true) + .unwrap(); "Disable" } _ => { - menu_handle.get_item("update_prompt").set_selected(true).unwrap(); + menu_handle + .get_item("update_prompt") + .set_selected(true) + .unwrap(); "Prompt" } }; @@ -296,7 +331,10 @@ pub fn menu_handler(event: WindowMenuEvent) { "stay_on_top" => { let chat_conf = conf::ChatConfJson::get_chat_conf(); let stay_on_top = !chat_conf.stay_on_top; - menu_handle.get_item(menu_id).set_selected(stay_on_top).unwrap(); + menu_handle + .get_item(menu_id) + .set_selected(stay_on_top) + .unwrap(); win.set_always_on_top(stay_on_top).unwrap(); ChatConfJson::amend(&serde_json::json!({ "stay_on_top": stay_on_top }), None).unwrap(); } @@ -341,10 +379,19 @@ pub fn tray_menu() -> SystemTray { if cfg!(target_os = "macos") { SystemTray::new().with_menu( SystemTrayMenu::new() - .add_item(CustomMenuItem::new("control_center".to_string(), "Control Center")) + .add_item(CustomMenuItem::new( + "control_center".to_string(), + "Control Center", + )) .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("show_dock_icon".to_string(), "Show Dock Icon")) - .add_item(CustomMenuItem::new("hide_dock_icon".to_string(), "Hide Dock Icon")) + .add_item(CustomMenuItem::new( + "show_dock_icon".to_string(), + "Show Dock Icon", + )) + .add_item(CustomMenuItem::new( + "hide_dock_icon".to_string(), + "Hide Dock Icon", + )) .add_item(CustomMenuItem::new("show_core".to_string(), "Show ChatGPT")) .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("quit".to_string(), "Quit ChatGPT")), @@ -352,7 +399,10 @@ pub fn tray_menu() -> SystemTray { } else { SystemTray::new().with_menu( SystemTrayMenu::new() - .add_item(CustomMenuItem::new("control_center".to_string(), "Control Center")) + .add_item(CustomMenuItem::new( + "control_center".to_string(), + "Control Center", + )) .add_item(CustomMenuItem::new("show_core".to_string(), "Show ChatGPT")) .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("quit".to_string(), "Quit ChatGPT")), diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs index 507e633..1b34874 100644 --- a/src-tauri/src/app/window.rs +++ b/src-tauri/src/app/window.rs @@ -31,7 +31,12 @@ pub fn tray_window(handle: &tauri::AppHandle) { }); } -pub fn dalle2_window(handle: &tauri::AppHandle, query: Option, title: Option, is_new: Option) { +pub fn dalle2_window( + handle: &tauri::AppHandle, + query: Option, + title: Option, + is_new: Option, +) { info!("dalle2_query: {:?}", query); let theme = conf::ChatConfJson::theme(); let app = handle.clone(); @@ -57,18 +62,22 @@ pub fn dalle2_window(handle: &tauri::AppHandle, query: Option, title: Op if app.get_window("dalle2").is_none() { tauri::async_runtime::spawn(async move { - WindowBuilder::new(&app, label, WindowUrl::App("https://labs.openai.com".into())) - .title(title.unwrap_or_else(|| "DALL·E 2".to_string())) - .resizable(true) - .fullscreen(false) - .inner_size(800.0, 600.0) - .always_on_top(false) - .theme(theme) - .initialization_script(include_str!("../scripts/core.js")) - .initialization_script(&query) - .initialization_script(include_str!("../scripts/dalle2.js")) - .build() - .unwrap(); + WindowBuilder::new( + &app, + label, + WindowUrl::App("https://labs.openai.com".into()), + ) + .title(title.unwrap_or_else(|| "DALL·E 2".to_string())) + .resizable(true) + .fullscreen(false) + .inner_size(800.0, 600.0) + .always_on_top(false) + .theme(theme) + .initialization_script(include_str!("../scripts/core.js")) + .initialization_script(&query) + .initialization_script(include_str!("../scripts/dalle2.js")) + .build() + .unwrap(); }); } else { let dalle2_win = app.get_window("dalle2").unwrap(); diff --git a/src-tauri/src/conf.rs b/src-tauri/src/conf.rs index 557ca6d..b70d68c 100644 --- a/src-tauri/src/conf.rs +++ b/src-tauri/src/conf.rs @@ -15,7 +15,8 @@ pub const ISSUES_URL: &str = "https://github.com/lencx/ChatGPT/issues"; pub const UPDATE_LOG_URL: &str = "https://github.com/lencx/ChatGPT/blob/main/UPDATE_LOG.md"; pub const AWESOME_URL: &str = "https://github.com/lencx/ChatGPT/blob/main/AWESOME.md"; pub const BUY_COFFEE: &str = "https://www.buymeacoffee.com/lencx"; -pub const GITHUB_PROMPTS_CSV_URL: &str = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"; +pub const GITHUB_PROMPTS_CSV_URL: &str = + "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"; pub const DEFAULT_CHAT_CONF: &str = r#"{ "stay_on_top": false, "auto_update": "Prompt", @@ -148,7 +149,10 @@ impl ChatConfJson { config.insert(k, v); } - fs::write(ChatConfJson::conf_path(), serde_json::to_string_pretty(&config)?)?; + fs::write( + ChatConfJson::conf_path(), + serde_json::to_string_pretty(&config)?, + )?; if let Some(handle) = app { tauri::api::process::restart(&handle.env()); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 40b9072..0468ac8 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,4 +1,7 @@ -#![cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")] +#![cfg_attr( + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" +)] mod app; mod conf; @@ -48,7 +51,10 @@ async fn main() { .build(), ) .plugin(tauri_plugin_positioner::init()) - .plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, None)) + .plugin(tauri_plugin_autostart::init( + MacosLauncher::LaunchAgent, + None, + )) .invoke_handler(tauri::generate_handler![ cmd::drag_window, cmd::fullscreen, diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 9261e56..63cd7dd 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -64,7 +64,11 @@ pub fn open_file(path: PathBuf) { Command::new("open").arg("-R").arg(path).spawn().unwrap(); #[cfg(target_os = "windows")] - Command::new("explorer").arg("/select,").arg(path).spawn().unwrap(); + Command::new("explorer") + .arg("/select,") + .arg(path) + .spawn() + .unwrap(); // https://askubuntu.com/a/31071 #[cfg(target_os = "linux")] @@ -78,12 +82,17 @@ pub fn clear_conf(app: &tauri::AppHandle) { "Path: {}\nAre you sure to clear all ChatGPT configurations? Please backup in advance if necessary!", root.to_string_lossy() ); - tauri::api::dialog::ask(app.get_window("core").as_ref(), "Clear Config", msg, move |is_ok| { - if is_ok { - fs::remove_dir_all(root).unwrap(); - tauri::api::process::restart(&app2.env()); - } - }); + tauri::api::dialog::ask( + app.get_window("core").as_ref(), + "Clear Config", + msg, + move |is_ok| { + if is_ok { + fs::remove_dir_all(root).unwrap(); + tauri::api::process::restart(&app2.env()); + } + }, + ); } pub fn merge(v: &Value, fields: &HashMap) -> Value { @@ -104,7 +113,10 @@ pub fn gen_cmd(name: String) -> String { re.replace_all(&name, "_").to_lowercase() } -pub async fn get_data(url: &str, app: Option<&tauri::AppHandle>) -> Result, reqwest::Error> { +pub async fn get_data( + url: &str, + app: Option<&tauri::AppHandle>, +) -> Result, reqwest::Error> { let res = reqwest::get(url).await?; let is_ok = res.status() == 200; let body = res.text().await?; @@ -222,7 +234,11 @@ pub async fn silent_install(app: AppHandle, update: UpdateResponse) -> } pub fn is_hidden(entry: &walkdir::DirEntry) -> bool { - entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) + entry + .file_name() + .to_str() + .map(|s| s.starts_with('.')) + .unwrap_or(false) } pub fn vec_to_hashmap(