Skip to content

Commit

Permalink
Add image-backend argument
Browse files Browse the repository at this point in the history
  • Loading branch information
shuni64 committed Nov 9, 2019
1 parent cb8225f commit c5a1e2c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/image_backends/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use image::DynamicImage;

#[cfg(target_os = "linux")]
mod kitty;
pub mod kitty;
#[cfg(target_os = "linux")]
mod sixel;
pub mod sixel;

pub trait ImageBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String;
Expand Down
9 changes: 6 additions & 3 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use git2::Repository;
use image::DynamicImage;
use license::Detector;

use crate::image_backends;
use crate::image_backends::ImageBackend;
use crate::language::Language;
use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};

Expand All @@ -35,6 +35,7 @@ pub struct Info {
disable_fields: InfoFieldOn,
bold_enabled: bool,
custom_image: Option<DynamicImage>,
image_backend: Option<Box<dyn ImageBackend>>
}

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

if let Some(custom_image) = &self.custom_image {
if let Some(backend) = image_backends::get_best_backend() {
if let Some(image_backend) = &self.image_backend {
writeln!(
f,
"{}",
backend.add_image(
image_backend.add_image(
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
custom_image
)
Expand Down Expand Up @@ -286,6 +287,7 @@ impl Info {
disabled: InfoFieldOn,
bold_flag: bool,
custom_image: Option<DynamicImage>,
image_backend: Option<Box<dyn ImageBackend>>,
no_merges: bool,
) -> Result<Info> {
let repo = Repository::discover(&dir).map_err(|_| Error::NotGitRepo)?;
Expand Down Expand Up @@ -326,6 +328,7 @@ impl Info {
disable_fields: disabled,
bold_enabled: bold_flag,
custom_image,
image_backend,
})
}

Expand Down
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod license;
use ascii_art::AsciiArt;
use commit_info::CommitInfo;
use error::Error;
use image_backends::ImageBackend;
use info::Info;
use language::Language;

Expand Down Expand Up @@ -192,6 +193,13 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
.takes_value(true)
.help("Sets a custom image to use instead of the ascii logo"),
)
.arg(
Arg::with_name("image-backend")
.long("image-backend")
.takes_value(true)
.possible_values(&["kitty", "sixel"])
.help("Overrides image backend detection"),
)
.arg(
Arg::with_name("no-merges")
.short("m")
Expand Down Expand Up @@ -258,6 +266,20 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
} else {
None
};
let image_backend = if custom_image.is_some() {
if let Some(backend_name) = matches.value_of("image-backend") {
#[cfg(target_os = "linux")]
Some(match backend_name {
"kitty" => Box::new(image_backends::kitty::KittyBackend::new()) as Box<dyn ImageBackend>,
"sixel" => Box::new(image_backends::sixel::SixelBackend::new()) as Box<dyn ImageBackend>,
_ => unreachable!()
})
} else {
crate::image_backends::get_best_backend()
}
} else {
None
};

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

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

Expand Down

0 comments on commit c5a1e2c

Please sign in to comment.