summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorquentin konieczko <konieczko@gmail.com>2023-03-21 17:24:19 +0100
committerquentin konieczko <konieczko@gmail.com>2023-03-21 17:24:19 +0100
commitcdefd70dbbd8ddfa34caab75efd6a81ed8410321 (patch)
treee01f94b3ab49910a12c731712c2ac97922a463ad
parent738caef8ea8f17c6201b118ad16591a689f53e98 (diff)
use skim ansi parser to print ansi formated line
-rw-r--r--src/main.rs13
-rw-r--r--src/skim.rs21
2 files changed, 33 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index f6ec13a..d6eb003 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -19,7 +19,7 @@ use fm::utils::{drop_everything, init_term, print_on_quit};
/// Init the status and display and listen to events (keyboard, mouse, resize, custom...).
/// The application is redrawn after every event.
/// When the user issues a quit event, the main loop is broken and we reset the cursor.
-fn main() -> Result<()> {
+fn main2() -> Result<()> {
set_loggers()?;
info!("fm is starting");
@@ -67,3 +67,14 @@ fn main() -> Result<()> {
info!("fm is shutting down");
Ok(())
}
+
+fn main() -> Result<()> {
+ let term = Arc::new(init_term()?);
+ // let s = "\x1B[35mA\x1B[mB";
+ let s = "│ / │ 219.5G │ 135.0G │ 73.3G │  61.5% │ ext4 │ /dev/sda7 │";
+ fm::skim::print_ansi_str(s, &term, Some(0), Some(0))?;
+ term.present()?;
+ while let Ok(ev) = term.poll_event() {}
+
+ Ok(())
+}
diff --git a/src/skim.rs b/src/skim.rs
index 8e27796..ea7335b 100644
--- a/src/skim.rs
+++ b/src/skim.rs
@@ -96,3 +96,24 @@ fn pick_first_installed<'a>(commands: &'a [&'a str]) -> Option<&'a str> {
}
None
}
+
+/// Print an ANSI escaped with corresponding colors.
+pub fn print_ansi_str(
+ text: &str,
+ term: &Arc<tuikit::term::Term>,
+ col: Option<usize>,
+ row: Option<usize>,
+) -> anyhow::Result<()> {
+ let mut col = match col {
+ Some(col) => col,
+ None => 0,
+ };
+ let row = match row {
+ Some(row) => row,
+ None => 0,
+ };
+ for (chr, attr) in skim::AnsiString::parse(text).iter() {
+ col += term.print_with_attr(row, col, &chr.to_string(), attr)?;
+ }
+ Ok(())
+}