1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! [`CommandOutputBlock`] (`.t3-cmd`, "Command output" in `t3.css`): a
//! command and its output. The 40px head (terminal icon, plain mono
//! `$ command` per r3-gal, meta, duration, then a spinner, a `stopped` tag
//! or the exit tag, and the chevron) folds the output; a non-zero exit adds
//! a 3px `support-error` rail.
use super::card;
use crate::components::badge::{Badge, BadgeSize, BadgeVariant};
use crate::t3::base::{Spin, fmt_elapsed};
use crate::t3::press::{self, rem};
use crate::{
icons::CarbonIcon,
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
AnyElement, App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _,
};
/// `duration`: seconds (formatted) or text.
#[derive(Clone, Debug, PartialEq)]
pub enum CommandDuration {
/// Seconds, shown with `fmtElapsed`.
Seconds(u64),
/// Preformatted.
Text(SharedString),
}
/// The exit tag: (label, variant) for `exitCode`.
pub fn exit_tag(code: i32) -> (SharedString, BadgeVariant) {
if code != 0 {
(format!("exit {code}").into(), BadgeVariant::Red)
} else {
("exit 0".into(), BadgeVariant::Green)
}
}
/// A command and its output. See the module docs.
#[derive(IntoElement)]
pub struct CommandOutputBlock {
id: ElementId,
command: SharedString,
output: SharedString,
body: Option<AnyElement>,
interactive: bool,
exit_code: Option<i32>,
running: bool,
stopped: bool,
cwd: Option<SharedString>,
meta: Option<SharedString>,
duration: Option<CommandDuration>,
running_label: SharedString,
stopped_label: SharedString,
}
impl CommandOutputBlock {
/// `command` with its `output` (open by default when there is output).
pub fn new(
id: impl Into<ElementId>,
command: impl Into<SharedString>,
output: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
command: command.into(),
output: output.into(),
body: None,
interactive: true,
exit_code: None,
running: false,
stopped: false,
cwd: None,
meta: None,
duration: None,
running_label: "Running".into(),
stopped_label: "stopped".into(),
}
}
/// Rich ANSI-parsed output element replacing raw `output` text.
pub fn body(mut self, body: impl IntoElement) -> Self {
self.body = Some(body.into_any_element());
self
}
/// Whether the header toggles open/closed (false in Guest read-only mode).
pub fn interactive(mut self, interactive: bool) -> Self {
self.interactive = interactive;
self
}
/// `exitCode`.
pub fn exit_code(mut self, code: i32) -> Self {
self.exit_code = Some(code);
self
}
/// `running`: a spinner instead of the exit tag.
pub fn running(mut self, running: bool) -> Self {
self.running = running;
self
}
/// `stopped`: a gray `stopped` tag.
pub fn stopped(mut self, stopped: bool) -> Self {
self.stopped = stopped;
self
}
/// `cwd`: printed above the output.
pub fn cwd(mut self, cwd: impl Into<SharedString>) -> Self {
self.cwd = Some(cwd.into());
self
}
/// `meta`: a quiet note after the command.
pub fn meta(mut self, meta: impl Into<SharedString>) -> Self {
self.meta = Some(meta.into());
self
}
/// `duration`.
pub fn duration(mut self, duration: CommandDuration) -> Self {
self.duration = Some(duration);
self
}
/// The spinner's hidden label and the stopped tag's text (defaults
/// "Running", "stopped").
pub fn labels(
mut self,
running: impl Into<SharedString>,
stopped: impl Into<SharedString>,
) -> Self {
self.running_label = running.into();
self.stopped_label = stopped.into();
self
}
}
impl RenderOnce for CommandOutputBlock {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let has_output = self.body.is_some() || !self.output.is_empty();
let open_state = window.use_keyed_state((id.clone(), "open"), cx, move |_, _| has_output);
let open = *open_state.read(cx);
let interactive = self.interactive;
let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((id.clone(), "focus"), window, cx);
let failed = self.exit_code.is_some_and(|c| c != 0);
let secondary_icon = color(ColorRole::IconSecondary);
let helper = color(ColorRole::TextHelper);
let status = if self.running {
Some(
Spin::new((id.clone(), "spin"))
.label(self.running_label.clone())
.into_any_element(),
)
} else if self.stopped {
Some(
Badge::new(self.stopped_label.clone())
.size(BadgeSize::Sm)
.variant(BadgeVariant::Gray)
.into_any_element(),
)
} else {
self.exit_code.map(|code| {
let (label, variant) = exit_tag(code);
Badge::new(label)
.size(BadgeSize::Sm)
.variant(variant)
.into_any_element()
})
};
let duration = self.duration.clone().map(|d| match d {
CommandDuration::Seconds(s) => SharedString::from(fmt_elapsed(s)),
CommandDuration::Text(t) => t,
});
// `.t3-cmd-head { display: flex; align-items: center; gap: spacing-03; width: 100%;
// min-height: size-md; padding: 0 spacing-04; color: text-primary }`, `:hover { background: layer-hover-01 }`.
let head = gpui_kit::base::Button::new((id.clone(), "head"))
.when(interactive, |b| b.track_focus(&focus))
.accessibility_label(self.command.clone())
.relative()
.flex()
.flex_row()
.items_center()
.justify_start()
.w_full()
.gap(rem(8.))
.min_h(rem(40.))
.px(rem(12.))
.text_color(color(ColorRole::TextPrimary))
.when(interactive, |b| {
b.cursor_pointer()
.when(hovered, |this| this.bg(color(ColorRole::LayerHover01)))
.on_hover(press::track_hover(hover))
.on_click(move |_, _, cx| {
open_state.update(cx, |o, cx| {
*o = !*o;
cx.notify();
})
})
})
.child(press::icon(CarbonIcon::Terminal, 16., secondary_icon))
// `.t3-cmd-line { flex: 1; text-overflow: ellipsis; font-family: mono }`, 13px (r3-gal);
// `.t3-cmd-prompt { color: text-helper; margin-right: spacing-03 }`.
.child(
div()
.flex()
.flex_row()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.font(font(FontFamilyKey::Mono, 400))
.text_size(rem(13.))
.child(div().flex_none().mr(rem(8.)).text_color(helper).child("$"))
.child(
div()
.min_w_0()
.overflow_hidden()
.text_ellipsis()
.child(self.command.clone()),
),
)
// `.t3-cmd-meta { flex: none; font-size: 0.75rem; color: text-helper }`.
.when_some(self.meta.clone(), |this, m| {
this.child(
div()
.flex_none()
.text_size(rem(12.))
.text_color(helper)
.child(m),
)
})
// `.t3-cmd-dur { flex: none; font-size: 0.75rem; color: text-helper; tabular-nums }`.
.when_some(duration, |this, d| {
this.child(
div()
.flex_none()
.text_size(rem(12.))
.text_color(helper)
.child(d),
)
})
.children(status)
.when(interactive, |this| {
this.child(
press::icon(CarbonIcon::ChevronDown, 14., secondary_icon).rotate(
gpui_kit::radians(if open {
0.
} else {
-std::f32::consts::FRAC_PI_2
}),
),
)
.when(ring, |b| b.child(press::ring()))
});
// `.t3-cmd { background: layer-01 }`, `.t3-cmd--fail { box-shadow: inset 3px 0 0 support-error }`.
div()
.flex()
.flex_col()
.w_full()
.min_w_0()
.bg(color(ColorRole::Layer01))
.when(failed, |this| {
this.shadow(card::rail(color(ColorRole::SupportError)))
})
.text_size(rem(14.))
.line_height(rem(18.))
.child(head)
.when(open, |this| {
// `.t3-cmd-out { max-height: 12rem; overflow: auto; padding: spacing-04;
// border-top: 1px solid border-subtle-01; font-family: mono; font-size: 0.75rem;
// line-height: 1.33333; color: text-secondary; white-space: pre-wrap }`.
this.child(
div()
.id((id.clone(), "out"))
.max_h(rem(192.))
.overflow_y_scroll()
.p(rem(12.))
.border_t_1()
.border_color(color(ColorRole::BorderSubtle01))
.font(font(FontFamilyKey::Mono, 400))
.text_size(rem(12.))
.line_height(rem(16.))
.text_color(color(ColorRole::TextSecondary))
// `.t3-cmd-cwd { color: text-helper }`.
.when_some(self.cwd.clone(), |this, cwd| {
this.child(div().text_color(helper).child(cwd))
})
.map(|this| match self.body {
Some(body) => this.child(body),
None => this.child(self.output.clone()),
}),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_tags_follow_the_code() {
assert_eq!(exit_tag(1), ("exit 1".into(), BadgeVariant::Red));
assert_eq!(exit_tag(0), ("exit 0".into(), BadgeVariant::Green));
}
}