Skip to content

feat: add option to set environment variables for edit command #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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"
Expand Down
32 changes: 32 additions & 0 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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<String, String> = 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(())
Expand Down
3 changes: 3 additions & 0 deletions src/config/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Code {
pub editor: String,
#[serde(rename(serialize = "editor-args"), alias = "editor-args", default)]
pub editor_args: Option<Vec<String>>,
#[serde(rename(serialize = "editor-envs"), alias = "editor-envs", default)]
pub editor_envs: Option<Vec<String>>,
#[serde(default, skip_serializing)]
pub edit_code_marker: bool,
#[serde(default, skip_serializing)]
Expand Down Expand Up @@ -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(),
Expand Down