Skip to content

Commit

Permalink
New option --image-colors to specify colors used in image backends
Browse files Browse the repository at this point in the history
  • Loading branch information
yoichi committed Oct 17, 2020
1 parent 7ccc949 commit 83459a8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Cli {
pub no_bold: bool,
pub image: Option<DynamicImage>,
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
pub image_colors: usize,
pub no_merges: bool,
pub no_color_blocks: bool,
pub number_of_authors: usize,
Expand Down Expand Up @@ -115,6 +116,15 @@ impl Cli {
.possible_values(&possible_backends)
.help("Which image BACKEND to use."),
)
.arg(
Arg::with_name("image-colors")
.long("image-colors")
.value_name("NUM")
.takes_value(true)
.max_values(1)
.default_value("16")
.help("NUM of colors (16-256) used in image backend."),
)
.arg(
Arg::with_name("no-merge-commits")
.long("no-merge-commits")
Expand Down Expand Up @@ -173,6 +183,17 @@ impl Cli {
None
};

let image_colors = if let Some(value) = matches.value_of("image-colors") {
let colors = usize::from_str(value).unwrap();
if 16 <= colors && colors <= 256 {
colors
} else {
16
}
} else {
16
};

let path = String::from(matches.value_of("input").unwrap());
let ascii_language = if let Some(ascii_language) = matches.value_of("ascii-language") {
Language::from_str(&ascii_language.to_lowercase()).unwrap()
Expand Down Expand Up @@ -208,6 +229,7 @@ impl Cli {
no_bold,
image,
image_backend,
image_colors,
no_merges,
no_color_blocks,
number_of_authors,
Expand Down
2 changes: 1 addition & 1 deletion src/onefetch/image_backends/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl KittyBackend {
}

impl super::ImageBackend for KittyBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, _colors: usize) -> String {
let tty_size = unsafe {
let tty_size: winsize = std::mem::zeroed();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
Expand Down
2 changes: 1 addition & 1 deletion src/onefetch/image_backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod kitty;
pub mod sixel;

pub trait ImageBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String;
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String;
}

#[cfg(not(windows))]
Expand Down
4 changes: 2 additions & 2 deletions src/onefetch/image_backends/sixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl SixelBackend {
}

impl super::ImageBackend for SixelBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String {
let tty_size = unsafe {
let tty_size: winsize = std::mem::zeroed();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
Expand All @@ -95,7 +95,7 @@ impl super::ImageBackend for SixelBackend {
// reduce the amount of colors using dithering
colorops::dither(
&mut rgba_image,
&NeuQuant::new(10, 128, flat_samples.image_slice().unwrap()),
&NeuQuant::new(10, colors, flat_samples.image_slice().unwrap()),
);
let rgb_image = ImageBuffer::from_fn(rgba_image.width(), rgba_image.height(), |x, y| {
let rgba_pixel = rgba_image.get_pixel(x, y);
Expand Down
3 changes: 2 additions & 1 deletion src/onefetch/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ impl std::fmt::Display for Info {
"{}",
image_backend.add_image(
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
custom_image
custom_image,
self.config.image_colors
)
)?;
} else {
Expand Down

0 comments on commit 83459a8

Please sign in to comment.