Skip to content

Commit

Permalink
fix: ignore the invalid highlight line number
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Nov 5, 2020
1 parent 34eb926 commit e4417f5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl ImageFormatter {
}
}

fn highlight_lines(&self, image: &mut DynamicImage, lines: &[u32]) {
fn highlight_lines<I: IntoIterator<Item = u32>>(&self, image: &mut DynamicImage, lines: I) {
let width = image.width();
let height = self.font.get_font_height() + self.line_pad;
let mut color = image.get_pixel(20, 20);
Expand All @@ -251,7 +251,7 @@ impl ImageFormatter {
let shadow = RgbaImage::from_pixel(width, height, color);

for i in lines {
let y = self.get_line_y(*i - 1);
let y = self.get_line_y(i - 1);
copy_alpha(&shadow, image.as_mut_rgba8().unwrap(), 0, y);
}
}
Expand All @@ -278,7 +278,12 @@ impl ImageFormatter {
let mut image = DynamicImage::ImageRgba8(RgbaImage::from_pixel(size.0, size.1, background));

if !self.highlight_lines.is_empty() {
self.highlight_lines(&mut image, &self.highlight_lines);
let highlight_lines = self
.highlight_lines
.iter()
.cloned()
.filter(|&n| n >= 1 && n <= drawables.max_lineno + 1);
self.highlight_lines(&mut image, highlight_lines);
}
if self.line_number {
self.draw_line_number(&mut image, drawables.max_lineno, foreground);
Expand Down

0 comments on commit e4417f5

Please sign in to comment.