Skip to content

Commit c5a1e2c

Browse files
committed
Add image-backend argument
1 parent cb8225f commit c5a1e2c

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Diff for: src/image_backends/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use image::DynamicImage;
22

33
#[cfg(target_os = "linux")]
4-
mod kitty;
4+
pub mod kitty;
55
#[cfg(target_os = "linux")]
6-
mod sixel;
6+
pub mod sixel;
77

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

Diff for: src/info.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use git2::Repository;
88
use image::DynamicImage;
99
use license::Detector;
1010

11-
use crate::image_backends;
11+
use crate::image_backends::ImageBackend;
1212
use crate::language::Language;
1313
use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
1414

@@ -35,6 +35,7 @@ pub struct Info {
3535
disable_fields: InfoFieldOn,
3636
bold_enabled: bool,
3737
custom_image: Option<DynamicImage>,
38+
image_backend: Option<Box<dyn ImageBackend>>
3839
}
3940

4041
impl std::fmt::Display for Info {
@@ -238,11 +239,11 @@ impl std::fmt::Display for Info {
238239
let mut info_lines = buf.lines();
239240

240241
if let Some(custom_image) = &self.custom_image {
241-
if let Some(backend) = image_backends::get_best_backend() {
242+
if let Some(image_backend) = &self.image_backend {
242243
writeln!(
243244
f,
244245
"{}",
245-
backend.add_image(
246+
image_backend.add_image(
246247
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
247248
custom_image
248249
)
@@ -286,6 +287,7 @@ impl Info {
286287
disabled: InfoFieldOn,
287288
bold_flag: bool,
288289
custom_image: Option<DynamicImage>,
290+
image_backend: Option<Box<dyn ImageBackend>>,
289291
no_merges: bool,
290292
) -> Result<Info> {
291293
let repo = Repository::discover(&dir).map_err(|_| Error::NotGitRepo)?;
@@ -326,6 +328,7 @@ impl Info {
326328
disable_fields: disabled,
327329
bold_enabled: bold_flag,
328330
custom_image,
331+
image_backend,
329332
})
330333
}
331334

Diff for: src/main.rs

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod license;
3838
use ascii_art::AsciiArt;
3939
use commit_info::CommitInfo;
4040
use error::Error;
41+
use image_backends::ImageBackend;
4142
use info::Info;
4243
use language::Language;
4344

@@ -192,6 +193,13 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
192193
.takes_value(true)
193194
.help("Sets a custom image to use instead of the ascii logo"),
194195
)
196+
.arg(
197+
Arg::with_name("image-backend")
198+
.long("image-backend")
199+
.takes_value(true)
200+
.possible_values(&["kitty", "sixel"])
201+
.help("Overrides image backend detection"),
202+
)
195203
.arg(
196204
Arg::with_name("no-merges")
197205
.short("m")
@@ -258,6 +266,20 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
258266
} else {
259267
None
260268
};
269+
let image_backend = if custom_image.is_some() {
270+
if let Some(backend_name) = matches.value_of("image-backend") {
271+
#[cfg(target_os = "linux")]
272+
Some(match backend_name {
273+
"kitty" => Box::new(image_backends::kitty::KittyBackend::new()) as Box<dyn ImageBackend>,
274+
"sixel" => Box::new(image_backends::sixel::SixelBackend::new()) as Box<dyn ImageBackend>,
275+
_ => unreachable!()
276+
})
277+
} else {
278+
crate::image_backends::get_best_backend()
279+
}
280+
} else {
281+
None
282+
};
261283

262284
let no_merges = matches.is_present("no-merges");
263285

@@ -268,6 +290,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
268290
disable_fields,
269291
bold_flag,
270292
custom_image,
293+
image_backend,
271294
no_merges,
272295
)?;
273296

0 commit comments

Comments
 (0)