-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.rs
464 lines (380 loc) · 14.5 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use tiny_skia::{Color, GradientStop};
use crate::{
snapshot::{ascii_snapshot::ASCIISnapshot, image_snapshot::ImageSnapshot},
themes::get_theme,
utils::color::RgbaColor,
};
pub const DEFAULT_WINDOW_MARGIN: f32 = 82.;
#[derive(Clone, Serialize, Debug)]
#[serde(untagged)]
pub enum DimensionValue {
Num(f32),
Max,
}
impl<'de> Deserialize<'de> for DimensionValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum AnyType {
Num(f32),
Max(String),
}
Ok(match AnyType::deserialize(deserializer)? {
AnyType::Num(num) => DimensionValue::Num(num),
AnyType::Max(max) if max == "max" => DimensionValue::Max,
_ => {
return Err(serde::de::Error::custom(
"The value of DimensionValue should be a number or 'max'",
))
}
})
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Point<T> {
pub x: T,
pub y: T,
}
pub type GradientPoint = Point<DimensionValue>;
impl Point<DimensionValue> {
pub fn into_f32_point(&self, pixmap_width: f32, pixmap_height: f32) -> Point<f32> {
let x = match self.x {
DimensionValue::Num(num) => num,
DimensionValue::Max => pixmap_width,
};
let y = match self.y {
DimensionValue::Num(num) => num,
DimensionValue::Max => pixmap_height,
};
Point { x, y }
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct LinearGradientStop {
position: f32,
color: String,
}
impl LinearGradientStop {
pub fn new(position: f32, color: &str) -> Self {
if position < 0. || position > 1. {
panic!("The position of the gradient stop should be in the range of 0.0 to 1.0");
}
LinearGradientStop {
position,
color: color.to_string(),
}
}
}
impl From<LinearGradientStop> for GradientStop {
fn from(stop: LinearGradientStop) -> Self {
let rgba_color: RgbaColor = stop.color.as_str().into();
let color: Color = rgba_color.into();
GradientStop::new(stop.position, color)
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct LinearGradient {
pub start: GradientPoint,
pub end: GradientPoint,
pub stops: Vec<LinearGradientStop>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum Background {
Solid(String),
Gradient(LinearGradient),
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct TitleConfig {
#[builder(setter(into, strip_option), default = String::from("CaskaydiaCove Nerd Font"))]
pub font_family: String,
#[builder(setter(into), default = String::from("#aca9b2"))]
pub color: String,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct Margin {
#[builder(setter(into, strip_option), default = DEFAULT_WINDOW_MARGIN)]
pub x: f32,
#[builder(setter(into, strip_option), default = DEFAULT_WINDOW_MARGIN)]
pub y: f32,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug, Default)]
pub struct Breadcrumbs {
#[builder(setter(into, strip_option), default = String::from("/"))]
pub separator: String,
#[builder(setter(into, strip_option), default = String::from("CaskaydiaCove Nerd Font"))]
pub font_family: String,
#[builder(setter(into), default = String::from("#80848b"))]
pub color: String,
}
#[derive(Clone, Builder, Default, Serialize, Deserialize, Debug)]
pub struct Border {
#[builder(setter(into), default = String::from("#ffffff30"))]
pub color: String,
#[builder(setter(into), default = 1.)]
pub width: f32,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct Shadow {
#[builder(default = 20.)]
pub radius: f32,
#[builder(setter(into), default = String::from("#0000004d"))]
pub color: String,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct Window {
#[builder(setter(into), default = MarginBuilder::default().build().unwrap())]
pub margin: Margin,
#[builder(setter(into), default = TitleConfigBuilder::default().build().unwrap())]
pub title_config: TitleConfig,
#[builder(setter(into), default = BorderBuilder::default().build().unwrap())]
pub border: Border,
#[builder(default = true)]
pub mac_window_bar: bool,
#[builder(default = ShadowBuilder::default().build().unwrap())]
pub shadow: Shadow,
}
impl WindowBuilder {
pub fn from_window(window: Window) -> WindowBuilder {
WindowBuilder {
margin: Some(window.margin),
title_config: Some(window.title_config),
border: Some(window.border),
mac_window_bar: Some(window.mac_window_bar),
shadow: Some(window.shadow),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum HighlightLine {
Single(u32, String),
Range(u32, u32, String),
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct CommandLineContent {
#[builder(setter(into))]
pub content: String,
#[builder(setter(into))]
pub full_command: String,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
pub struct Code {
#[builder(setter(into))]
pub content: String,
#[builder(default = false)]
pub has_breadcrumbs: bool,
#[builder(setter(into, strip_option), default = None)]
pub start_line_number: Option<u32>,
#[builder(setter(into), default = vec![])]
#[serde(default)]
pub highlight_lines: Vec<HighlightLine>,
/// The `language` will be used to determine the syntax highlighting to use for generating
/// the snapshot.
#[builder(setter(into, strip_option), default = None)]
pub language: Option<String>,
#[builder(setter(into, strip_option), default = None)]
pub file_path: Option<String>,
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug, Default)]
pub struct CommandOutputConfig {
#[builder(setter(into), default = String::from("❯"))]
pub prompt: String,
#[builder(setter(into), default = String::from("CaskaydiaCove Nerd Font"))]
pub font_family: String,
#[builder(setter(into), default = String::from("#F78FB3"))]
pub prompt_color: String,
#[builder(setter(into), default = String::from("#98C379"))]
pub command_color: String,
#[builder(setter(into), default = String::from("#ff0000"))]
pub string_arg_color: String,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum Content {
Code(Code),
CommandOutput(Vec<CommandLineContent>),
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug, Default)]
pub struct CodeConfig {
// #[builder(setter(into), default = String::from(""))]
// #[serde(default)]
// pub content: String,
#[builder(setter(into), default = String::from("CaskaydiaCove Nerd Font"))]
pub font_family: String,
/// Breadcrumbs is a useful and unique feature of CodeSnap, it can help users to understand the
/// code location in the project. If the `has_breadcrumbs` is true, CodeSnap will display the
/// `file_path` on top of the code.
///
/// The code snapshot is different from normal screenshots, it should provide more information
/// about the code, such as the file path, the line number and highlight code line, these
/// information can help users to understand the code better.
#[builder(setter(into, strip_option), default = BreadcrumbsBuilder::default().build().unwrap())]
#[serde(default)]
pub breadcrumbs: Breadcrumbs,
}
/// Draw a watermark below the code, you can use this to add a logo or any other text
/// The watermark is designed as a place for users to provide personalize label
#[derive(Serialize, Deserialize, Clone, Builder, Debug)]
pub struct Watermark {
#[builder(setter(into))]
pub content: String,
#[builder(setter(into), default = String::from("Pacifico"))]
pub font_family: String,
#[builder(setter(into), default = String::from("#ffffff"))]
pub color: String,
}
impl WatermarkBuilder {
pub fn from_watermark(watermark: Option<Watermark>) -> WatermarkBuilder {
watermark
.and_then(|watermark| {
Some(WatermarkBuilder {
content: Some(watermark.content),
font_family: Some(watermark.font_family),
color: Some(watermark.color),
})
})
.unwrap_or(WatermarkBuilder::default())
}
}
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
#[builder(name = "CodeSnap", build_fn(validate = "Self::validate"))]
#[builder(derive(serde::Deserialize, serde::Serialize, Debug))]
pub struct SnapshotConfig {
#[builder(setter(into, strip_option), default = WindowBuilder::default().build().unwrap())]
pub window: Window,
/// The code to be displayed in the snapshot
#[builder(setter(into), default = CommandOutputConfigBuilder::default().build().unwrap())]
pub command_output_config: CommandOutputConfig,
#[builder(setter(into), default = CodeConfigBuilder::default().build().unwrap())]
pub code_config: CodeConfig,
#[builder(setter(into), default = None)]
pub watermark: Option<Watermark>,
#[builder(setter(into))]
pub content: Content,
/// CodeSnap default generate triple size snapshot image,
/// you can use this config to change the scale factor.
#[builder(default = 3)]
#[serde(default = "default_scale_factor")]
pub scale_factor: u8,
/// CodeSnap use Syntect as the syntax highlighting engine, you can provide a custom theme
/// for the snapshot. If the `themes_folder` is provided, CodeSnap will load the theme from
/// the folder, otherwise, CodeSnap will load the default themes.
///
/// Visit https://github.com/trishume/syntect for more detail
#[builder(setter(into, strip_option), default = None)]
pub themes_folder: Option<String>,
/// Load fonts from the fonts_folder to render the code, CodeSnap use fonts which you have
/// installed on your system by default, but you can still provide `fonts_folder` to tell
/// CodeSnap to load extra fonts from the folder.
///
/// This config is useful when you want to develop a tool based on CodeSnap, you can package
/// some fonts with your tool and publish, so that users can use these fonts without installing
/// them manually on their system.
#[builder(setter(into, strip_option), default = None)]
pub fonts_folder: Option<String>,
/// CodeSnap use Syntect as the syntax highlighting engine, you can provide a custom theme
/// for code highlighting and background.
/// The theme is load from the `themes_folder`(if not provided, CodeSnap load the default
/// themes), you can use the theme name to specify the theme you want to use.
///
/// See `themes_folder` config for more detail.
#[builder(setter(into), default = String::from("candy"))]
pub theme: String,
#[builder(setter(into))]
pub background: Background,
#[builder(setter(into), default = String::from("#495162"))]
pub line_number_color: String,
#[builder(setter(into, strip_option), default = None)]
pub title: Option<String>,
}
impl CodeSnap {
fn validate(&self) -> Result<(), String> {
if let Some(scale_factor) = self.scale_factor {
if scale_factor < 1 {
return Err("The scale factor must be greater than 1".to_string());
}
}
Ok(())
}
pub fn from_default_theme() -> Result<CodeSnap, serde_json::Error> {
Self::from_theme("bamboo")
}
pub fn from_theme(theme_name: &str) -> Result<CodeSnap, serde_json::Error> {
let theme = get_theme(theme_name);
Self::from_config(&theme)
}
pub fn from_config(config: &str) -> Result<CodeSnap, serde_json::Error> {
serde_json::from_str::<CodeSnap>(config)
}
pub fn map_code_config<F>(&mut self, f: F) -> anyhow::Result<&mut Self>
where
F: Fn(CodeConfig) -> anyhow::Result<CodeConfig>,
{
self.code_config = Some(f(self
.code_config
.clone()
.unwrap_or(CodeConfigBuilder::default().build()?))?);
Ok(self)
}
pub fn map_code<F>(&mut self, f: F) -> anyhow::Result<&mut Self>
where
F: Fn(Code) -> anyhow::Result<Content>,
{
let content = self.content.clone().unwrap_or(Content::Code(
CodeBuilder::default().content(String::from("")).build()?,
));
let code_content = match content {
Content::Code(code_content) => code_content,
_ => return Ok(self),
};
self.content = Some(f(code_content)?);
Ok(self)
}
pub fn map_window<F>(&mut self, f: F) -> anyhow::Result<&mut Self>
where
F: Fn(Window) -> anyhow::Result<Window>,
{
self.window = Some(f(self
.window
.clone()
.unwrap_or(WindowBuilder::default().build()?))?);
Ok(self)
}
pub fn map_watermark<F>(&mut self, f: F) -> anyhow::Result<&mut Self>
where
F: Fn(Option<Watermark>) -> anyhow::Result<Option<Watermark>>,
{
self.watermark = Some(f(self.watermark.clone().unwrap_or(None))?);
Ok(self)
}
}
impl SnapshotConfig {
/// Create a beautiful code snapshot from the config
pub fn create_snapshot(&self) -> anyhow::Result<ImageSnapshot> {
ImageSnapshot::from_config(self.clone())
}
/// Create a ASCII "snapshot" from the config, the ASCII "snapshot" is a text representation of
/// the code, it's useful when you want to display the code in the terminal or other text-based
/// environment, and because of it's text-based, you can easily copy and paste it to anywhere.
///
/// Through the ASCII "snapshot" is text-based, but it still has some basic styles, and it's
/// will take some important information of code, such as the `line number` and `file path`,
/// these information can help users to understand the code better.
///
/// And If you want to highlighting the ASCII "snapshot", you can try put it into a markdown
/// code block, most markdown renderers will highlight the code block for you.
///
/// The ASCII "snapshot" is really cool, hope you like it!
pub fn create_ascii_snapshot(&self) -> anyhow::Result<ASCIISnapshot> {
ASCIISnapshot::from_config(self.clone())
}
}
fn default_scale_factor() -> u8 {
3
}