added functionality for setting line offset

This commit is contained in:
Leon Stichternath 2020-11-09 14:24:07 +01:00
parent e4417f5e06
commit 25d8424175
2 changed files with 24 additions and 3 deletions

View File

@ -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,
@ -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()?)
}

View File

@ -36,6 +36,8 @@ pub struct ImageFormatter {
shadow_adder: Option<ShadowAdder>,
/// Tab width
tab_width: u8,
/// Line Offset
line_offset: u32,
}
#[derive(Default)]
@ -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`
@ -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;
@ -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,
})
}
}
@ -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,
@ -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;