Skip to content

Commit 7a3c65f

Browse files
authored
[Feat] add diff supports for CLI (#36)
* [Chore] remove unused code * [Feat] add `diff` supports for CLI
1 parent 3adfe18 commit 7a3c65f

File tree

5 files changed

+180
-17
lines changed

5 files changed

+180
-17
lines changed

cli/src/code.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use codesnap::{
1111
utils::clipboard::Clipboard,
1212
};
1313

14-
use crate::{range::Range, CLI, STDIN_CODE_DEFAULT_CHAR};
14+
use crate::{
15+
highlight::{create_highlight_lines_by_opt_range, create_highlight_lines_by_ranges},
16+
range::Range,
17+
CLI, STDIN_CODE_DEFAULT_CHAR,
18+
};
1519

1620
pub fn create_code(cli: &CLI, config_code: Code) -> anyhow::Result<Code> {
1721
let range = Range::from_opt_string(cli.range.clone())?;
@@ -48,20 +52,17 @@ fn create_highlight_lines(cli: &CLI, code_snippet: &str) -> anyhow::Result<Vec<H
4852
return Ok(highlight_lines);
4953
}
5054

51-
let highlight_lines = match cli.highlight_range {
52-
Some(ref highlight_range) => {
53-
let Range(start, end) = Range::from_str(&highlight_range)?.parse_range(code_snippet)?;
54-
55-
vec![HighlightLine::Range(
56-
start as u32,
57-
end as u32,
58-
cli.highlight_range_color.clone(),
59-
)]
60-
}
61-
None => vec![],
62-
};
63-
64-
Ok(highlight_lines)
55+
let highlight_lines = create_highlight_lines_by_opt_range(
56+
&cli.highlight_range,
57+
&cli.highlight_range_color,
58+
code_snippet,
59+
)?;
60+
let delete_highlight_lines =
61+
create_highlight_lines_by_ranges(&cli.delete_line, &cli.delete_line_color, code_snippet)?;
62+
let new_highlight_lines =
63+
create_highlight_lines_by_ranges(&cli.add_line, &cli.add_line_color, code_snippet)?;
64+
65+
Ok([highlight_lines, delete_highlight_lines, new_highlight_lines].concat())
6566
}
6667

