use dioxus::prelude::*;
use super::copy::CopyAction;
use super::util::optional_text;
use crate::class::compose;
pub const TERMINAL_DEFAULT_PROMPT: &str = "$";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TerminalLineKind {
#[default]
Command,
Output,
Comment,
}
impl TerminalLineKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Command => "command",
Self::Output => "output",
Self::Comment => "comment",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TerminalLineTone {
#[default]
Default,
Muted,
Success,
Warning,
Danger,
Brand,
}
impl TerminalLineTone {
pub fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::Muted => "muted",
Self::Success => "success",
Self::Warning => "warning",
Self::Danger => "danger",
Self::Brand => "brand",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TerminalLine {
pub kind: TerminalLineKind,
pub text: String,
pub tone: TerminalLineTone,
}
impl TerminalLine {
pub fn command(text: impl Into<String>) -> Self {
Self {
kind: TerminalLineKind::Command,
text: text.into(),
tone: TerminalLineTone::Default,
}
}
pub fn output(text: impl Into<String>) -> Self {
Self {
kind: TerminalLineKind::Output,
..Self::command(text)
}
}
pub fn comment(text: impl Into<String>) -> Self {
Self {
kind: TerminalLineKind::Comment,
..Self::command(text)
}
}
#[must_use]
pub fn tone(mut self, tone: TerminalLineTone) -> Self {
self.tone = tone;
self
}
pub fn class_name(&self) -> String {
let kind = format!("ui-terminal__line--{}", self.kind.as_str());
let tone = (self.tone != TerminalLineTone::Default)
.then(|| format!("ui-terminal__line--{}", self.tone.as_str()));
compose([
Some("ui-terminal__line"),
Some(kind.as_str()),
tone.as_deref(),
])
}
}
pub fn terminal_copy_value(lines: &[TerminalLine]) -> String {
lines
.iter()
.filter(|line| line.kind == TerminalLineKind::Command)
.map(|line| line.text.as_str())
.collect::<Vec<_>>()
.join("\n")
}
#[derive(Props, Clone, PartialEq)]
pub struct TerminalProps {
#[props(default)]
pub lines: Vec<TerminalLine>,
#[props(into)]
pub title: Option<String>,
#[props(into)]
pub label: Option<String>,
#[props(default = TERMINAL_DEFAULT_PROMPT.to_string(), into)]
pub prompt: String,
#[props(default)]
pub copy: bool,
#[props(into)]
pub copy_value: Option<String>,
#[props(into)]
pub copy_label: Option<String>,
#[props(into)]
pub copied_label: Option<String>,
#[props(into)]
pub error_label: Option<String>,
pub on_copy: Option<Callback<String, bool>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = figure)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Terminal(props: TerminalProps) -> Element {
let value = props
.copy_value
.clone()
.unwrap_or_else(|| terminal_copy_value(&props.lines));
let title = optional_text(props.title.as_deref());
let body_label = optional_text(props.label.as_deref())
.or_else(|| title.clone())
.unwrap_or_else(|| "Terminal".to_string());
let class = compose([Some("ui-terminal"), props.class.as_deref()]);
let prompt = props.prompt.clone();
rsx! {
figure { class, "data-slot": "terminal", ..props.attributes,
if let Some(title) = title {
figcaption { class: "ui-terminal__title", "data-slot": "terminal-title", {title} }
}
div { class: "ui-terminal__chrome", "data-slot": "terminal-chrome",
span { aria_hidden: "true", class: "ui-terminal__dots",
span { class: "ui-terminal__dot" }
span { class: "ui-terminal__dot" }
span { class: "ui-terminal__dot" }
}
span { class: "ui-terminal__actions", "data-slot": "terminal-actions",
if props.copy && !value.is_empty() {
CopyAction {
value: value.clone(),
label: optional_text(props.copy_label.as_deref())
.unwrap_or_else(|| "Copy commands".into()),
copied_label: optional_text(props.copied_label.as_deref())
.unwrap_or_else(|| "Commands copied to clipboard".into()),
error_label: optional_text(props.error_label.as_deref()).unwrap_or_else(|| {
"Could not copy commands. Select and copy them manually.".into()
}),
class: "ui-terminal__copy",
variant_class: "ui-button--ghost",
on_copy: props.on_copy,
"Copy"
}
}
}
}
pre {
aria_label: body_label,
class: "ui-terminal__body",
"data-slot": "terminal-body",
role: "region",
tabindex: "0",
if props.lines.is_empty() {
{props.children}
} else {
for line in props.lines.iter() {
span { class: line.class_name(), "data-kind": line.kind.as_str(),
if line.kind == TerminalLineKind::Command && !prompt.is_empty() {
span { aria_hidden: "true", class: "ui-terminal__prompt", {prompt.clone()} }
}
{line.text.clone()}
}
}
}
}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct InstallCommandProps {
#[props(into)]
pub command: String,
#[props(into)]
pub label: Option<String>,
#[props(default = TERMINAL_DEFAULT_PROMPT.to_string(), into)]
pub prompt: String,
#[props(into)]
pub copy_label: Option<String>,
#[props(into)]
pub copied_label: Option<String>,
#[props(into)]
pub error_label: Option<String>,
pub on_copy: Option<Callback<String, bool>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
}
#[component]
pub fn InstallCommand(props: InstallCommandProps) -> Element {
let authored = optional_text(props.label.as_deref());
let group_label = authored
.clone()
.unwrap_or_else(|| "Install command".to_string());
let copy_label = optional_text(props.copy_label.as_deref()).unwrap_or_else(|| {
authored.as_ref().map_or_else(
|| "Copy install command".to_string(),
|label| format!("Copy {label}"),
)
});
let class = compose([Some("ui-install-command"), props.class.as_deref()]);
rsx! {
div {
aria_label: group_label.clone(),
class,
"data-slot": "install-command",
role: "group",
..props.attributes,
span {
aria_label: group_label,
class: "ui-install-command__scroll",
"data-slot": "install-command-scroll",
role: "region",
tabindex: "0",
if !props.prompt.is_empty() {
span { aria_hidden: "true", class: "ui-install-command__prompt", {props.prompt.clone()} }
}
code { class: "ui-install-command__code", {props.command.clone()} }
}
if optional_text(Some(&props.command)).is_some() {
span { class: "ui-install-command__actions", "data-slot": "install-command-actions",
CopyAction {
value: props.command.clone(),
label: copy_label,
copied_label: optional_text(props.copied_label.as_deref())
.unwrap_or_else(|| "Command copied to clipboard".into()),
error_label: optional_text(props.error_label.as_deref()).unwrap_or_else(|| {
"Could not copy the command. Select and copy it manually.".into()
}),
class: "ui-install-command__copy",
variant_class: "ui-button--ghost",
on_copy: props.on_copy,
"Copy"
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::{TerminalLine, TerminalLineTone, terminal_copy_value};
#[test]
fn copies_only_command_lines() {
let lines = vec![
TerminalLine::command("pnpm install"),
TerminalLine::output("done").tone(TerminalLineTone::Success),
TerminalLine::comment("# next"),
TerminalLine::command("pnpm dev"),
];
assert_eq!(terminal_copy_value(&lines), "pnpm install\npnpm dev");
assert_eq!(terminal_copy_value(&[]), "");
}
#[test]
fn line_classes_follow_lumen_core() {
assert_eq!(
TerminalLine::command("x").class_name(),
"ui-terminal__line ui-terminal__line--command"
);
assert_eq!(
TerminalLine::output("x")
.tone(TerminalLineTone::Danger)
.class_name(),
"ui-terminal__line ui-terminal__line--output ui-terminal__line--danger"
);
}
}