aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
6
akode-rs/ansi-escape/BUILD.bazel
Normal file
6
akode-rs/ansi-escape/BUILD.bazel
Normal file
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "ansi-escape",
|
||||
crate_name = "akode_ansi_escape",
|
||||
)
|
||||
22
akode-rs/ansi-escape/Cargo.toml
Normal file
22
akode-rs/ansi-escape/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "akode-ansi-escape"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "akode_ansi_escape"
|
||||
path = "src/lib.rs"
|
||||
doctest = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
ansi-to-tui = { workspace = true }
|
||||
ratatui = { workspace = true }
|
||||
tracing = { workspace = true, features = ["log"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
15
akode-rs/ansi-escape/README.md
Normal file
15
akode-rs/ansi-escape/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# oai-codex-ansi-escape
|
||||
|
||||
Small helper functions that wrap functionality from
|
||||
<https://crates.io/crates/ansi-to-tui>:
|
||||
|
||||
```rust
|
||||
pub fn ansi_escape_line(s: &str) -> Line<'static>
|
||||
pub fn ansi_escape<'a>(s: &'a str) -> Text<'a>
|
||||
```
|
||||
|
||||
Advantages:
|
||||
|
||||
- `ansi_to_tui::IntoText` is not in scope for the entire TUI crate
|
||||
- we `panic!()` and log if `IntoText` returns an `Err` and log it so that
|
||||
the caller does not have to deal with it
|
||||
76
akode-rs/ansi-escape/src/ansi_escape_tests.rs
Normal file
76
akode-rs/ansi-escape/src/ansi_escape_tests.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
//! Check that multiline warnings omit payloads and preserve first-line rendering.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::style::Stylize;
|
||||
use ratatui::text::Line;
|
||||
use tracing::Event;
|
||||
use tracing::Level;
|
||||
use tracing::Subscriber;
|
||||
use tracing::field::Field;
|
||||
use tracing::field::Visit;
|
||||
use tracing_subscriber::Layer;
|
||||
use tracing_subscriber::layer::Context;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
use super::ansi_escape_line;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct CapturedEvent {
|
||||
level: Level,
|
||||
fields: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl Visit for CapturedEvent {
|
||||
fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
|
||||
self.fields
|
||||
.insert(field.name().to_string(), format!("{value:?}"));
|
||||
}
|
||||
}
|
||||
|
||||
struct CaptureLayer(Arc<Mutex<Vec<CapturedEvent>>>);
|
||||
|
||||
impl<S: Subscriber> Layer<S> for CaptureLayer {
|
||||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
||||
let mut captured = CapturedEvent {
|
||||
level: *event.metadata().level(),
|
||||
fields: BTreeMap::new(),
|
||||
};
|
||||
event.record(&mut captured);
|
||||
self.0.lock().expect("capture events").push(captured);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiline_warning_contains_counts_and_preserves_the_styled_first_line() {
|
||||
let events = Arc::new(Mutex::new(Vec::new()));
|
||||
let subscriber = tracing_subscriber::registry().with(CaptureLayer(Arc::clone(&events)));
|
||||
let mut expected_events = Vec::new();
|
||||
|
||||
tracing::subscriber::with_default(subscriber, || {
|
||||
for tail_bytes in [8, 128 * 1024] {
|
||||
let tail = "x".repeat(tail_bytes);
|
||||
let input = format!("\x1b[31mfirst\x1b[0m\n{tail}\nlast");
|
||||
assert_eq!(ansi_escape_line(&input), Line::from("first".red()));
|
||||
expected_events.push(CapturedEvent {
|
||||
level: Level::WARN,
|
||||
fields: BTreeMap::from([
|
||||
("input_bytes".to_string(), input.len().to_string()),
|
||||
("line_count".to_string(), "3".to_string()),
|
||||
(
|
||||
"message".to_string(),
|
||||
"ansi_escape_line: expected a single line".to_string(),
|
||||
),
|
||||
]),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
*events.lock().expect("read captured events"),
|
||||
expected_events
|
||||
);
|
||||
}
|
||||
66
akode-rs/ansi-escape/src/lib.rs
Normal file
66
akode-rs/ansi-escape/src/lib.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use ansi_to_tui::Error;
|
||||
use ansi_to_tui::IntoText;
|
||||
use ratatui::text::Line;
|
||||
use ratatui::text::Text;
|
||||
|
||||
// Expand tabs in a best-effort way for transcript rendering.
|
||||
// Tabs can interact poorly with left-gutter prefixes in our TUI and CLI
|
||||
// transcript views (e.g., `nl` separates line numbers from content with a tab).
|
||||
// Replacing tabs with spaces avoids odd visual artifacts without changing
|
||||
// semantics for our use cases.
|
||||
fn expand_tabs(s: &str) -> std::borrow::Cow<'_, str> {
|
||||
if s.contains('\t') {
|
||||
// Keep it simple: replace each tab with 4 spaces.
|
||||
// We do not try to align to tab stops since most usages (like `nl`)
|
||||
// look acceptable with a fixed substitution and this avoids stateful math
|
||||
// across spans.
|
||||
std::borrow::Cow::Owned(s.replace('\t', " "))
|
||||
} else {
|
||||
std::borrow::Cow::Borrowed(s)
|
||||
}
|
||||
}
|
||||
|
||||
/// This function should be used when the contents of `s` are expected to match
|
||||
/// a single line. If multiple lines are found, a warning is logged and only the
|
||||
/// first line is returned.
|
||||
pub fn ansi_escape_line(s: &str) -> Line<'static> {
|
||||
// Normalize tabs to spaces to avoid odd gutter collisions in transcript mode.
|
||||
let s = expand_tabs(s);
|
||||
let text = ansi_escape(&s);
|
||||
match text.lines.as_slice() {
|
||||
[] => "".into(),
|
||||
[only] => only.clone(),
|
||||
[first, rest @ ..] => {
|
||||
tracing::warn!(
|
||||
input_bytes = s.len(),
|
||||
line_count = rest.len() + 1,
|
||||
"ansi_escape_line: expected a single line"
|
||||
);
|
||||
first.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ansi_escape(s: &str) -> Text<'static> {
|
||||
// to_text() claims to be faster, but introduces complex lifetime issues
|
||||
// such that it's not worth it.
|
||||
match s.into_text() {
|
||||
Ok(text) => text,
|
||||
Err(err) => match err {
|
||||
Error::NomError(message) => {
|
||||
tracing::error!(
|
||||
"ansi_to_tui NomError docs claim should never happen when parsing `{s}`: {message}"
|
||||
);
|
||||
panic!();
|
||||
}
|
||||
Error::Utf8Error(utf8error) => {
|
||||
tracing::error!("Utf8Error: {utf8error}");
|
||||
panic!();
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "ansi_escape_tests.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user