Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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 all 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
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -190,7 +194,7 @@ Some linting tools/lsps will throw errors unless the necessary libraries are imp

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

Expand Down
8 changes: 4 additions & 4 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand All @@ -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)
Expand Down Expand Up @@ -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(),
));
}

Expand Down Expand Up @@ -255,7 +255,7 @@ impl Cache {
rfid: i32,
test_case: Option<String>,
) -> 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;
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