Skip to content

Commit

Permalink
feat: added a code right pad option and fixed documentation for highl…
Browse files Browse the repository at this point in the history
…ight-lines (#245)
  • Loading branch information
vivaansinghvi07 authored Sep 26, 2024
1 parent 70cc0fd commit f5c0412
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ EOF
Highlight specified line

```bash
silicon main.rs -o main.png --highlight-lines '1; 3-4'
silicon main.rs -o main.png --highlight-lines '1;3-4'
```

Custom the image
Expand Down
9 changes: 7 additions & 2 deletions src/bin/silicon/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct Config {
#[structopt(long, short, value_name = "FONT", parse(from_str = parse_font_str))]
pub font: Option<FontList>,

/// Lines to high light. rg. '1-3; 4'
/// Lines to highlight. eg. '1-3;4'
#[structopt(long, value_name = "LINES", parse(try_from_str = parse_line_range))]
pub highlight_lines: Option<Lines>,

Expand All @@ -126,6 +126,10 @@ pub struct Config {
#[structopt(long, value_name = "PAD", default_value = "2")]
pub line_pad: u32,

/// Add PAD padding to the right of the code.
#[structopt(long, value_name = "PAD", default_value = "25")]
pub code_pad_right: u32,

/// Line number offset
#[structopt(long, value_name = "OFFSET", default_value = "1")]
pub line_offset: u32,
Expand Down Expand Up @@ -281,7 +285,8 @@ impl Config {
.shadow_adder(self.get_shadow_adder()?)
.tab_width(self.tab_width)
.highlight_lines(self.highlight_lines.clone().unwrap_or_default())
.line_offset(self.line_offset);
.line_offset(self.line_offset)
.code_pad_right(self.code_pad_right);

Ok(formatter.build()?)
}
Expand Down
14 changes: 13 additions & 1 deletion src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct ImageFormatter<T> {
/// pad of top of the code area
/// Default: 50
code_pad_top: u32,
/// pad of right of the code area
/// Default: 25
code_pad_right: u32,
/// Title bar padding
/// Default: 15
title_bar_pad: u32,
Expand Down Expand Up @@ -57,6 +60,8 @@ pub struct ImageFormatter<T> {
pub struct ImageFormatterBuilder<S> {
/// Pad between lines
line_pad: u32,
/// Padding to the right of the code
code_pad_right: u32,
/// Show line number
line_number: bool,
/// Font of english character, should be mono space font
Expand Down Expand Up @@ -108,6 +113,12 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
self.line_pad = pad;
self
}

/// Set the pad on the right of the screen
pub fn code_pad_right(mut self, pad: u32) -> Self {
self.code_pad_right = pad;
self
}

/// Set the font
pub fn font(mut self, fonts: Vec<(S, f32)>) -> Self {
Expand Down Expand Up @@ -164,6 +175,7 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
line_pad: self.line_pad,
code_pad: 25,
code_pad_top: if title_bar { 50 } else { 0 },
code_pad_right: self.code_pad_right,
title_bar_pad: 15,
window_controls: self.window_controls,
window_controls_width: 120,
Expand Down Expand Up @@ -205,7 +217,7 @@ impl<T: TextLineDrawer> ImageFormatter<T> {
/// calculate the size of code area
fn get_image_size(&mut self, max_width: u32, lineno: u32) -> (u32, u32) {
(
(max_width + self.code_pad).max(150),
(max_width + self.code_pad_right).max(150),
self.get_line_y(lineno + 1) + self.code_pad,
)
}
Expand Down

0 comments on commit f5c0412

Please sign in to comment.