use std::sync::Arc;
use std::time::Instant;
use ratatui::{
Terminal,
backend::TestBackend,
crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers},
};
use tokio::sync::mpsc;
use vtcode_ui::tui::app::{InlineCommand, InlineEvent, InlineMessageKind, InlineSegment, InlineTextStyle, InlineTheme};
use vtcode_ui::tui::core_tui::app::AppSession;
fn segment(text: impl Into<String>) -> InlineSegment {
InlineSegment {
text: text.into(),
style: Arc::new(InlineTextStyle::default()),
}
}
#[hotpath::measure]
fn fill_transcript(session: &mut AppSession, count: usize) {
for index in 0..count {
let text = format!("line {index}: the quick brown fox jumps over the lazy dog while streaming tool output");
session.handle_command(InlineCommand::AppendLine {
kind: InlineMessageKind::Agent,
segments: vec![segment(text)],
});
}
}
#[hotpath::measure]
fn stream_chunks(session: &mut AppSession, chunks: usize) {
session.handle_command(InlineCommand::AppendLine {
kind: InlineMessageKind::Agent,
segments: vec![segment("stream-start")],
});
for index in 0..chunks {
session.handle_command(InlineCommand::AppendLine {
kind: InlineMessageKind::Agent,
segments: vec![segment(format!(" chunk-{index}"))],
});
}
}
#[hotpath::measure]
fn fill_link_transcript(session: &mut AppSession, count: usize) {
for index in 0..count {
session.handle_command(InlineCommand::AppendLine {
kind: InlineMessageKind::Agent,
segments: vec![segment(format!(
"line {index}: inspect /tmp/project/src/file_{index}.rs:42 or https://example.com/item/{index}"
))],
});
}
}
#[hotpath::measure]
fn render_frames(session: &mut AppSession, terminal: &mut Terminal<TestBackend>, frames: usize) {
for _ in 0..frames {
let _ = terminal.draw(|frame| session.render(frame)).expect("render fixture frame");
}
}
#[hotpath::measure]
fn input_burst(session: &mut AppSession, events: usize) {
let (tx, mut rx) = mpsc::unbounded_channel::<InlineEvent>();
for index in 0..events {
let key = if index % 7 == 0 {
KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)
} else if index % 7 == 1 {
KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)
} else {
KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)
};
session.handle_event(CrosstermEvent::Key(key), &tx, None);
}
while rx.try_recv().is_ok() {}
}
#[hotpath::measure]
fn channel_demo() {
let (tx, mut rx) = hotpath::channel!(mpsc::unbounded_channel::<u64>(), label = "tui-hotpath-demo");
for job in 0..100u64 {
if tx.send(job).is_err() {
break;
}
}
let mut received = 0u64;
while rx.try_recv().is_ok() {
received += 1;
}
let _ = std::hint::black_box(received);
}
#[hotpath::measure]
fn mutex_demo() {
let counter = hotpath::mutex!(std::sync::Mutex::new(0u64), label = "tui-hotpath-demo-mutex");
for _ in 0..10_000 {
*counter.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) += 1;
}
let total = *counter.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let _ = std::hint::black_box(total);
}
#[hotpath::measure]
fn io_demo() {
use std::io::{Read, Write};
const LINE: &[u8] = b"line: the quick brown fox jumps over the lazy dog while streaming tool output\n";
let mut spool = hotpath::io!(std::io::Cursor::new(Vec::new()), label = "tui-hotpath-spool-write");
let mut written = 0usize;
for _ in 0..1_000 {
if spool.write_all(LINE).is_err() {
break;
}
written += LINE.len();
}
let bytes = hotpath::io_unwrap(spool).into_inner();
let mut reader = hotpath::io!(std::io::Cursor::new(bytes), label = "tui-hotpath-spool-read");
let mut read_back = Vec::new();
if reader.read_to_end(&mut read_back).is_err() {
read_back.clear();
}
let _ = std::hint::black_box((written, read_back.len()));
}
#[hotpath::measure]
fn main_workload() {
let mut session = AppSession::new(InlineTheme::default(), None, 30);
let mut terminal = Terminal::new(TestBackend::new(120, 40)).expect("terminal");
fill_transcript(&mut session, 2_000);
render_frames(&mut session, &mut terminal, 120);
stream_chunks(&mut session, 400);
render_frames(&mut session, &mut terminal, 80);
input_burst(&mut session, 300);
render_frames(&mut session, &mut terminal, 60);
fill_transcript(&mut session, 4_000);
render_frames(&mut session, &mut terminal, 40);
fill_link_transcript(&mut session, 1_000);
render_frames(&mut session, &mut terminal, 40);
channel_demo();
mutex_demo();
io_demo();
}
#[hotpath::main(functions_limit = 0)]
fn main() {
let started = Instant::now();
main_workload();
eprintln!("[tui_hotpath] workload finished in {:?}", started.elapsed());
}