From 3dae83c791ff3651d2892fbffb1b1a53dbff4edf Mon Sep 17 00:00:00 2001 From: Joshua Yin <56745535+Subjective@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:37:51 -0700 Subject: [PATCH 1/4] Add code templates --- src/cmds/edit.rs | 5 +++++ src/config/code.rs | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 9be45ec..3a081ab 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -93,6 +93,11 @@ impl Command for EditCommand { file_code.write_all(p_desc_comment.as_bytes())?; file_code.write_all(question_desc.as_bytes())?; } + if let Some(template) = &conf.code.template { + for line in template { + file_code.write_all((line.to_string() + "\n").as_bytes())?; + } + } if conf.code.edit_code_marker { file_code.write_all( (conf.code.comment_leading.clone() diff --git a/src/config/code.rs b/src/config/code.rs index 0384608..d134422 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 = "template"), alias = "template", default)] + pub template: Option>, #[serde(default, skip_serializing)] pub edit_code_marker: bool, #[serde(default, skip_serializing)] @@ -40,6 +42,7 @@ impl Default for Code { Self { editor: "vim".into(), editor_args: None, + template: None, edit_code_marker: false, start_marker: "".into(), end_marker: "".into(), From c1dd20c5adf0a956356e8919c66c418d4265757b Mon Sep 17 00:00:00 2001 From: Joshua Yin <56745535+Subjective@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:43:37 -0700 Subject: [PATCH 2/4] chore: cleanup messages --- src/cache/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 03e138e..fc3da47 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -356,7 +356,7 @@ impl Cache { ) -> Result { trace!("Exec problem filter —— Test or Submit"); let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?; - trace!("Pre run code result {:#?}, {}, {}", json, url, refer); + trace!("Pre-run code result {:#?}, {}, {}", json, url, refer); let text = self .0 @@ -367,7 +367,7 @@ impl Cache { .await?; let run_res: RunCode = serde_json::from_str(&text).map_err(|e| { - anyhow!("json error: {e}, plz double check your session and csrf config.") + anyhow!("JSON error: {e}, please double check your session and csrf config.") })?; trace!("Run code result {:#?}", run_res); From fd9b3f5453f120c98fc05a2f43f097ca8ba7ac6c Mon Sep 17 00:00:00 2001 From: Joshua Yin <56745535+Subjective@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:06:30 -0700 Subject: [PATCH 3/4] Add "inject_before" and "inject_after" to replace code templates --- src/cmds/edit.rs | 9 +++++++-- src/config/code.rs | 9 ++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 3a081ab..73a9878 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -93,8 +93,8 @@ impl Command for EditCommand { file_code.write_all(p_desc_comment.as_bytes())?; file_code.write_all(question_desc.as_bytes())?; } - if let Some(template) = &conf.code.template { - for line in template { + if let Some(inject_before) = &conf.code.inject_before { + for line in inject_before { file_code.write_all((line.to_string() + "\n").as_bytes())?; } } @@ -117,6 +117,11 @@ impl Command for EditCommand { .as_bytes(), )?; } + if let Some(inject_after) = &conf.code.inject_after { + for line in inject_after { + file_code.write_all((line.to_string() + "\n").as_bytes())?; + } + } if test_flag { let mut file_tests = File::create(&test_path)?; diff --git a/src/config/code.rs b/src/config/code.rs index d134422..f8e6cea 100644 --- a/src/config/code.rs +++ b/src/config/code.rs @@ -16,14 +16,16 @@ pub struct Code { pub editor: String, #[serde(rename(serialize = "editor-args"), alias = "editor-args", default)] pub editor_args: Option>, - #[serde(rename(serialize = "template"), alias = "template", default)] - pub template: Option>, #[serde(default, skip_serializing)] pub edit_code_marker: bool, #[serde(default, skip_serializing)] pub start_marker: String, #[serde(default, skip_serializing)] pub end_marker: String, + #[serde(rename(serialize = "inject_before"), alias = "inject_before", default)] + pub inject_before: Option>, + #[serde(rename(serialize = "inject_after"), alias = "inject_after", default)] + pub inject_after: Option>, #[serde(default, skip_serializing)] pub comment_problem_desc: bool, #[serde(default, skip_serializing)] @@ -42,10 +44,11 @@ impl Default for Code { Self { editor: "vim".into(), editor_args: None, - template: None, edit_code_marker: false, start_marker: "".into(), end_marker: "".into(), + inject_before: None, + inject_after: None, comment_problem_desc: false, comment_leading: "".into(), test: true, From 666a1c965c417570898e5336a402bce3cc5ce167 Mon Sep 17 00:00:00 2001 From: Joshua Yin <56745535+Subjective@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:46:05 -0700 Subject: [PATCH 4/4] docs: Update README.md for `inject_before` and `inject_after` --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c80ae9d..39c61dc 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ SUBCOMMANDS: ## Example -For example, given this config (could be found at `~/.leetcode/leetcode.toml`): +To configure leetcode-cli, create a file at `~/.leetcode/leetcode.toml`): ```toml [code] @@ -186,6 +186,14 @@ pub fn three_sum(nums: Vec) -> Vec> {
+Some linting tools/lsps will throw errors unless the necessary libraries are imported. leetcode-cli can generate this boilerplate automatically if the `inject_before` key is set. Similarly, if you want to test out your code locally, you can automate that with `inject_after`. For c++ this might look something like: + +```toml +[code] +inject_before = ["#includepick ```sh 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