Skip to content

Commit

Permalink
fix(wrap): fix text wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 26, 2024
1 parent 8f43a68 commit e7b6d71
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum Commands {

/// Case sensitive search
#[arg(required = false, long)]
case_sensitive: bool
case_sensitive: bool,
},

/// Query package info
Expand Down
2 changes: 1 addition & 1 deletion src/core/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{path::PathBuf, sync::LazyLock};

use super::{config::CONFIG, util::build_path};

pub static CACHE_PATH: LazyLock<PathBuf> =
pub static CACHE_PATH: LazyLock<PathBuf> =
LazyLock::new(|| build_path(&CONFIG.soar_path).unwrap().join("cache"));
pub static REGISTRY_PATH: LazyLock<PathBuf> =
LazyLock::new(|| build_path(&CONFIG.soar_path).unwrap().join("registry"));
Expand Down
40 changes: 28 additions & 12 deletions src/core/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use anyhow::{Context, Result};
use futures::StreamExt;
use libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ};
use termion::cursor;
use tokio::{
fs::{self, File},
io::{AsyncReadExt, AsyncWriteExt},
Expand Down Expand Up @@ -233,20 +234,35 @@ pub async fn remove_broken_symlink() -> Result<()> {
Ok(())
}

// FIXME: handle ansi sequences
pub fn wrap_text(text: &str, padding: usize) -> String {
pub fn wrap_text(text: &str, available_width: usize) -> String {
let mut wrapped_text = String::new();
let width = get_terminal_width() - padding / 2;
let mut pos = 0;
let mut current_line_length = 0;
let mut current_ansi_sequence = String::new();
let mut chars = text.chars().peekable();

while pos < text.len() {
let end = std::cmp::min(pos + width, text.len());
let chunk = &text[pos..end];

wrapped_text.push_str(&format!("{:<padding$}{}", "", chunk, padding = padding));
pos = end;
if pos < text.len() {
wrapped_text.push('\n');
while let Some(c) = chars.next() {
if c == '\x1B' {
// Start of ANSI escape sequence
current_ansi_sequence.push(c);
while let Some(&next_c) = chars.peek() {
if !next_c.is_ascii_alphabetic() {
current_ansi_sequence.push(chars.next().unwrap());
} else {
current_ansi_sequence.push(chars.next().unwrap());
wrapped_text.push_str(&current_ansi_sequence);
current_ansi_sequence.clear();
break;
}
}
} else {
// Regular character
if current_line_length >= available_width {
wrapped_text.push('\n');
wrapped_text.push_str(&cursor::Right(32).to_string());
current_line_length = 0;
}
wrapped_text.push(c);
current_line_length += 1;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ pub async fn init() -> Result<()> {
Commands::ListInstalledPackages { packages } => {
registry.info(packages.as_deref()).await?;
}
Commands::Search { query, case_sensitive} => {
Commands::Search {
query,
case_sensitive,
} => {
registry.search(&query, case_sensitive).await?;
}
Commands::Query { query } => {
Expand Down
72 changes: 23 additions & 49 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use loader::RegistryLoader;
use package::{image::PackageImage, parse_package_query, update::Updater, ResolvedPackage};
use serde::Deserialize;
use storage::{PackageStorage, RepositoryPackages};
use termion::cursor;
use tokio::{fs, sync::Mutex};

use crate::{
core::{
color::{Color, ColorExt},
config::CONFIG,
constant::REGISTRY_PATH,
util::{download, wrap_text},
util::{download, get_terminal_width, wrap_text},
},
error, info, success,
};
Expand Down Expand Up @@ -251,60 +252,33 @@ impl PackageRegistry {

let mut printable = Vec::new();
match pkg_image {
PackageImage::Sixel(img) => {
PackageImage::Sixel(img) | PackageImage::HalfBlock(img) => {
printable.extend(format!("{:<2}{}\x1B\\", "", img).as_bytes());
printable.extend(format!("\x1B[{}A", 15).as_bytes());
printable.extend(format!("\x1B[{}C", 32).as_bytes());

data.iter().for_each(|(k, v)| {
let value = strip_ansi_escapes::strip(v);
let value = String::from_utf8(value).unwrap();

if !value.is_empty() && value != "null" {
let line =
wrap_text(&format!("{}: {}", k.color(Color::Red).bold(), v), 4);
printable.extend(format!("\x1B[s{}\x1B[u\x1B[1B", line).as_bytes());
}
});
printable.extend(format!("\n\x1B[{}B", 16).as_bytes());
println!("{}", String::from_utf8(printable).unwrap());
printable.extend(cursor::Up(15).to_string().as_bytes());
printable.extend(cursor::Right(32).to_string().as_bytes());
}
PackageImage::Kitty(img) => {
printable.extend(format!("{:<2}{}\x1B\\", "", img).as_bytes());
printable.extend(format!("\x1B[{}A", 15).as_bytes());

data.iter().for_each(|(k, v)| {
let value = strip_ansi_escapes::strip(v);
let value = String::from_utf8(value).unwrap();

if !value.is_empty() && value != "null" {
let line =
wrap_text(&format!("{}: {}", k.color(Color::Red).bold(), v), 4);
printable.extend(format!("\x1B[s{}\x1B[u\x1B[1B", line).as_bytes());
}
});
printable.extend(format!("\n\x1B[{}B", 16).as_bytes());
println!("{}", String::from_utf8(printable).unwrap());
}
PackageImage::HalfBlock(img) => {
printable.extend(format!("{:<2}{}\x1B\\", "", img).as_bytes());
printable.extend(format!("\x1B[{}A", 15).as_bytes());
printable.extend(format!("\x1B[{}C", 32).as_bytes());

data.iter().for_each(|(k, v)| {
let value = strip_ansi_escapes::strip(v);
let value = String::from_utf8(value).unwrap();

if !value.is_empty() && value != "null" {
let line =
wrap_text(&format!("{}: {}", k.color(Color::Red).bold(), v), 4);
printable.extend(format!("\x1B[s{}\x1B[u\x1B[1B", line).as_bytes());
}
});
printable.extend(format!("\n\x1B[{}B", 16).as_bytes());
println!("{}", String::from_utf8(printable).unwrap());
printable.extend(cursor::Up(15).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 line = wrap_text(
&format!("{}: {}", k.color(Color::Red).bold(), v),
available_width,
);

printable.extend(format!("{}\n", line).as_bytes());
printable.extend(cursor::Right(32).to_string().as_bytes());
}
});
printable.extend(cursor::Down(16).to_string().as_bytes());
println!("{}", String::from_utf8(printable).unwrap());
}
Ok(())
}
Expand Down

0 comments on commit e7b6d71

Please sign in to comment.