diff --git a/README.md b/README.md index 39c61dc..5a6ad7f 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ To configure leetcode-cli, create a file at `~/.leetcode/leetcode.toml`): editor = 'emacs' # Optional parameter editor_args = ['-nw'] +# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ]) +editor_envs = [] lang = 'rust' edit_code_marker = false start_marker = "" @@ -104,6 +106,8 @@ scripts = 'scripts' editor = 'emacs' # Optional parameter editor_args = ['-nw'] +# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ]) +editor_envs = [] lang = 'rust' edit_code_marker = true start_marker = "start_marker" @@ -190,7 +194,7 @@ Some linting tools/lsps will throw errors unless the necessary libraries are imp ```toml [code] -inject_before = ["#include", "using namespace std;"] inject_after = ["int main() {\n Solution solution;\n\n}"] ``` diff --git a/src/cache/mod.rs b/src/cache/mod.rs index fc3da47..169c18b 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -115,7 +115,7 @@ impl Cache { let p: Problem = problems.filter(fid.eq(rfid)).first(&mut self.conn()?)?; if p.category != "algorithms" { return Err(Error::FeatureError( - "Not support database and shell questions for now".to_string(), + "No support for database and shell questions yet".to_string(), )); } @@ -129,7 +129,7 @@ impl Cache { .first(&mut self.conn()?)?; if p.category != "algorithms" { return Err(Error::FeatureError( - "Not support database and shell questions for now".to_string(), + "No support for database and shell questions yet".to_string(), )); } Ok(p.fid) @@ -174,7 +174,7 @@ impl Cache { if target.category != "algorithms" { return Err(Error::FeatureError( - "Not support database and shell questions for now".to_string(), + "No support for database and shell questions yet".to_string(), )); } @@ -255,7 +255,7 @@ impl Cache { rfid: i32, test_case: Option, ) -> Result<(HashMap<&'static str, String>, [String; 2]), Error> { - trace!("pre run code..."); + trace!("pre-run code..."); use crate::helper::code_path; use std::fs::File; use std::io::Read; diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 73a9878..9f42efe 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -3,6 +3,7 @@ use super::Command; use crate::Error; use async_trait::async_trait; use clap::{Arg, ArgMatches, Command as ClapCommand}; +use std::collections::HashMap; /// Abstract `edit` command /// @@ -161,8 +162,39 @@ impl Command for EditCommand { args.extend_from_slice(&editor_args); } + // Set environment variables for editor + // + // for example: + // + // ```toml + // [code] + // editor = "nvim" + // editor_envs = [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ] + // ``` + // + // ```rust + // Command::new("nvim").envs(&[ ("XDG_DATA_HOME", "..."), ("XDG_CONFIG_HOME", "..."), ("XDG_STATE_HOME", "..."), ]); + // ``` + let mut envs: HashMap = Default::default(); + if let Some(editor_envs) = &conf.code.editor_envs { + for env in editor_envs.iter() { + let parts: Vec<&str> = env.split('=').collect(); + if parts.len() == 2 { + let name = parts[0].trim(); + let value = parts[1].trim(); + envs.insert(name.to_string(), value.to_string()); + } else { + return Err(crate::Error::FeatureError(format!( + "Invalid editor environment variable: {}", + &env + ))); + } + } + } + args.push(path); std::process::Command::new(conf.code.editor) + .envs(envs) .args(args) .status()?; Ok(()) diff --git a/src/config/code.rs b/src/config/code.rs index f8e6cea..b842657 100644 --- a/src/config/code.rs +++ b/src/config/code.rs @@ -16,6 +16,8 @@ pub struct Code { pub editor: String, #[serde(rename(serialize = "editor-args"), alias = "editor-args", default)] pub editor_args: Option>, + #[serde(rename(serialize = "editor-envs"), alias = "editor-envs", default)] + pub editor_envs: Option>, #[serde(default, skip_serializing)] pub edit_code_marker: bool, #[serde(default, skip_serializing)] @@ -44,6 +46,7 @@ impl Default for Code { Self { editor: "vim".into(), editor_args: None, + editor_envs: None, edit_code_marker: false, start_marker: "".into(), end_marker: "".into(),