Displays a command the agent runs, with its output as it streams. Each command is a trace row that opens on its log while it runs and closes when it exits.
Loading GPUI…
use std::time::Duration;
use gpui::{div, prelude::*, px, relative, Context, Task, Window};
use crate::icons::{icon, CHECK, TERMINAL};
use crate::theme::ActiveTheme;
use crate::ui::bubble::{Bubble, BubbleAlign, BubbleVariant};
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::message::{Message, MessageAlign};
use crate::ui::trace::{Trace, TraceCollapsible, TraceLine, TraceStatus};
const MONO: &str = "Geist Mono";
/// The mock transport's pace: one chunk every 20ms, and 200ms between output lines.
const CHUNK: Duration = Duration::from_millis(20);
const OUTPUT_LINE: Duration = Duration::from_millis(200);
const QUESTION: &str = "Run the checks, then build the app.";
const ANSWER: &str = "Lint and tests pass, and the build compiled both routes.";
const COMMANDS: [(&str, &[&str]); 3] = [
("pnpm lint", &["> acme-chat@0.1.0 lint", "> eslint ."]),
(
"pnpm test",
&[
"> acme-chat@0.1.0 test",
"> vitest run",
"",
" ✓ lib/ai.test.ts (6 tests) 41ms",
" ✓ components/chat.test.tsx (9 tests) 212ms",
"",
" Test Files 2 passed (2)",
" Tests 15 passed (15)",
],
),
(
"pnpm build",
&[
"> acme-chat@0.1.0 build",
"> next build",
"",
" ▲ Next.js 16.3.0",
" Creating an optimized production build ...",
" ✓ Compiled successfully in 4.2s",
" ✓ Generating static pages (12/12)",
"",
"Route (app) Size First Load JS",
"┌ ○ / 1.9 kB 112 kB",
"└ ƒ /api/chat 141 B 102 kB",
],
),
];
/// One command the agent has started.
#[derive(Clone, Copy)]
struct Run {
/// Output lines streamed so far.
lines: usize,
exited: bool,
/// Set once the reader opens or closes the row; until then it follows the run.
open: Option<bool>,
}
pub struct TerminalPattern {
runs: Vec<Run>,
/// Words of the closing answer shown so far.
words: usize,
busy: bool,
stream: Option<Task<()>>,
}
impl Default for TerminalPattern {
/// Starts on the finished transcript with the last command's log open.
fn default() -> Self {
let mut runs: Vec<Run> = COMMANDS
.iter()
.map(|(_, output)| Run { lines: output.len(), exited: true, open: None })
.collect();
if let Some(last) = runs.last_mut() {
last.open = Some(true);
}
Self { runs, words: ANSWER.split_inclusive(' ').count(), busy: false, stream: None }
}
}
impl TerminalPattern {
fn replay(&mut self, cx: &mut Context<Self>) {
self.runs.clear();
self.words = 0;
self.busy = true;
self.stream = Some(cx.spawn(async move |this, cx| {
for (ix, (_, output)) in COMMANDS.iter().enumerate() {
cx.background_executor().timer(CHUNK).await;
let started = this.update(cx, |this, cx| {
this.runs.push(Run { lines: 0, exited: false, open: None });
cx.notify();
});
if started.is_err() {
return;
}
for _ in 0..output.len() {
cx.background_executor().timer(CHUNK).await;
let _ = this.update(cx, |this, cx| {
this.runs[ix].lines += 1;
cx.notify();
});
cx.background_executor().timer(OUTPUT_LINE).await;
}
cx.background_executor().timer(CHUNK).await;
let _ = this.update(cx, |this, cx| {
this.runs[ix].exited = true;
cx.notify();
});
}
for _ in 0..ANSWER.split_inclusive(' ').count() {
cx.background_executor().timer(CHUNK).await;
if this.update(cx, |this, cx| {
this.words += 1;
cx.notify();
})
.is_err()
{
return;
}
}
let _ = this.update(cx, |this, cx| {
this.busy = false;
cx.notify();
});
}));
cx.notify();
}
}
impl Render for TerminalPattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
let (muted, fg) = (theme.muted, theme.foreground);
let rows = self.runs.iter().enumerate().map(|(ix, run)| {
let (command, output) = COMMANDS[ix];
let running = !run.exited;
let title = if running { format!("Running {command}") } else { format!("Ran {command}") };
let line = TraceLine::new(("command", ix), title)
.icon(TERMINAL)
.status(if running { TraceStatus::Running } else { TraceStatus::Done });
let open = run.open.unwrap_or(running && run.lines > 0);
let log = output[..run.lines].join("\n");
TraceCollapsible::new(("run", ix), open, line)
.on_open_change(cx.listener(move |this, open: &bool, _, cx| {
if let Some(run) = this.runs.get_mut(ix) {
run.open = Some(*open);
}
cx.notify();
}))
.child(
// shadcn's small code block: a muted panel with 12px mono text.
div()
.max_h(px(256.0))
.overflow_hidden()
.p_3()
.rounded(px(14.0))
.bg(muted)
.font_family(MONO)
.text_xs()
.line_height(px(19.5))
.text_color(fg)
.whitespace_normal()
.children(log.lines().map(|line| {
// Geist Mono has no check mark, so draw the tick as an icon in its
// character cell.
match line.split_once('✓') {
Some((lead, rest)) => div()
.flex()
.items_center()
.min_h(px(19.5))
.child(lead.to_string())
.child(div().w(px(7.2)).flex().justify_center().child(icon(CHECK).size(px(10.0)).text_color(fg)))
.child(rest.to_string()),
None => div().min_h(px(19.5)).child(line.to_string()),
}
})),
)
});
let answer: String = ANSWER.split_inclusive(' ').take(self.words).collect();
div()
.w(px(384.0))
.min_h(px(288.0))
.flex()
.flex_col()
.gap_4()
.py_6()
.text_sm()
.child(
Message::new()
.align(MessageAlign::End)
.child(Bubble::new("question").align(BubbleAlign::End).variant(BubbleVariant::Muted).child(QUESTION)),
)
.when(!self.runs.is_empty(), |this| {
this.child(
Message::new().child(
div()
.flex()
.flex_col()
.gap_4()
.w_full()
.child(Trace::new().children(rows))
.when(!answer.is_empty(), |this| {
this.child(div().line_height(relative(1.4286)).text_color(fg).child(answer))
}),
),
)
})
.when(!self.busy, |this| {
this.child(
div().mt_auto().flex().child(
Button::new("replay", "Replay")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Compact)
.rounded_full()
.on_click(cx.listener(|this, _, _, cx| this.replay(cx))),
),
)
})
}
}Installation
Add the components
$ cargo hyuskui add bubble button message trace iconsCopy the pattern
Paste the code into
src/ai/terminal.rsand declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.src/ai/terminal.rs use std::time::Duration; use gpui::{div, prelude::*, px, relative, Context, Task, Window}; use crate::icons::{icon, CHECK, TERMINAL}; use crate::theme::ActiveTheme; use crate::ui::bubble::{Bubble, BubbleAlign, BubbleVariant}; use crate::ui::button::{Button, ButtonSize, ButtonVariant}; use crate::ui::message::{Message, MessageAlign}; use crate::ui::trace::{Trace, TraceCollapsible, TraceLine, TraceStatus}; const MONO: &str = "Geist Mono"; /// The mock transport's pace: one chunk every 20ms, and 200ms between output lines. const CHUNK: Duration = Duration::from_millis(20); const OUTPUT_LINE: Duration = Duration::from_millis(200); const QUESTION: &str = "Run the checks, then build the app."; const ANSWER: &str = "Lint and tests pass, and the build compiled both routes."; const COMMANDS: [(&str, &[&str]); 3] = [ ("pnpm lint", &["> acme-chat@0.1.0 lint", "> eslint ."]), ( "pnpm test", &[ "> acme-chat@0.1.0 test", "> vitest run", "", " ✓ lib/ai.test.ts (6 tests) 41ms", " ✓ components/chat.test.tsx (9 tests) 212ms", "", " Test Files 2 passed (2)", " Tests 15 passed (15)", ], ), ( "pnpm build", &[ "> acme-chat@0.1.0 build", "> next build", "", " ▲ Next.js 16.3.0", " Creating an optimized production build ...", " ✓ Compiled successfully in 4.2s", " ✓ Generating static pages (12/12)", "", "Route (app) Size First Load JS", "┌ ○ / 1.9 kB 112 kB", "└ ƒ /api/chat 141 B 102 kB", ], ), ]; /// One command the agent has started. #[derive(Clone, Copy)] struct Run { /// Output lines streamed so far. lines: usize, exited: bool, /// Set once the reader opens or closes the row; until then it follows the run. open: Option<bool>, } pub struct TerminalPattern { runs: Vec<Run>, /// Words of the closing answer shown so far. words: usize, busy: bool, stream: Option<Task<()>>, } impl Default for TerminalPattern { /// Starts on the finished transcript with the last command's log open. fn default() -> Self { let mut runs: Vec<Run> = COMMANDS .iter() .map(|(_, output)| Run { lines: output.len(), exited: true, open: None }) .collect(); if let Some(last) = runs.last_mut() { last.open = Some(true); } Self { runs, words: ANSWER.split_inclusive(' ').count(), busy: false, stream: None } } } impl TerminalPattern { fn replay(&mut self, cx: &mut Context<Self>) { self.runs.clear(); self.words = 0; self.busy = true; self.stream = Some(cx.spawn(async move |this, cx| { for (ix, (_, output)) in COMMANDS.iter().enumerate() { cx.background_executor().timer(CHUNK).await; let started = this.update(cx, |this, cx| { this.runs.push(Run { lines: 0, exited: false, open: None }); cx.notify(); }); if started.is_err() { return; } for _ in 0..output.len() { cx.background_executor().timer(CHUNK).await; let _ = this.update(cx, |this, cx| { this.runs[ix].lines += 1; cx.notify(); }); cx.background_executor().timer(OUTPUT_LINE).await; } cx.background_executor().timer(CHUNK).await; let _ = this.update(cx, |this, cx| { this.runs[ix].exited = true; cx.notify(); }); } for _ in 0..ANSWER.split_inclusive(' ').count() { cx.background_executor().timer(CHUNK).await; if this.update(cx, |this, cx| { this.words += 1; cx.notify(); }) .is_err() { return; } } let _ = this.update(cx, |this, cx| { this.busy = false; cx.notify(); }); })); cx.notify(); } } impl Render for TerminalPattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme(); let (muted, fg) = (theme.muted, theme.foreground); let rows = self.runs.iter().enumerate().map(|(ix, run)| { let (command, output) = COMMANDS[ix]; let running = !run.exited; let title = if running { format!("Running {command}") } else { format!("Ran {command}") }; let line = TraceLine::new(("command", ix), title) .icon(TERMINAL) .status(if running { TraceStatus::Running } else { TraceStatus::Done }); let open = run.open.unwrap_or(running && run.lines > 0); let log = output[..run.lines].join("\n"); TraceCollapsible::new(("run", ix), open, line) .on_open_change(cx.listener(move |this, open: &bool, _, cx| { if let Some(run) = this.runs.get_mut(ix) { run.open = Some(*open); } cx.notify(); })) .child( // shadcn's small code block: a muted panel with 12px mono text. div() .max_h(px(256.0)) .overflow_hidden() .p_3() .rounded(px(14.0)) .bg(muted) .font_family(MONO) .text_xs() .line_height(px(19.5)) .text_color(fg) .whitespace_normal() .children(log.lines().map(|line| { // Geist Mono has no check mark, so draw the tick as an icon in its // character cell. match line.split_once('✓') { Some((lead, rest)) => div() .flex() .items_center() .min_h(px(19.5)) .child(lead.to_string()) .child(div().w(px(7.2)).flex().justify_center().child(icon(CHECK).size(px(10.0)).text_color(fg))) .child(rest.to_string()), None => div().min_h(px(19.5)).child(line.to_string()), } })), ) }); let answer: String = ANSWER.split_inclusive(' ').take(self.words).collect(); div() .w(px(384.0)) .min_h(px(288.0)) .flex() .flex_col() .gap_4() .py_6() .text_sm() .child( Message::new() .align(MessageAlign::End) .child(Bubble::new("question").align(BubbleAlign::End).variant(BubbleVariant::Muted).child(QUESTION)), ) .when(!self.runs.is_empty(), |this| { this.child( Message::new().child( div() .flex() .flex_col() .gap_4() .w_full() .child(Trace::new().children(rows)) .when(!answer.is_empty(), |this| { this.child(div().line_height(relative(1.4286)).text_color(fg).child(answer)) }), ), ) }) .when(!self.busy, |this| { this.child( div().mt_auto().flex().child( Button::new("replay", "Replay") .variant(ButtonVariant::Outline) .size(ButtonSize::Compact) .rounded_full() .on_click(cx.listener(|this, _, _, cx| this.replay(cx))), ), ) }) } }