Skip to content

Commit 0322356

Browse files
authored
feat: add option to set environment variables for edit command (clearloop#133)
* feat: add option to configure environment variable for edit command * feat: allow for unlimited environment variables * Update README for Optional environment variables * docs: fix inject_before example in README * chore: fix grammar/typos
1 parent 68d269c commit 0322356

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ To configure leetcode-cli, create a file at `~/.leetcode/leetcode.toml`):
7575
editor = 'emacs'
7676
# Optional parameter
7777
editor_args = ['-nw']
78+
# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ])
79+
editor_envs = []
7880
lang = 'rust'
7981
edit_code_marker = false
8082
start_marker = ""
@@ -104,6 +106,8 @@ scripts = 'scripts'
104106
editor = 'emacs'
105107
# Optional parameter
106108
editor_args = ['-nw']
109+
# Optional environment variables (ex. [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ])
110+
editor_envs = []
107111
lang = 'rust'
108112
edit_code_marker = true
109113
start_marker = "start_marker"
@@ -190,7 +194,7 @@ Some linting tools/lsps will throw errors unless the necessary libraries are imp
190194

191195
```toml
192196
[code]
193-
inject_before = ["#include<bits/stdc++.h", "using namespace std;"]
197+
inject_before = ["#include<bits/stdc++.h>", "using namespace std;"]
194198
inject_after = ["int main() {\n Solution solution;\n\n}"]
195199
```
196200

src/cache/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Cache {
115115
let p: Problem = problems.filter(fid.eq(rfid)).first(&mut self.conn()?)?;
116116
if p.category != "algorithms" {
117117
return Err(Error::FeatureError(
118-
"Not support database and shell questions for now".to_string(),
118+
"No support for database and shell questions yet".to_string(),
119119
));
120120
}
121121

@@ -129,7 +129,7 @@ impl Cache {
129129
.first(&mut self.conn()?)?;
130130
if p.category != "algorithms" {
131131
return Err(Error::FeatureError(
132-
"Not support database and shell questions for now".to_string(),
132+
"No support for database and shell questions yet".to_string(),
133133
));
134134
}
135135
Ok(p.fid)
@@ -174,7 +174,7 @@ impl Cache {
174174

175175
if target.category != "algorithms" {
176176
return Err(Error::FeatureError(
177-
"Not support database and shell questions for now".to_string(),
177+
"No support for database and shell questions yet".to_string(),
178178
));
179179
}
180180

@@ -255,7 +255,7 @@ impl Cache {
255255
rfid: i32,
256256
test_case: Option<String>,
257257
) -> Result<(HashMap<&'static str, String>, [String; 2]), Error> {
258-
trace!("pre run code...");
258+
trace!("pre-run code...");
259259
use crate::helper::code_path;
260260
use std::fs::File;
261261
use std::io::Read;

src/cmds/edit.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::Command;
33
use crate::Error;
44
use async_trait::async_trait;
55
use clap::{Arg, ArgMatches, Command as ClapCommand};
6+
use std::collections::HashMap;
67

78
/// Abstract `edit` command
89
///
@@ -161,8 +162,39 @@ impl Command for EditCommand {
161162
args.extend_from_slice(&editor_args);
162163
}
163164

165+
// Set environment variables for editor
166+
//
167+
// for example:
168+
//
169+
// ```toml
170+
// [code]
171+
// editor = "nvim"
172+
// editor_envs = [ "XDG_DATA_HOME=...", "XDG_CONFIG_HOME=...", "XDG_STATE_HOME=..." ]
173+
// ```
174+
//
175+
// ```rust
176+
// Command::new("nvim").envs(&[ ("XDG_DATA_HOME", "..."), ("XDG_CONFIG_HOME", "..."), ("XDG_STATE_HOME", "..."), ]);
177+
// ```
178+
let mut envs: HashMap<String, String> = Default::default();
179+
if let Some(editor_envs) = &conf.code.editor_envs {
180+
for env in editor_envs.iter() {
181+
let parts: Vec<&str> = env.split('=').collect();
182+
if parts.len() == 2 {
183+
let name = parts[0].trim();
184+
let value = parts[1].trim();
185+
envs.insert(name.to_string(), value.to_string());
186+
} else {
187+
return Err(crate::Error::FeatureError(format!(
188+
"Invalid editor environment variable: {}",
189+
&env
190+
)));
191+
}
192+
}
193+
}
194+
164195
args.push(path);
165196
std::process::Command::new(conf.code.editor)
197+
.envs(envs)
166198
.args(args)
167199
.status()?;
168200
Ok(())

src/config/code.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct Code {
1616
pub editor: String,
1717
#[serde(rename(serialize = "editor-args"), alias = "editor-args", default)]
1818
pub editor_args: Option<Vec<String>>,
19+
#[serde(rename(serialize = "editor-envs"), alias = "editor-envs", default)]
20+
pub editor_envs: Option<Vec<String>>,
1921
#[serde(default, skip_serializing)]
2022
pub edit_code_marker: bool,
2123
#[serde(default, skip_serializing)]
@@ -44,6 +46,7 @@ impl Default for Code {
4446
Self {
4547
editor: "vim".into(),
4648
editor_args: None,
49+
editor_envs: None,
4750
edit_code_marker: false,
4851
start_marker: "".into(),
4952
end_marker: "".into(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy