From 1ecf2311e40a753fa95811eb1b065e18a2751dbc Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sun, 26 Oct 2025 22:20:30 +0700 Subject: [PATCH] Add ansi escape --- Cargo.lock | 36 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/repo.rs | 48 +++++++++++++++++++++++++++++------------------- src/cook/pty.rs | 2 +- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 907d9d9e3..749958945 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,19 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi-to-tui" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67555e1f1ece39d737e28c8a017721287753af3f93225e4a445b29ccb0f5912c" +dependencies = [ + "nom", + "ratatui", + "simdutf8", + "smallvec", + "thiserror 1.0.69", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -1540,6 +1553,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1572,6 +1591,16 @@ dependencies = [ "libc", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -2061,6 +2090,7 @@ dependencies = [ name = "redox_cookbook" version = "0.1.0" dependencies = [ + "ansi-to-tui", "anyhow", "blake3 1.5.3", "filedescriptor", @@ -2537,6 +2567,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + [[package]] name = "slab" version = "0.4.10" diff --git a/Cargo.toml b/Cargo.toml index ae5e18c94..e21950d79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ termion = "4" toml = "0.8" walkdir = "2.3.1" filedescriptor = "0.8.3" +ansi-to-tui = "7.0.0" [dependencies.ratatui] version = "0.29.0" diff --git a/src/bin/repo.rs b/src/bin/repo.rs index 6e643f4aa..6097b575e 100644 --- a/src/bin/repo.rs +++ b/src/bin/repo.rs @@ -1,3 +1,21 @@ +use ansi_to_tui::IntoText; +use anyhow::{Context, anyhow, bail}; +use cookbook::WALK_DEPTH; +use cookbook::config::{CookConfig, get_config, init_config}; +use cookbook::cook::cook_build::build; +use cookbook::cook::fetch::{fetch, fetch_offline}; +use cookbook::cook::fs::create_target_dir; +use cookbook::cook::package::package; +use cookbook::cook::pty::{PtyOut, UnixSlavePty, setup_pty}; +use cookbook::recipe::CookRecipe; +use pkg::PackageName; +use pkg::package::PackageError; +use ratatui::Terminal; +use ratatui::layout::{Constraint, Direction, Layout, Position, Rect}; +use ratatui::prelude::TermionBackend; +use ratatui::style::{Color, Style}; +use ratatui::text::{Line, Span, Text}; +use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap}; use std::collections::{HashMap, VecDeque}; use std::io::{BufRead, BufReader, Read, Write, stderr, stdin, stdout}; use std::path::PathBuf; @@ -8,24 +26,6 @@ use std::sync::{Arc, mpsc}; use std::time::{Duration, Instant}; use std::{cmp, env, fs}; use std::{process, thread}; - -use anyhow::{Context, anyhow, bail}; -use cookbook::WALK_DEPTH; -use cookbook::config::{CookConfig, get_config, init_config}; -use cookbook::cook::cook_build::build; -use cookbook::cook::fetch::{fetch, fetch_offline}; -use cookbook::cook::fs::create_target_dir; -use cookbook::cook::package::package; -use cookbook::cook::pty::{setup_pty, PtyOut, UnixSlavePty}; -use cookbook::recipe::CookRecipe; -use pkg::PackageName; -use pkg::package::PackageError; -use ratatui::Terminal; -use ratatui::layout::{Constraint, Direction, Layout, Position, Rect}; -use ratatui::prelude::TermionBackend; -use ratatui::style::{Color, Style}; -use ratatui::text::{Line, Span}; -use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap}; use termion::event::{Event, Key, MouseEvent}; use termion::input::TermRead; use termion::raw::IntoRawMode; @@ -937,7 +937,17 @@ fn run_tui_cook( log_text[start..end] .iter() - .map(|s| Line::from(s.clone())) + .map(|s| { + let text_with_colors = s + .into_text() + .unwrap_or_else(|_| Text::raw("--unrenderable line--")); + + text_with_colors + .lines + .into_iter() + .next() + .unwrap_or_else(|| Line::raw("--unrenderable line--")) + }) .collect() } else { vec![Line::from("No logs yet")] diff --git a/src/cook/pty.rs b/src/cook/pty.rs index 42876e9cf..701655074 100644 --- a/src/cook/pty.rs +++ b/src/cook/pty.rs @@ -286,7 +286,7 @@ impl PtyFd { // them) and won't work in the usual way anyway. // In practice these are None, but it seems best to be move them // out in case the behavior of Command changes in the future. - child.stdin.take(); + // child.stdin.take(); child.stdout.take(); child.stderr.take();