Skip to content

Commit ab2e9f3

Browse files
authored
Merge pull request #289 from o2sh/remove-panic-from-info-fmt
return Err when image but no image backend
2 parents 2b2aeaa + 949db39 commit ab2e9f3

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

Diff for: src/onefetch/cli.rs

+6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ impl Cli {
138138
.long("image-backend")
139139
.value_name("BACKEND")
140140
.takes_value(true)
141+
.requires("image")
141142
.max_values(1)
142143
.possible_values(&possible_backends)
143144
.help("Which image BACKEND to use."),
@@ -218,6 +219,7 @@ impl Cli {
218219

219220
let image_backend = if image.is_some() {
220221
if let Some(backend_name) = matches.value_of("image-backend") {
222+
image_backends::check_if_supported(backend_name)?;
221223
image_backends::get_image_backend(backend_name)
222224
} else {
223225
image_backends::get_best_backend()
@@ -226,6 +228,10 @@ impl Cli {
226228
None
227229
};
228230

231+
if image.is_some() && image_backend.is_none() {
232+
return Err("Could not detect a supported image backend".into());
233+
}
234+
229235
let image_colors = if let Some(value) = matches.value_of("color-resolution") {
230236
usize::from_str(value).unwrap()
231237
} else {

Diff for: src/onefetch/cli_utils.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ impl<W: Write> Printer<W> {
2424
if self.info.config.art_off {
2525
buf.push_str(&info_str);
2626
} else if let Some(custom_image) = &self.info.config.image {
27-
if let Some(image_backend) = &self.info.config.image_backend {
28-
buf.push_str(&image_backend.add_image(
29-
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
30-
custom_image,
31-
self.info.config.image_colors,
32-
));
33-
} else {
34-
panic!("No image backend found")
35-
}
27+
buf.push_str(&self.info.config.image_backend.as_ref().unwrap().add_image(
28+
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
29+
custom_image,
30+
self.info.config.image_colors,
31+
));
3632
} else {
3733
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
3834
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)

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

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::onefetch::error::*;
12
use image::DynamicImage;
23

34
#[cfg(not(windows))]
@@ -20,6 +21,28 @@ pub fn get_best_backend() -> Option<Box<dyn ImageBackend>> {
2021
}
2122
}
2223

24+
pub fn check_if_supported(backend_name: &str) -> Result<()> {
25+
#[cfg(windows)]
26+
return Err(format!("{} image backend is not supported", backend_name).into());
27+
28+
#[cfg(not(windows))]
29+
match backend_name {
30+
"kitty" => {
31+
if !kitty::KittyBackend::supported() {
32+
return Err("Kitty image backend is not supported".into());
33+
}
34+
}
35+
"sixel" => {
36+
if !sixel::SixelBackend::supported() {
37+
return Err("Sixel image backend is not supported".into());
38+
}
39+
}
40+
_ => unreachable!(),
41+
};
42+
43+
Ok(())
44+
}
45+
2346
pub fn get_image_backend(backend_name: &str) -> Option<Box<dyn ImageBackend>> {
2447
#[cfg(not(windows))]
2548
let backend = Some(match backend_name {

0 commit comments

Comments
 (0)