Skip to content

Commit

Permalink
fix: open file, closing #197
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Dec 26, 2023
1 parent 211bc44 commit 4345dc2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions backend/tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
}

#[tauri::command]
pub fn view_profile(index: String) -> CmdResult {
pub fn view_profile(app_handle: tauri::AppHandle, index: String) -> CmdResult {
let file = {
wrap_err!(Config::profiles().latest().get_item(&index))?
.file
Expand All @@ -106,7 +106,7 @@ pub fn view_profile(index: String) -> CmdResult {
ret_err!("the file not found");
}

wrap_err!(help::open_file(path))
wrap_err!(help::open_file(app_handle, path))
}

#[tauri::command]
Expand Down
21 changes: 13 additions & 8 deletions backend/tauri/src/utils/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use nanoid::nanoid;
use serde::{de::DeserializeOwned, Serialize};
use serde_yaml::{Mapping, Value};
use std::{fs, path::PathBuf, str::FromStr};

use tauri::{
api::shell::{open, Program},
Manager,
};
/// read data from yaml as struct T
pub fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
if !path.exists() {
Expand Down Expand Up @@ -81,18 +84,20 @@ pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> {

/// open file
/// 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")]
let code = "Visual Studio Code";
#[cfg(not(target_os = "macos"))]
let code = "code";

// use vscode first
if let Err(err) = open::with(&path, code) {
log::error!(target: "app", "failed to open file with VScode `{err}`");
// default open
open::that(path)?;
}
let _ = match Program::from_str(code) {
Ok(code) => open(&app.shell_scope(), &path.to_string_lossy(), Some(code)),
Err(err) => {
log::error!(target: "app", "Can't find VScode `{err}`");
// default open
open(&app.shell_scope(), &path.to_string_lossy(), None)
}
};

Ok(())
}
Expand Down

0 comments on commit 4345dc2

Please sign in to comment.