Skip to content

Commit 83459a8

Browse files
committed
New option --image-colors to specify colors used in image backends
1 parent 7ccc949 commit 83459a8

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

Diff for: src/onefetch/cli.rs

+22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct Cli {
1616
pub no_bold: bool,
1717
pub image: Option<DynamicImage>,
1818
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
19+
pub image_colors: usize,
1920
pub no_merges: bool,
2021
pub no_color_blocks: bool,
2122
pub number_of_authors: usize,
@@ -115,6 +116,15 @@ impl Cli {
115116
.possible_values(&possible_backends)
116117
.help("Which image BACKEND to use."),
117118
)
119+
.arg(
120+
Arg::with_name("image-colors")
121+
.long("image-colors")
122+
.value_name("NUM")
123+
.takes_value(true)
124+
.max_values(1)
125+
.default_value("16")
126+
.help("NUM of colors (16-256) used in image backend."),
127+
)
118128
.arg(
119129
Arg::with_name("no-merge-commits")
120130
.long("no-merge-commits")
@@ -173,6 +183,17 @@ impl Cli {
173183
None
174184
};
175185

186+
let image_colors = if let Some(value) = matches.value_of("image-colors") {
187+
let colors = usize::from_str(value).unwrap();
188+
if 16 <= colors && colors <= 256 {
189+
colors
190+
} else {
191+
16
192+
}
193+
} else {
194+
16
195+
};
196+
176197
let path = String::from(matches.value_of("input").unwrap());
177198
let ascii_language = if let Some(ascii_language) = matches.value_of("ascii-language") {
178199
Language::from_str(&ascii_language.to_lowercase()).unwrap()
@@ -208,6 +229,7 @@ impl Cli {
208229
no_bold,
209230
image,
210231
image_backend,
232+
image_colors,
211233
no_merges,
212234
no_color_blocks,
213235
number_of_authors,

Diff for: src/onefetch/image_backends/kitty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl KittyBackend {
7777
}
7878

7979
impl super::ImageBackend for KittyBackend {
80-
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
80+
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, _colors: usize) -> String {
8181
let tty_size = unsafe {
8282
let tty_size: winsize = std::mem::zeroed();
8383
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);

Diff for: src/onefetch/image_backends/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod kitty;
66
pub mod sixel;
77

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

1212
#[cfg(not(windows))]

Diff for: src/onefetch/image_backends/sixel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl SixelBackend {
7171
}
7272

7373
impl super::ImageBackend for SixelBackend {
74-
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
74+
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String {
7575
let tty_size = unsafe {
7676
let tty_size: winsize = std::mem::zeroed();
7777
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
@@ -95,7 +95,7 @@ impl super::ImageBackend for SixelBackend {
9595
// reduce the amount of colors using dithering
9696
colorops::dither(
9797
&mut rgba_image,
98-
&NeuQuant::new(10, 128, flat_samples.image_slice().unwrap()),
98+
&NeuQuant::new(10, colors, flat_samples.image_slice().unwrap()),
9999
);
100100
let rgb_image = ImageBuffer::from_fn(rgba_image.width(), rgba_image.height(), |x, y| {
101101
let rgba_pixel = rgba_image.get_pixel(x, y);

Diff for: src/onefetch/info.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ impl std::fmt::Display for Info {
263263
"{}",
264264
image_backend.add_image(
265265
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
266-
custom_image
266+
custom_image,
267+
self.config.image_colors
267268
)
268269
)?;
269270
} else {

0 commit comments

Comments
 (0)