use crossterm::event::{KeyCode, KeyEvent};
use dugix_core::app::AppCommand;
use dugix_core::tools::Decision;
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use crate::app_view::AppView;
use super::chrome::{self, PopupFrame, hint, keys};
use crate::ui::diff::{self, escaped_rows};
use crate::ui::style;
fn request_lines(
tool: &str,
action: &str,
summary: &str,
class: Option<&str>,
width: usize,
theme: &crate::theme::Theme,
) -> Vec<Line<'static>> {
let dim = Style::default().fg(theme.fg_dim);
let normal = Style::default().fg(theme.fg);
let (evidence, summary) = summary
.split_once('\n')
.filter(|(head, _)| head.starts_with("in_scope ") || head.starts_with("risk: "))
.unwrap_or(("", summary));
let (head, body) = summary.split_once('\n').unwrap_or((summary, ""));
let is_diff = body.starts_with("--- ") || body.starts_with("diff --git ");
let parsed_counts = head.rsplit_once(" (+").and_then(|(label, counts)| {
let (added, deleted) = counts.strip_suffix(')')?.split_once(" −")?;
Some((
label,
added.parse::<usize>().ok()?,
deleted.parse::<usize>().ok()?,
))
});
let label = if tool == "fs" {
parsed_counts
.map_or(head, |(label, _, _)| label)
.to_string()
} else {
format!("{tool}{}{action}", style::SEP)
};
let mut header = escaped_rows(&label, width, normal, dim);
if is_diff || parsed_counts.is_some() {
let (added, deleted) = parsed_counts.map_or_else(
|| diff::counts(body),
|(_, added, deleted)| (added, deleted),
);
let line = header.last_mut().expect("request has a header");
line.spans.push(Span::styled(
format!(" +{added}"),
Style::default().fg(theme.success),
));
line.spans.push(Span::styled(
format!(" −{deleted}"),
Style::default().fg(theme.error),
));
}
if let Some(class) = class {
let badge_style = match class {
"runs a command" => Style::default().fg(theme.accent2),
"destructive" => Style::default()
.fg(theme.error)
.add_modifier(Modifier::BOLD),
"reaches outside" => Style::default()
.fg(theme.warning)
.add_modifier(Modifier::BOLD),
_ => dim,
};
let badge = escaped_rows(class, width, badge_style, dim).remove(0);
let line = header.last_mut().expect("request has a header");
*line = chrome::fit_line(line.clone(), width.saturating_sub(badge.width() + 1));
let padding = width.saturating_sub(line.width() + badge.width()).max(1);
line.spans.push(Span::raw(" ".repeat(padding)));
line.spans.extend(badge.spans);
}
header.push(Line::default());
if !evidence.is_empty() {
header.extend(escaped_rows(evidence, width, dim, dim));
}
if is_diff {
header.extend(diff::render(
body,
width,
theme,
diff::Options {
max_rows: None,
context: 3,
hint: "",
},
));
} else {
let text = if tool == "fs" { body } else { summary };
for line in text.split('\n').filter(|line| !line.is_empty()) {
let line = line.strip_prefix("background: ").unwrap_or(line);
let color = if line.starts_with("could not parse safely:")
|| line.starts_with("this tool's definition changed")
{
Style::default().fg(theme.warning)
} else if line.starts_with("$ ") {
normal.add_modifier(Modifier::BOLD)
} else if line.starts_with("in ") {
dim
} else {
normal
};
header.extend(escaped_rows(line, width, color, dim));
}
}
header
}
fn wave_lines(summary: &str, width: usize, theme: &crate::theme::Theme) -> Vec<Line<'static>> {
let mut sections: Vec<(&str, &str, &str, String)> = Vec::new();
for line in summary.split('\n').skip(1) {
if let Some((number, call)) = line
.split_once(". ")
.filter(|(number, _)| number.parse::<usize>().is_ok())
{
let (tool, action) = call.split_once(' ').unwrap_or((call, ""));
sections.push((number, tool, action, String::new()));
} else if let Some((_, _, _, body)) = sections.last_mut() {
if !body.is_empty() {
body.push('\n');
}
body.push_str(line.strip_prefix(" ").unwrap_or(line));
}
}
let mut lines = Vec::new();
for (number, tool, action, body) in sections {
if !lines.is_empty() {
lines.push(Line::default());
}
let mut section =
request_lines(tool, action, &body, None, width.saturating_sub(5), theme).into_iter();
let Some(mut head) = section.next() else {
continue;
};
let section_style = style::section(theme, "", 0).spans[0].style;
for span in &mut head.spans {
if span.style.fg == Some(theme.fg) {
span.style = section_style;
}
}
head.spans.insert(
0,
Span::styled(
format!("{} {number} ", style::glyph::SECTION),
section_style,
),
);
lines.push(head);
lines.extend(section);
while lines.last().is_some_and(|line| line.width() == 0) {
lines.pop();
}
}
lines
}
fn explain(tool: &str, action: &str, text: &str) -> String {
let text = text
.split_once('\n')
.filter(|(head, _)| head.starts_with("in_scope ") || head.starts_with("risk: "))
.map_or(text, |(_, rest)| rest);
let (head, body) = text.split_once('\n').unwrap_or((text, ""));
let (label, counts) = head.rsplit_once(" (+").map_or((head, ""), |(l, c)| (l, c));
let path = label.split_once(' ').map_or(label, |(_, path)| path);
let (added, deleted) = counts
.strip_suffix(')')
.and_then(|counts| counts.split_once(" −"))
.map_or_else(
|| {
let (added, deleted) = diff::counts(body);
(added.to_string(), deleted.to_string())
},
|(added, deleted)| (added.to_string(), deleted.to_string()),
);
let (added, deleted) = (added.as_str(), deleted.as_str());
let lines = |n: &str| {
if n == "1" {
"1 line".to_string()
} else {
format!("{n} lines")
}
};
match (tool, action) {
("wave", _) => {
format!("The agent wants to make these {action} together. Each is shown below.")
}
("fs", "edit") => format!(
"The agent wants to change {path}: {} added, {} removed.",
lines(added),
lines(deleted)
),
("fs", "write") if body.contains("@@ -0,0 ") || !body.contains("\n-") => {
format!("The agent wants to create {path} with {}.", lines(added))
}
("fs", "write") => format!(
"The agent wants to replace the contents of {path}: {} added, {} removed.",
lines(added),
lines(deleted)
),
("fs", "delete") => format!("The agent wants to delete {path} ({}).", lines(deleted)),
("fs", "move") => format!("The agent wants to {head}."),
("shell", _) => "The agent wants to run this command:".to_string(),
("proc", _) => "The agent wants to start this in the background:".to_string(),
("task", _) => {
"The agent wants a sub-agent to edit files in its own git worktree:".to_string()
}
_ => format!("The agent wants to use {tool}{}{action}:", style::SEP),
}
}
fn insert_intro(
lines: &mut Vec<Line<'static>>,
request: &dugix_core::tools::ApprovalRequest,
text: &str,
width: usize,
theme: &crate::theme::Theme,
) {
let wave = request.tool == "wave";
let connector = request.class.as_deref() == Some(dugix_core::connectors::exec::CLASS);
let at = if wave {
0
} else {
lines
.iter()
.position(|line| line.width() == 0)
.unwrap_or(lines.len())
};
let dim = Style::default().fg(theme.fg_dim);
let sentence = if connector {
format!(
"The model wants to use {} from your {} connector. Check what it will send:",
request.action, request.tool
)
} else {
explain(&request.tool, &request.action, text)
};
let mut intro: Vec<Line<'static>> = crate::ui::cards::wrap_plain(&sentence, width)
.iter()
.flat_map(|row| escaped_rows(row, width, Style::default().fg(theme.fg), dim))
.collect();
if wave {
intro.push(Line::default());
} else {
intro.insert(0, Line::default());
}
lines.splice(at..at, intro);
}
fn shell_unread(app: &AppView) -> bool {
app.approval_request.as_ref().is_some_and(|request| {
request.tool == "shell"
|| (request.tool == "wave"
&& request.summary.lines().next().is_some_and(|counts| {
counts
.split(", ")
.any(|count| count.ends_with(" command") || count.ends_with(" commands"))
}))
}) && app.approval_max_scroll.get() > 0
&& !app.approval_seen_end.get()
}
fn content_lines(app: &AppView, width: usize) -> Option<Vec<Line<'static>>> {
let request = app.approval_request.as_ref()?;
let text = request.detail.as_deref().unwrap_or(&request.summary);
let mut lines = if request.tool == "wave" {
wave_lines(text, width, &app.theme)
} else {
request_lines(
&request.tool,
&request.action,
text,
request.class.as_deref(),
width,
&app.theme,
)
};
insert_intro(&mut lines, request, text, width, &app.theme);
Some(lines)
}
fn panel_block(app: &AppView) -> ratatui::widgets::Block<'static> {
chrome::popup_block_focused("", &app.theme, true, chrome::Tone::Normal)
}
#[must_use]
pub fn docked_height(app: &AppView, column: Rect) -> u16 {
let frame = panel_block(app).inner(Rect::new(0, 0, column.width, 100));
let chrome_rows = 100 - frame.height;
let Some(lines) = content_lines(app, usize::from(frame.width)) else {
return 0;
};
let wanted = u16::try_from(lines.len())
.unwrap_or(u16::MAX)
.saturating_add(2)
.saturating_add(chrome_rows);
let cap = (column.height.saturating_mul(55) / 100).max(8);
wanted.min(cap).min(column.height.saturating_sub(2)).max(4)
}
#[cfg(test)]
pub fn render(f: &mut Frame, app: &AppView) {
let column = crate::ui::reading_column(f.area(), &app.settings.column_width);
let height = docked_height(app, column);
let area = Rect {
y: column.bottom().saturating_sub(height.saturating_add(1)),
height,
..column
};
render_in(f, app, area);
}
fn key_hints(
app: &AppView,
request: &dugix_core::tools::ApprovalRequest,
wave: bool,
connector: bool,
) -> Vec<chrome::Hint> {
let mut hints = Vec::new();
if shell_unread(app) {
hints.push(hint(keys::MOVE, "read to the end to approve"));
} else {
hints.push(hint("y", "approve"));
if request.allow_session {
hints.push(hint("a", "this session"));
}
if connector {
hints.push(hint("A", "always"));
}
}
if wave {
hints.push(hint("r", "one by one"));
}
hints.extend([hint("n", "deny"), hint(keys::ESC, "deny")]);
hints
}
pub fn render_in(f: &mut Frame, app: &AppView, area: Rect) {
let Some(request) = &app.approval_request else {
return;
};
app.approval_area.set(Some(area));
let wave = request.tool == "wave";
let dim = Style::default().fg(app.theme.fg_dim);
let width = usize::from(panel_block(app).inner(area).width);
let Some(lines) = content_lines(app, width) else {
return;
};
let inner = panel_block(app).inner(area);
let height = usize::from(inner.height).saturating_sub(2);
let max_scroll = lines.len().saturating_sub(height);
let offset = app.approval_scroll.get().min(max_scroll);
app.approval_scroll.set(offset);
app.approval_max_scroll.set(max_scroll);
app.approval_page_height
.set(height.saturating_sub(1).max(1));
if offset == max_scroll && height > 0 && width > 0 {
app.approval_seen_end.set(true);
}
let queued = app.approval_count();
let title = if wave {
format!(
"Approve {}{}{}",
request.action,
style::SEP,
request.summary.lines().next().unwrap_or_default()
)
} else {
"Approve".to_string()
};
let title = escaped_rows(
&title,
usize::MAX,
Style::default().fg(app.theme.accent),
dim,
)
.remove(0)
.to_string();
let connector = request.class.as_deref() == Some(dugix_core::connectors::exec::CLASS);
let mut count = Vec::new();
if queued > 1 {
count.push(format!("1 of {queued}"));
}
if let Some(percent) = offset.saturating_mul(100).checked_div(max_scroll) {
count.push(format!("{percent}%"));
}
let count = count.join(style::SEP);
let hints = key_hints(app, request, wave, connector);
chrome::render_popup(
f,
area,
&PopupFrame {
title: &title,
count: &count,
hints: &hints,
tone: chrome::Tone::Normal,
focused: true,
},
app,
);
if app
.approval_opened_at
.is_some_and(|opened| opened.elapsed() < app.approval_delay)
{
f.buffer_mut().set_style(
Rect::new(area.x, area.bottom().saturating_sub(1), area.width, 1),
Style::default().add_modifier(Modifier::DIM),
);
}
let visible = if offset < max_scroll {
height.saturating_sub(1)
} else {
height
};
let end = offset.saturating_add(visible).min(lines.len());
let more = lines.len() - end;
let mut visible: Vec<_> = lines.into_iter().skip(offset).take(visible).collect();
if more > 0 && height > 0 {
visible.push(Line::from(Span::styled(
format!("… {more} more lines"),
dim,
)));
}
let body = ratatui::layout::Rect {
height: u16::try_from(height).unwrap_or(u16::MAX),
..inner
};
f.render_widget(Paragraph::new(visible), body);
}
pub fn handle_key(app: &mut AppView, key: KeyEvent) -> anyhow::Result<()> {
let page = app.approval_page_height.get();
let (up, amount) = match key.code {
KeyCode::Up | KeyCode::Char('k') if !super::is_chord(key) => (true, 1),
KeyCode::Down | KeyCode::Char('j') if !super::is_chord(key) => (false, 1),
KeyCode::PageUp => (true, page),
KeyCode::PageDown | KeyCode::Char(' ') => (false, page),
KeyCode::Home | KeyCode::Char('g') if !super::is_chord(key) => (true, usize::MAX),
KeyCode::End | KeyCode::Char('G') if !super::is_chord(key) => (false, usize::MAX),
_ => (false, 0),
};
if amount > 0 {
let offset = app.approval_scroll.get();
app.approval_scroll.set(if up {
offset.saturating_sub(amount)
} else {
offset
.saturating_add(amount)
.min(app.approval_max_scroll.get())
});
return Ok(());
}
let decision = match key.code {
KeyCode::Esc => Decision::Deny,
KeyCode::Char('y') if !super::is_chord(key) => Decision::Allow,
KeyCode::Char('a')
if !super::is_chord(key)
&& app
.approval_request
.as_ref()
.is_some_and(|request| request.allow_session) =>
{
Decision::AllowForSession
}
KeyCode::Char('A')
if !super::is_chord(key)
&& app.approval_request.as_ref().is_some_and(|request| {
request.class.as_deref() == Some(dugix_core::connectors::exec::CLASS)
}) =>
{
Decision::AllowAlways
}
KeyCode::Char('n') if !super::is_chord(key) => Decision::Deny,
KeyCode::Char('r')
if !super::is_chord(key)
&& app
.approval_request
.as_ref()
.is_some_and(|request| request.tool == "wave") =>
{
Decision::ReviewEach
}
_ => return Ok(()),
};
if decision != Decision::Deny
&& ((decision != Decision::ReviewEach && shell_unread(app))
|| app
.approval_opened_at
.is_some_and(|opened| opened.elapsed() < app.approval_delay))
{
return Ok(());
}
app.execute(AppCommand::Approve { decision })
}
#[cfg(test)]
mod tests {
use super::*;
use dugix_core::app::{App, Popup};
use dugix_core::db::Db;
use dugix_core::space::Space;
use dugix_core::tools::ApprovalRequest;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use std::time::{Duration, Instant};
fn wave_app(summary: String) -> (AppView, tokio::sync::oneshot::Receiver<Decision>) {
let root = std::env::temp_dir().join(format!("dugix-wave-popup-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&root).unwrap();
let mut app = AppView::new(App::new(
Db::open_in_memory().unwrap(),
Some("k"),
Space { root },
));
let (sender, receiver) = tokio::sync::oneshot::channel();
app.approvals.pending = Some(sender);
app.approvals.pending_request = Some(ApprovalRequest {
tool: "wave".into(),
class: None,
detail: None,
action: "2 calls".into(),
summary,
allow_session: true,
});
app.sync_approval_popup();
(app, receiver)
}
fn draw(app: &AppView) {
Terminal::new(TestBackend::new(120, 24))
.unwrap()
.draw(|f| render(f, app))
.unwrap();
}
#[test]
fn one_time_wave_hides_and_ignores_session_approval() {
let (mut app, mut receiver) = wave_app("1 delete".into());
app.approval_request.as_mut().unwrap().allow_session = false;
let mut terminal = Terminal::new(TestBackend::new(120, 24)).unwrap();
terminal.draw(|frame| render(frame, &app)).unwrap();
let screen: String = terminal
.backend()
.buffer()
.content
.iter()
.map(|cell| cell.symbol())
.collect();
assert!(!screen.contains("a this session"));
handle_key(&mut app, KeyCode::Char('a').into()).unwrap();
assert!(receiver.try_recv().is_err());
handle_key(&mut app, KeyCode::Char('y').into()).unwrap();
assert_eq!(receiver.try_recv().unwrap(), Decision::Allow);
let root = app.space.root.clone();
drop(app);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn wave_keys_map_to_combined_decisions_and_keep_the_open_delay() {
for (key, expected) in [
(KeyCode::Char('y'), Decision::Allow),
(KeyCode::Char('a'), Decision::AllowForSession),
(KeyCode::Char('r'), Decision::ReviewEach),
(KeyCode::Char('n'), Decision::Deny),
(KeyCode::Esc, Decision::Deny),
] {
let (mut app, mut receiver) = wave_app(
"1 edit, 1 command\n1. fs write\n write one\n2. shell run\n $ true".into(),
);
let root = app.space.root.clone();
draw(&app);
app.approval_delay = Duration::from_millis(400);
app.approval_opened_at = Some(Instant::now() + Duration::from_secs(1));
handle_key(&mut app, key.into()).unwrap();
if expected != Decision::Deny {
assert!(receiver.try_recv().is_err());
assert_eq!(app.popup, Popup::Approval);
app.approval_opened_at = Some(Instant::now() - Duration::from_secs(1));
handle_key(&mut app, key.into()).unwrap();
}
assert_eq!(receiver.try_recv().unwrap(), expected);
assert_eq!(app.popup, Popup::None);
assert!(app.approvals.pending.is_none());
drop(app);
std::fs::remove_dir_all(root).unwrap();
}
}
#[test]
fn combined_shell_requires_scrolling_for_allow_but_not_review_or_denial() {
for (key, expected) in [
(KeyCode::Char('y'), Decision::Allow),
(KeyCode::Char('a'), Decision::AllowForSession),
(KeyCode::Char('r'), Decision::ReviewEach),
(KeyCode::Esc, Decision::Deny),
] {
let summary = format!(
"1 edit, 1 command\n1. fs edit\n{}\n2. shell run\n $ echo LAST_COMMAND",
" +a line of diff\n".repeat(50)
);
let (mut app, mut receiver) = wave_app(summary);
let root = app.space.root.clone();
draw(&app);
assert!(app.approval_max_scroll.get() > 0);
assert!(!app.approval_seen_end.get());
handle_key(&mut app, key.into()).unwrap();
if matches!(expected, Decision::Allow | Decision::AllowForSession) {
assert!(receiver.try_recv().is_err());
while app.approval_scroll.get() < app.approval_max_scroll.get() {
handle_key(&mut app, KeyCode::PageDown.into()).unwrap();
draw(&app);
}
assert!(app.approval_seen_end.get());
handle_key(&mut app, key.into()).unwrap();
}
assert_eq!(receiver.try_recv().unwrap(), expected);
drop(app);
std::fs::remove_dir_all(root).unwrap();
}
}
}