Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: allow for unlimited environment variables
  • Loading branch information
Subjective committed Sep 8, 2023
commit 7b3047fb12860a20342c3f046dd6d337ac732c12
39 changes: 29 additions & 10 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,21 +162,39 @@ impl Command for EditCommand {
args.extend_from_slice(&editor_args);
}

let editor_env = &conf.code.editor_env;
let mut env: Vec<&str> = vec!["", ""];
if !editor_env.is_empty() {
env = editor_env.splitn(2, '=').collect();
if env.len() != 2 {
return Err(crate::Error::FeatureError(
"Invalid environment variable, please check your configuration for errors"
.into(),
));
// 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)
.env(env[0], env[1])
.envs(envs)
.args(args)
.status()?;
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/config/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub struct Code {
pub editor: String,
#[serde(rename(serialize = "editor-args"), alias = "editor-args", default)]
pub editor_args: Option<Vec<String>>,
#[serde(default, skip_serializing)]
pub editor_env: 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 @@ -46,7 +46,7 @@ impl Default for Code {
Self {
editor: "vim".into(),
editor_args: None,
editor_env: "".into(),
editor_envs: None,
edit_code_marker: false,
start_marker: "".into(),
end_marker: "".into(),
Expand Down