Skip to content

Commit

Permalink
fix(image): fix image rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 26, 2024
1 parent 7e8bb26 commit b190bd0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/core/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub async fn remove_broken_symlink() -> Result<()> {
Ok(())
}

pub fn wrap_text(text: &str, available_width: usize) -> String {
pub fn wrap_text(text: &str, available_width: usize, indent: u16) -> String {
let mut wrapped_text = String::new();
let mut current_line_length = 0;
let mut current_ansi_sequence = String::new();
Expand All @@ -258,7 +258,7 @@ pub fn wrap_text(text: &str, available_width: usize) -> String {
// Regular character
if current_line_length >= available_width {
wrapped_text.push('\n');
wrapped_text.push_str(&cursor::Right(32).to_string());
wrapped_text.push_str(&cursor::Right(indent).to_string());
current_line_length = 0;
}
wrapped_text.push(c);
Expand Down
18 changes: 12 additions & 6 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,33 +251,39 @@ impl PackageRegistry {
let pkg_image = PackageImage::from(&pkg).await;

let mut printable = Vec::new();
let mut indent = 34;
match pkg_image {
PackageImage::Sixel(img) | PackageImage::HalfBlock(img) => {
printable.extend(format!("{:<2}{}\x1B\\", "", img).as_bytes());
indent = 32;
printable.extend(format!("{}\x1B\\", img).as_bytes());
printable.extend(cursor::Up(15).to_string().as_bytes());
printable.extend(cursor::Right(32).to_string().as_bytes());
printable.extend(cursor::Right(indent).to_string().as_bytes());
}
PackageImage::Kitty(img) => {
printable.extend(format!("{:<2}{}\x1B\\", "", img).as_bytes());
printable.extend(cursor::Down(1).to_string().as_bytes());
printable.extend(format!("{:<2}{}\n\x1B\\", "", img).as_bytes());
printable.extend(cursor::Up(15).to_string().as_bytes());
printable.extend(cursor::Right(indent).to_string().as_bytes());
}
};

data.iter().for_each(|(k, v)| {
let value = strip_ansi_escapes::strip_str(v);

if !value.is_empty() && value != "null" {
let available_width = get_terminal_width() - 32;
let available_width = get_terminal_width() - indent as usize;
let line = wrap_text(
&format!("{}: {}", k.color(Color::Red).bold(), v),
available_width,
indent,
);

printable.extend(format!("{}\n", line).as_bytes());
printable.extend(cursor::Right(32).to_string().as_bytes());
printable.extend(cursor::Right(indent).to_string().as_bytes());
}
});
printable.extend(cursor::Down(16).to_string().as_bytes());

printable.extend(cursor::Down(1).to_string().as_bytes());
println!("{}", String::from_utf8(printable).unwrap());
}
Ok(())
Expand Down
13 changes: 12 additions & 1 deletion src/registry/package/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,18 @@ impl PackageImage {
let image_width = (get_font_width() * 30) as u32;
let image_height = (get_font_height() * 16) as u32;

let img = image::load_from_memory(&icon).unwrap();
let img = match image::load_from_memory(&icon) {
Ok(img) => img,
Err(_) => image::load_from_memory(
&load_default_icon(&format!(
"{}-{}.png",
resolved_package.repo_name, resolved_package.collection
))
.await
.unwrap_or_default(),
)
.unwrap(),
};

if is_kitty_supported().unwrap_or(false) {
let img = img.resize_exact(
Expand Down

0 comments on commit b190bd0

Please sign in to comment.