Skip to content
Snippets Groups Projects
Unverified Commit 0a33bb86 authored by GyDi's avatar GyDi
Browse files

fix: open file with vscode

parent 72875628
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ use anyhow::{anyhow, bail, Context, Result}; ...@@ -2,7 +2,7 @@ use anyhow::{anyhow, bail, Context, Result};
use nanoid::nanoid; 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, process::Command, str::FromStr}; use std::{fs, path::PathBuf, str::FromStr};
/// 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> {
...@@ -82,29 +82,18 @@ pub fn parse_str<T: FromStr>(target: &str, key: &str) -> Option<T> { ...@@ -82,29 +82,18 @@ 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(path: PathBuf) -> Result<()> {
// use vscode first #[cfg(target_os = "macos")]
if let Ok(code) = which::which("code") { let code = "Visual Studio Code";
let mut command = Command::new(&code); #[cfg(not(target_os = "macos"))]
let code = "code";
#[cfg(target_os = "windows")]
{
use std::os::windows::process::CommandExt;
if let Err(err) = command.creation_flags(0x08000000).arg(&path).spawn() {
log::error!(target: "app", "failed to open with VScode `{err}`");
open::that(path)?;
}
}
#[cfg(not(target_os = "windows"))] // use vscode first
if let Err(err) = command.arg(&path).spawn() { if let Err(err) = open::with(&path, code) {
log::error!(target: "app", "failed to open with VScode `{err}`"); log::error!(target: "app", "failed to open file with VScode `{err}`");
open::that(path)?; // default open
} open::that(path)?;
return Ok(());
} }
open::that(path)?;
Ok(()) Ok(())
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment