fix(cli-rs): toast terminal list failures for commands only

TS shows "Unable to load terminal" when terminal.toggle, .select, or /terminal fail, and only logs failed refreshes from events and the session mount.
This commit is contained in:
Simon Klee 2026-11-04 22:04:57 +01:00
parent 3bae03dbe2
commit d759713d61
No known key found for this signature in database
GPG key ID: B91696044D47BEA3
4 changed files with 40 additions and 26 deletions

View file

@ -7,8 +7,8 @@ use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use opencode_ui::dialog::{
DIALOG_INSET, DialogOptions, DialogSize, FooterAction, FooterProps, HeaderProps, footer, header,
layout_view,
DIALOG_INSET, DialogOptions, DialogSize, FooterAction, FooterProps, HeaderProps, footer,
header, layout_view,
};
use opencode_ui::keymap::{KeymapLayer, TuiCommand};
use opencode_ui::toast::{ToastOptions, Variant};
@ -99,8 +99,7 @@ pub fn show(tui: &Tui) {
Rc::new(move || {
let Some(cx) = cx.upgrade() else { return };
let mut action =
FooterAction::new(if copied.get() { "✓ copied" } else { "copy" })
.key("c");
FooterAction::new(if copied.get() { "✓ copied" } else { "copy" }).key("c");
action.on_trigger = Some(run.clone());
if copied.get() {
action.color = Some(cx.theme.text.feedback.success.base);

View file

@ -23,13 +23,11 @@ pub struct Experiment {
/// In-flight features anyone can opt into. Each entry is temporary: an experiment either graduates (delete the
/// entry, make the behavior unconditional) or dies (delete the entry and the branch it gated). `new-designs`
/// graduated here: the Rust client draws only the new designs, though the TypeScript TUI still lists it.
pub const EXPERIMENTS: &[Experiment] = &[
Experiment {
id: "client-mcp",
title: "TUI tools",
description: "Let the agent open terminals in this TUI (requires server support)",
},
];
pub const EXPERIMENTS: &[Experiment] = &[Experiment {
id: "client-mcp",
title: "TUI tools",
description: "Let the agent open terminals in this TUI (requires server support)",
}];
fn options(tui: &Tui) -> Vec<SelectOption<&'static str>> {
EXPERIMENTS

View file

@ -1079,8 +1079,7 @@ fn open(local: &Rc<Local>, provider: Option<String>, filter: String) {
);
}
impl Local {
}
impl Local {}
// The select of `DialogConnectedModels`.
fn build(
@ -1097,7 +1096,13 @@ fn build(
let provider = provider.clone();
Rc::new(move |query: &str| {
weak.upgrade().map_or_else(Vec::new, |local| {
options(&local, provider.as_deref(), query, &favorites_at_open, marker)
options(
&local,
provider.as_deref(),
query,
&favorites_at_open,
marker,
)
})
})
};
@ -1236,7 +1241,11 @@ fn options(
option.footer_fg = Some(marker);
}
let provider = providers.iter().find(|item| item.id == model.provider_id);
option.detail = Some(detail(model, true, usage_label(usage.as_ref(), model, provider)));
option.detail = Some(detail(
model,
true,
usage_label(usage.as_ref(), model, provider),
));
};
let section = |items: &[Model], category: &str| -> Vec<SelectOption<Model>> {
if !show_sections {
@ -1364,7 +1373,8 @@ fn usage_label(
));
}
let paid = !model.cost.iter().all(|cost| cost.input == 0.0);
(crate::dialogs::usage::served_by_console(provider) && paid).then(|| ("No limit".to_owned(), false))
(crate::dialogs::usage::served_by_console(provider) && paid)
.then(|| ("No limit".to_owned(), false))
}
/// `ModelDetail`: the selected model's price (unless `price` is false), input modalities, context window, and
@ -1426,7 +1436,11 @@ pub fn detail(model: &ModelInfo, price: bool, usage: Option<(String, bool)>) ->
}
None => {
if price {
row.add(&Text::new(app, cost.as_str()).fg(theme.text.muted).wrap(WrapMode::None));
row.add(
&Text::new(app, cost.as_str())
.fg(theme.text.muted)
.wrap(WrapMode::None),
);
} else {
row.justify(Justify::Start);
}

View file

@ -208,14 +208,14 @@ pub fn event(tui: &Tui, event: &V2Event) {
_ => return,
};
if tui.terminals.available() && tui.terminals.lists.borrow().contains_key(&session.0) {
refresh(tui, session, |_| {});
refresh(tui, session, false, |_| {});
}
}
/// The session frame of `session` mounted: list its terminals (`createResource` in the frame).
pub fn session_mounted(tui: &Tui, session: &SessionID) {
if tui.terminals.available() {
refresh(tui, session, |_| {});
refresh(tui, session, false, |_| {});
}
}
@ -252,14 +252,15 @@ fn check(tui: &Tui) {
sessions.push(id.0);
}
for session in sessions {
refresh(&tui, &SessionID(session), |_| {});
refresh(&tui, &SessionID(session), false, |_| {});
}
},
);
}
/// Lists a session's terminals, and clears its selection when the selected one is gone. `then` runs after.
fn refresh(tui: &Tui, session: &SessionID, then: impl FnOnce(&Tui) + 'static) {
/// Lists a session's terminals, and clears its selection when the selected one is gone. `then` runs after. A failure
/// is logged; for a command (`toast`) it also shows "Unable to load terminal", as the TS commands do.
fn refresh(tui: &Tui, session: &SessionID, toast: bool, then: impl FnOnce(&Tui) + 'static) {
tui.terminals
.lists
.borrow_mut()
@ -283,7 +284,9 @@ fn refresh(tui: &Tui, session: &SessionID, then: impl FnOnce(&Tui) + 'static) {
Ok(list) => list,
Err(error) => {
log::error!("Failed to refresh persistent terminal panes: {error}");
tui.toasts.error("Unable to load terminal");
if toast {
tui.toasts.error("Unable to load terminal");
}
return;
}
};
@ -367,7 +370,7 @@ fn create(tui: &Tui, session: &SessionID) {
match result {
Ok(terminal) => {
let id = session.0.clone();
refresh(&tui, &session, move |tui| {
refresh(&tui, &session, true, move |tui| {
select(tui, &id, Some(&terminal.id.0), true);
});
}
@ -397,7 +400,7 @@ fn toggle(tui: &Tui) {
return;
}
let session = id.clone();
refresh(tui, &id, move |tui| {
refresh(tui, &id, true, move |tui| {
match tui.terminals.list(&session.0).last() {
Some(terminal) => select(tui, &session.0, Some(&terminal.id.0), true),
None => create(tui, &session),
@ -417,7 +420,7 @@ fn pick(tui: &Tui) {
if let Some(picker) = picker {
picker(tui);
}
refresh(tui, &id, |_| {});
refresh(tui, &id, true, |_| {});
}
/// `terminal.close`.