Skip to content

Commit

Permalink
added functionality for setting line offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Nudelmeister committed Nov 9, 2020
1 parent e4417f5 commit 25d8424
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ pub struct Config {
#[structopt(long, value_name = "PAD", default_value = "2")]
pub line_pad: u32,

/// Line number offset
#[structopt(long, value_name = "OFFSET", default_value = "1")]
pub line_offset: u32,

/// List all themes.
#[structopt(long)]
pub list_themes: bool,
Expand Down Expand Up @@ -231,7 +235,8 @@ impl Config {
.window_controls(!self.no_window_controls)
.shadow_adder(self.get_shadow_adder()?)
.tab_width(self.tab_width)
.highlight_lines(self.highlight_lines.clone().unwrap_or_default());
.highlight_lines(self.highlight_lines.clone().unwrap_or_default())
.line_offset(self.line_offset);

Ok(formatter.build()?)
}
Expand Down
20 changes: 18 additions & 2 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct ImageFormatter {
shadow_adder: Option<ShadowAdder>,
/// Tab width
tab_width: u8,
/// Line Offset
line_offset: u32,
}

#[derive(Default)]
Expand All @@ -56,6 +58,8 @@ pub struct ImageFormatterBuilder<S> {
shadow_adder: Option<ShadowAdder>,
/// Tab width
tab_width: u8,
/// Line Offset
line_offset: u32,
}

// FIXME: cannot use `ImageFormatterBuilder::new().build()` bacuse cannot infer type for `S`
Expand All @@ -77,6 +81,12 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
self
}

/// Set Line offset
pub fn line_offset(mut self, offset: u32) -> Self {
self.line_offset = offset;
self
}

/// Set the pad between lines
pub fn line_pad(mut self, pad: u32) -> Self {
self.line_pad = pad;
Expand Down Expand Up @@ -140,6 +150,7 @@ impl<S: AsRef<str> + Default> ImageFormatterBuilder<S> {
tab_width: self.tab_width,
code_pad_top,
font,
line_offset: self.line_offset,
})
}
}
Expand Down Expand Up @@ -227,7 +238,11 @@ impl ImageFormatter {
*i = (*i).saturating_sub(20);
}
for i in 0..=lineno {
let line_mumber = format!("{:>width$}", i + 1, width = self.line_number_chars as usize);
let line_mumber = format!(
"{:>width$}",
i + self.line_offset,
width = self.line_number_chars as usize
);
self.font.draw_text_mut(
image,
color,
Expand Down Expand Up @@ -259,7 +274,8 @@ impl ImageFormatter {
// TODO: use &T instead of &mut T ?
pub fn format(&mut self, v: &[Vec<(Style, &str)>], theme: &Theme) -> DynamicImage {
if self.line_number {
self.line_number_chars = ((v.len() as f32).log10() + 1.0).floor() as u32;
self.line_number_chars =
(((v.len() + self.line_offset as usize) as f32).log10() + 1.0).floor() as u32;
} else {
self.line_number_chars = 0;
self.line_number_pad = 0;
Expand Down

0 comments on commit 25d8424

Please sign in to comment.