6768
fn create_breadcrumbs(cli: &CLI) -> Option<Breadcrumbs> {

cli/src/highlight.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use codesnap::config::HighlightLine;
2+
3+
use crate::range::Range;
4+
5+
pub fn create_highlight_lines_by_range(
6+
range: &str,
7+
highlight_color: &str,
8+
code_snippet: &str,
9+
) -> anyhow::Result<Vec<HighlightLine>> {
10+
let Range(start, end) = Range::from_str(&range)?.parse_range(code_snippet)?;
11+
12+
Ok(vec![HighlightLine::Range(
13+
start as u32,
14+
end as u32,
15+
highlight_color.to_string(),
16+
)])
17+
}
18+
19+
pub fn create_highlight_lines_by_opt_range(
20+
range: &Option<String>,
21+
highlight_color: &str,
22+
code_snippet: &str,
23+
) -> anyhow::Result<Vec<HighlightLine>> {
24+
match range {
25+
Some(ref range) => create_highlight_lines_by_range(range, highlight_color, code_snippet),
26+
None => Ok(vec![]),
27+
}
28+
}
29+
30+
pub fn create_highlight_lines_by_ranges(
31+
ranges: &Vec<String>,
32+
highlight_color: &str,
33+
code_snippet: &str,
34+
) -> anyhow::Result<Vec<HighlightLine>> {
35+
ranges.iter().try_fold(vec![], |mut acc, range| {
36+
acc.extend(create_highlight_lines_by_range(
37+
range,
38+
highlight_color,
39+
code_snippet,
40+
)?);
41+
Ok(acc)
42+
})
43+
}

cli/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod code;
22
mod config;
33
mod egg;
4+
mod highlight;
45
mod logger;
56
mod range;
67
mod watermark;
@@ -101,12 +102,29 @@ struct CLI {
101102
#[arg(long, default_value = "#495162")]
102103
line_number_color: String,
103104

105+
/// Delete lines will be marked with a red line
106+
#[arg(long, short, num_args=0..)]
107+
delete_line: Vec<String>,
108+
109+
/// Delete line color
110+
#[arg(long, default_value = "#ff6b6b30")]
111+
delete_line_color: String,
112+
113+
/// New lines will be marked with a green line
114+
#[arg(long, short, num_args=0..)]
115+
add_line: Vec<String>,
116+
117+
/// New line color
118+
#[arg(long, default_value = "#2ecc7130")]
119+
add_line_color: String,
120+
104121
/// Convenient version of `--raw-highlight-lines` option, you can set the highlight range
105122
/// with a simple syntax, for example, highlight the 3rd to 5th lines:
106123
/// 3:5
107124
#[arg(long)]
108125
highlight_range: Option<String>,
109126

127+
/// Highlight color for the highlighted code lines
110128
#[arg(long, default_value = "#ffffff10")]
111129
highlight_range_color: String,
112130

cli/src/range.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::Lines;
2-
31
use anyhow::bail;
42

53
const RANGE_SEPARATOR: &'static str = ":";

core/src/main.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use config::{CodeBuilder, CodeSnap, HighlightLine, WatermarkBuilder};
2+
3+
mod components;
4+
mod config;
5+
mod edges;
6+
mod preset_background;
7+
mod snapshot;
8+
mod utils;
9+
10+
const CODE_SNIPPET: &'static str = r##"if not vim.g.neovide then
11+
vim.api.nvim_create_autocmd({
12+
"VimLeave",
13+
}, {
14+
pattern = "*",
15+
callback = function()
16+
os.execute("kitty @ set-spacing padding=10 margin=0")
17+
end,
18+
})
19+
20+
vim.api.nvim_create_autocmd({
21+
"VimEnter",
22+
}, {
23+
pattern = "*",
24+
callback = function()
25+
os.execute("kitty @ set-spacing padding=0 margin=0")
26+
end,
27+
})
28+
end"##;
29+
30+
const data: &'static str = r##"
31+
{
32+
"window": {
33+
"macWindowBar": true,
34+
"shadow": 20,
35+
"margin": {
36+
"x": 82,
37+
"y": 82
38+
}
39+
},
40+
"code": {
41+
"fontFamily": "CaskaydiaCove Nerd Font",
42+
"theme": "base16-ocean.light"
43+
},
44+
"watermark": {
45+
"content": "CodeSnap",
46+
"fontFamily": "Pacifico",
47+
"color": "#ff0000"
48+
},
49+
"background": {
50+
"start": {
51+
"x": 0,
52+
"y": 0
53+
},
54+
"end": {
55+
"x": "max",
56+
"y": 0
57+
},
58+
"stops": [
59+
{
60+
"position": 0,
61+
"color": "#6bcba5"
62+
},
63+
{
64+
"position": 1,
65+
"color": "#caf4c2"
66+
}
67+
]
68+
}
69+
}
70+
"##;
71+
72+
pub fn main() -> anyhow::Result<()> {
73+
CodeSnap::default()
74+
.code(
75+
CodeBuilder::default()
76+
.language("haskell")
77+
.content(r#"print "Hello, CodeSnap!""#)
78+
.highlight_lines(vec![HighlightLine::Range(1, 1, "#ff0000".to_string())])
79+
.build()?,
80+
)
81+
.watermark(WatermarkBuilder::default().content("YYM").build()?)
82+
.build()?
83+
.create_snapshot()?
84+
.raw_data()?
85+
.copy()?;
86+
87+
Ok(())
88+
89+
// CodeSnap::default()
90+
// .themes_folder("/Users/zhanhaozhao/repositories/codesnap/assets/themes")
91+
// .code(
92+
// CodeBuilder::default()
93+
// .language("rust")
94+
// .theme("candy")
95+
// .content(CODE_SNIPPET)
96+
// .build()?,
97+
// )
98+
// .watermark(WatermarkBuilder::default().content("CodeSnap").build()?)
99+
// .background(BAMBOO.clone())
100+
// .build()?
101+
// .create_snapshot()?
102+
// .copy()
103+
}

0 commit comments

Comments
 (0)