fix: Open File (#56)

This commit is contained in:
Pylogmon 2023-12-04 12:15:12 +08:00 committed by GitHub
parent b7e9d61c72
commit 2cf52f15ab
2 changed files with 15 additions and 9 deletions

View File

@ -85,7 +85,7 @@ pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
} }
#[tauri::command] #[tauri::command]
pub fn view_profile(index: String) -> CmdResult { pub fn view_profile(app_handle: tauri::AppHandle, index: String) -> CmdResult {
let file = { let file = {
wrap_err!(Config::profiles().latest().get_item(&index))? wrap_err!(Config::profiles().latest().get_item(&index))?
.file .file
@ -98,7 +98,7 @@ pub fn view_profile(index: String) -> CmdResult {
ret_err!("the file not found"); ret_err!("the file not found");
} }
wrap_err!(help::open_file(path)) wrap_err!(help::open_file(app_handle, path))
} }
#[tauri::command] #[tauri::command]

View File

@ -3,6 +3,10 @@ use nanoid::nanoid;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use serde_yaml::{Mapping, Value}; use serde_yaml::{Mapping, Value};
use std::{fs, path::PathBuf, str::FromStr}; use std::{fs, path::PathBuf, str::FromStr};
use tauri::{
api::shell::{open, Program},
Manager,
};
/// read data from yaml as struct T /// read data from yaml as struct T
pub fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> { pub fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
@ -81,18 +85,20 @@ pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> {
/// open file /// open file
/// use vscode by default /// use vscode by default
pub fn open_file(path: PathBuf) -> Result<()> { pub fn open_file(app: tauri::AppHandle, path: PathBuf) -> Result<()> {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let code = "Visual Studio Code"; let code = "Visual Studio Code";
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
let code = "code"; let code = "code";
// use vscode first let _ = match Program::from_str(code) {
if let Err(err) = open::with(&path, code) { Ok(code) => open(&app.shell_scope(), &path.to_string_lossy(), Some(code)),
log::error!(target: "app", "failed to open file with VScode `{err}`"); Err(err) => {
// default open log::error!(target: "app", "Can't find VScode `{err}`");
open::that(path)?; // default open
} open(&app.shell_scope(), &path.to_string_lossy(), None)
}
};
Ok(()) Ok(())
} }