Skip to content

fix issue 80 #81

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 15 commits into from
Aug 14, 2022
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
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ readme = './README.md'

[dependencies]
async-trait = "0.1.56"
tokio = { version = "1", features = ["full"] }
clap = { version = "3.2.8", features = ["cargo"] }
tokio = { version = "1.19.2", features = ["full"] }
clap = { version = "3.2.10", features = ["cargo"] }
colored = "2.0.0"
dirs = "4.0.0"
env_logger = "0.9.0"
keyring = "1.1.2"
keyring = "1.2.0"
log = "0.4.17"
openssl = "0.10.40"
openssl = "0.10.41"
pyo3 = { version = "0.16.5", optional = true }
rand = "0.8.5"
serde = { version = "1.0.138", features = ["derive"] }
serde = { version = "1.0.139", features = ["derive"] }
serde_json = "1.0.82"
toml = "0.5.9"
regex = "1.5.6"
regex = "1.6.0"
scraper = "0.13.0"

[dependencies.diesel]
Expand Down
15 changes: 10 additions & 5 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod sql;
use self::models::*;
use self::schemas::{problems::dsl::*, tags::dsl::*};
use self::sql::*;
use crate::cmds::{CODE_END, CODE_START};
use crate::helper::test_cases_path;
use crate::{cfg, err::Error, plugins::LeetCode};
use colored::Colorize;
Expand Down Expand Up @@ -285,10 +284,16 @@ impl Cache {

File::open(code_path(&p, None)?)?.read_to_string(&mut code)?;

let begin = code.find(CODE_START).unwrap_or(0);
let end = code.find(CODE_END).unwrap_or(code.len());
let code = if let Some(solution) = code.get(begin..end) {
solution.to_string()
let code = if conf.code.edit_code_marker {
let begin = code.find(&conf.code.start_marker);
let end = code.find(&conf.code.end_marker);
if let (Some(l), Some(r)) = (begin, end) {
code.get((l + conf.code.start_marker.len())..r)
.map(|s| s.to_string())
.unwrap_or_else(|| code)
} else {
code
}
} else {
code
};
Expand Down
21 changes: 12 additions & 9 deletions src/cache/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ impl Problem {
_ => "Unknown",
}
}
pub fn desc_comment(&self) -> String {
pub fn desc_comment(&self, conf: &Config) -> String {
let mut res = String::new();
res += format!("// Category: {}\n", self.category).as_str();
res += format!("// Level: {}\n", self.display_level(),).as_str();
res += format!("// Percent: {}%\n\n", self.percent).as_str();
let comment_leading = &conf.code.comment_leading;
res += format!("{} Category: {}\n", comment_leading, self.category).as_str();
res += format!("{} Level: {}\n", comment_leading, self.display_level(),).as_str();
res += format!("{} Percent: {}%\n\n", comment_leading, self.percent).as_str();

res + "\n"
}
Expand Down Expand Up @@ -148,13 +149,13 @@ impl Question {
self.content.render()
}

pub fn desc_comment(&self) -> String {
pub fn desc_comment(&self, conf: &Config) -> String {
let desc = self.content.render();

let mut res = desc
.lines()
.fold("/*\n".to_string(), |acc, e| acc + " * " + e + "\n");
res += " */\n";
let mut res = desc.lines().fold("\n".to_string(), |acc, e| {
acc + " " + conf.code.comment_leading.as_str() + " " + e + "\n"
});
res += " \n";

res
}
Expand Down Expand Up @@ -481,7 +482,9 @@ impl std::fmt::Display for VerifyResult {
}
}

use crate::Config;
use verify::*;

mod verify {
use super::super::parser::ssr;
use serde::Deserialize;
Expand Down
35 changes: 27 additions & 8 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::Error;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::PathBuf};

const DEFAULT_CONFIG: &str = r##"
pub const DEFAULT_CONFIG: &str = r##"
# usually you don't wanna change those
[sys]
categories = [
Expand Down Expand Up @@ -57,6 +57,12 @@ favorite_delete = "https://leetcode.com/list/api/questions/$hash/$id"
[code]
editor = "vim"
lang = "rust"
edit_code_marker = false
comment_problem_desc = false
comment_leading = "///"
start_marker = "@lc code=start"
end_marker = "@lc code=start"
test = true
pick = "${fid}.${slug}"
submission = "${fid}.${slug}.${sid}.${ac}"

Expand All @@ -66,10 +72,10 @@ session = ""

[storage]
root = "~/.leetcode"
cache = "Problems"
scripts = "scripts"
# absolutely path for the code, other use root as parent dir
code = "code"
# absolutely path for the cache, other use root as parent dir
cache = "~/.cache/leetcode"
"##;

/// Sync with `~/.leetcode/config.toml`
Expand Down Expand Up @@ -134,6 +140,12 @@ pub struct Code {
pub editor: String,
#[serde(rename(serialize = "editor-args", deserialize = "editor-args"))]
pub editor_args: Option<Vec<String>>,
pub edit_code_marker: bool,
pub start_marker: String,
pub end_marker: String,
pub comment_problem_desc: bool,
pub comment_leading: String,
pub test: bool,
pub lang: String,
pub pick: String,
pub submission: String,
Expand Down Expand Up @@ -163,16 +175,23 @@ impl Storage {

/// get cache path
pub fn cache(&self) -> Result<String, crate::Error> {
let root = &self.root()?;
Ok(PathBuf::from(root)
.join(&self.cache)
let home = dirs::home_dir()
.ok_or(Error::NoneError)?
.to_string_lossy()
.to_string())
.to_string();
let path = PathBuf::from(self.cache.replace('~', &home));
if !path.is_dir() {
info!("Generate cache dir at {:?}.", &path);
fs::DirBuilder::new().recursive(true).create(&path)?;
}

Ok(path.join("Problems").to_string_lossy().to_string())
}

/// get code path
pub fn code(&self) -> Result<String, crate::Error> {
let p = PathBuf::from(&self.code);
let root = &self.root()?;
let p = PathBuf::from(root).join(&self.code);
if !PathBuf::from(&p).exists() {
fs::create_dir(&p)?
}
Expand Down
48 changes: 28 additions & 20 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ use clap::{Arg, ArgMatches, Command as ClapCommand};
/// ```
pub struct EditCommand;

pub const CODE_START: &str = r#"// @lc code=start"#;
pub const CODE_END: &str = r#"// @lc code=end"#;

#[async_trait]
impl Command for EditCommand {
/// `edit` usage
Expand All @@ -45,13 +42,6 @@ impl Command for EditCommand {
.required(true)
.help("question id"),
)
.arg(
Arg::with_name("test")
.long("test")
.short('t')
.required(false)
.help("write test file"),
)
}

/// `edit` handler
Expand All @@ -66,16 +56,16 @@ impl Command for EditCommand {
let problem = cache.get_problem(id)?;
let mut conf = cache.to_owned().0.conf;

let test_flag = m.contains_id("test");
let test_flag = conf.code.test;

let p_desc_comment = problem.desc_comment();
let p_desc_comment = problem.desc_comment(&conf);
// condition language
if m.contains_id("lang") {
conf.code.lang = m.value_of("lang").ok_or(Error::NoneError)?.to_string();
conf.sync()?;
}

let lang = conf.code.lang;
let lang = &conf.code.lang;
let path = crate::helper::code_path(&problem, Some(lang.to_owned()))?;

if !Path::new(&path).exists() {
Expand All @@ -87,22 +77,40 @@ impl Command for EditCommand {
let question: Question = qr?;

let mut file_code = File::create(&path)?;
let question_desc = question.desc_comment() + "\n";
let question_desc = question.desc_comment(&conf) + "\n";

let test_path = crate::helper::test_cases_path(&problem)?;
let mut file_tests = File::create(&test_path)?;

let mut flag = false;
for d in question.defs.0 {
if d.value == lang {
if d.value == *lang {
flag = true;
file_code.write_all(p_desc_comment.as_bytes())?;
file_code.write_all(question_desc.as_bytes())?;
file_code.write_all((CODE_START.to_string() + "\n").as_bytes())?;
if conf.code.comment_problem_desc {
file_code.write_all(p_desc_comment.as_bytes())?;
file_code.write_all(question_desc.as_bytes())?;
}
if conf.code.edit_code_marker {
file_code.write_all(
(conf.code.comment_leading.clone()
+ " "
+ &conf.code.start_marker
+ "\n")
.as_bytes(),
)?;
}
file_code.write_all((d.code.to_string() + "\n").as_bytes())?;
file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?;
if conf.code.edit_code_marker {
file_code.write_all(
(conf.code.comment_leading.clone()
+ " "
+ &conf.code.end_marker
+ "\n")
.as_bytes(),
)?;
}

if test_flag {
let mut file_tests = File::create(&test_path)?;
file_tests.write_all(question.all_cases.as_bytes())?;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/cmds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ pub use list::ListCommand;
pub use pick::PickCommand;
pub use stat::StatCommand;
pub use test::TestCommand;

pub use edit::{CODE_END, CODE_START};
29 changes: 21 additions & 8 deletions src/err.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Errors in leetcode-cli
use crate::cfg::{root, DEFAULT_CONFIG};
use crate::cmds::{Command, DataCommand};
use colored::Colorize;
use std::fmt;
Expand Down Expand Up @@ -98,19 +99,31 @@ impl std::convert::From<serde_json::error::Error> for Error {

// toml
impl std::convert::From<toml::de::Error> for Error {
fn from(err: toml::de::Error) -> Self {
Error::ParseError(format!(
"{}, {}{}{}{}{}{}{}{}",
err,
fn from(_err: toml::de::Error) -> Self {
let conf = root().unwrap().join("leetcode_tmp.toml");
std::fs::write(&conf, &DEFAULT_CONFIG[1..]).unwrap();
#[cfg(debug_assertions)]
let err_msg = format!(
"{}, {}{}{}{}{}{}",
_err,
"Parse config file failed, ",
"leetcode-cli has just generated a new leetcode.toml at ",
"~/.leetcode/leetcode_tmp.toml,".green().bold().underline(),
" the current one at ",
"~/.leetcode/leetcode.toml".yellow().bold().underline(),
"seems missing some keys, please compare to ",
"the tmp one and add them up : )\n",
".",
))
" seems missing some keys, Please compare the new file and add the missing keys.\n",
);
#[cfg(not(debug_assertions))]
let err_msg = format!(
"{}{}{}{}{}{}",
"Parse config file failed, ",
"leetcode-cli has just generated a new leetcode.toml at ",
"~/.leetcode/leetcode_tmp.toml,".green().bold().underline(),
" the current one at ",
"~/.leetcode/leetcode.toml".yellow().bold().underline(),
" seems missing some keys, Please compare the new file and add the missing keys.\n",
);
Error::ParseError(err_msg)
}
}

Expand Down
57 changes: 51 additions & 6 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,67 @@ mod filter {
}
}

pub fn superscript(n: u8) -> String {
match n {
x if x >= 10 => format!("{}{}", superscript(n / 10), superscript(n % 10)),
0 => "⁰".to_string(),
1 => "¹".to_string(),
2 => "²".to_string(),
3 => "³".to_string(),
4 => "⁴".to_string(),
5 => "⁵".to_string(),
6 => "⁶".to_string(),
7 => "⁷".to_string(),
8 => "⁸".to_string(),
9 => "⁹".to_string(),
_ => n.to_string(),
}
}

pub fn subscript(n: u8) -> String {
match n {
x if x >= 10 => format!("{}{}", subscript(n / 10), subscript(n % 10)),
0 => "₀".to_string(),
1 => "₁".to_string(),
2 => "₂".to_string(),
3 => "₃".to_string(),
4 => "₄".to_string(),
5 => "₅".to_string(),
6 => "₆".to_string(),
7 => "₇".to_string(),
8 => "₈".to_string(),
9 => "₉".to_string(),
_ => n.to_string(),
}
}

/// Render html to command-line
mod html {
use crate::helper::{subscript, superscript};
use regex::Captures;
use scraper::Html;

/// Html render plugin
pub trait HTML {
fn render(&self) -> String;
}

impl HTML for String {
fn render(&self) -> String {
let rep = self
.replace(r#"</sup>"#, "")
.replace(r#"<sup>"#, "^")
.replace(r#"</sub>"#, "")
.replace(r#"<sub>"#, "_");
let frag = Html::parse_fragment(rep.as_str());
let sup_re = regex::Regex::new(r"<sup>(?P<num>[0-9]*)</sup>").unwrap();
let sub_re = regex::Regex::new(r"<sub>(?P<num>[0-9]*)</sub>").unwrap();

let res = sup_re.replace_all(self, |cap: &Captures| {
let num: u8 = cap["num"].to_string().parse().unwrap();
superscript(num)
});

let res = sub_re.replace_all(&res, |cap: &Captures| {
let num: u8 = cap["num"].to_string().parse().unwrap();
subscript(num)
});

let frag = Html::parse_fragment(&res);

let res = frag
.root_element()
Expand Down