tui: DocView full-screen scroll view; plan approval opens it instead of printing the plan in chat
This commit is contained in:
1 parent
531919a23c
commit
a5c7b580ea
4 files changed
+171
-20
No files matched your search
@@ -337,7 +337,7 @@ impl Tool for ExitPlan {
|
|||||||
json!({"type":"object","properties":{"plan":{"type":"string"}},"required":["plan"]}),
|
json!({"type":"object","properties":{"plan":{"type":"string"}},"required":["plan"]}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
async fn call(&self, _: Value, cx: &ToolCtx) -> ToolResult {
|
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
|
||||||
let on = || cx.state.allow.lock().unwrap_or_else(|e| e.into_inner());
|
let on = || cx.state.allow.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
if !on().plan_on() {
|
if !on().plan_on() {
|
||||||
return ToolResult::ok("not in plan mode; just do the work");
|
return ToolResult::ok("not in plan mode; just do the work");
|
||||||
@@ -353,7 +353,7 @@ impl Tool for ExitPlan {
|
|||||||
],
|
],
|
||||||
multi: false,
|
multi: false,
|
||||||
other: true,
|
other: true,
|
||||||
plan: None,
|
plan: a["plan"].as_str().map(Into::into),
|
||||||
};
|
};
|
||||||
match cx.state.choose(q).await {
|
match cx.state.choose(q).await {
|
||||||
Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => {
|
Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => {
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
//! Full-screen read-only text view with scrolling and a row of action keys. Used for plan approval;
|
||||||
|
//! meant to be reused for diffs (`lines` are plain styled lines).
|
||||||
|
|
||||||
|
use crate::{scroll::Scroll, theme};
|
||||||
|
use crossterm::event::{KeyCode, KeyEvent};
|
||||||
|
use ratatui::layout::Rect;
|
||||||
|
use ratatui::text::{Line, Span};
|
||||||
|
use ratatui::widgets::{Block, Clear, Paragraph, Wrap};
|
||||||
|
|
||||||
|
pub enum Act {
|
||||||
|
None,
|
||||||
|
/// An action key was pressed: its label.
|
||||||
|
Pick(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DocView {
|
||||||
|
title: String,
|
||||||
|
lines: Vec<Line<'static>>,
|
||||||
|
scroll: Scroll,
|
||||||
|
/// (key, label); esc picks the one with key `\x1b`
|
||||||
|
actions: Vec<(KeyCode, &'static str, &'static str)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DocView {
|
||||||
|
/// `actions`: (key, key name shown, label returned/shown).
|
||||||
|
pub fn new(
|
||||||
|
title: impl Into<String>,
|
||||||
|
lines: Vec<Line<'static>>,
|
||||||
|
actions: Vec<(KeyCode, &'static str, &'static str)>,
|
||||||
|
) -> Self {
|
||||||
|
let mut scroll = Scroll::default();
|
||||||
|
scroll.start();
|
||||||
|
Self {
|
||||||
|
title: title.into(),
|
||||||
|
lines,
|
||||||
|
scroll,
|
||||||
|
actions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn key(&mut self, k: KeyEvent) -> Act {
|
||||||
|
if let Some((_, _, l)) = self.actions.iter().find(|(c, _, _)| *c == k.code) {
|
||||||
|
return Act::Pick((*l).into());
|
||||||
|
}
|
||||||
|
let (p, h) = (self.scroll.page(), self.scroll.half_page());
|
||||||
|
match k.code {
|
||||||
|
KeyCode::Char('j') | KeyCode::Down => self.scroll.down(1),
|
||||||
|
KeyCode::Char('k') | KeyCode::Up => self.scroll.up(1),
|
||||||
|
KeyCode::PageDown | KeyCode::Char(' ') => self.scroll.down(p),
|
||||||
|
KeyCode::PageUp | KeyCode::Char('b') => self.scroll.up(p),
|
||||||
|
KeyCode::Char('d') => self.scroll.down(h),
|
||||||
|
KeyCode::Char('u') => self.scroll.up(h),
|
||||||
|
KeyCode::Char('g') | KeyCode::Home => self.scroll.start(),
|
||||||
|
KeyCode::Char('G') | KeyCode::End => self.scroll.end(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Act::None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wheel(&mut self, up: bool) {
|
||||||
|
if up {
|
||||||
|
self.scroll.up(3)
|
||||||
|
} else {
|
||||||
|
self.scroll.down(3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&mut self, f: &mut ratatui::Frame) {
|
||||||
|
let th = theme::t();
|
||||||
|
let a = f.area();
|
||||||
|
let (w, h) = (a.width * 9 / 10, a.height * 9 / 10);
|
||||||
|
let r = Rect::new(a.x + (a.width - w) / 2, a.y + (a.height - h) / 2, w, h);
|
||||||
|
let mut hint = vec![Span::styled(" ", th.dim())];
|
||||||
|
for (_, name, label) in &self.actions {
|
||||||
|
hint.push(Span::styled(format!("{name} "), th.accent()));
|
||||||
|
hint.push(Span::styled(format!("{label} · "), th.dim()));
|
||||||
|
}
|
||||||
|
hint.push(Span::styled("j/k pgup/pgdn g/G scroll ", th.dim()));
|
||||||
|
let block = Block::bordered()
|
||||||
|
.border_style(th.dim())
|
||||||
|
.title(Span::styled(format!(" {} ", self.title), th.accent()))
|
||||||
|
.title_bottom(Line::from(hint));
|
||||||
|
let inner = block.inner(r);
|
||||||
|
f.render_widget(Clear, r);
|
||||||
|
f.render_widget(block, r);
|
||||||
|
let p = Paragraph::new(self.lines.clone()).wrap(Wrap { trim: false });
|
||||||
|
let total = p.line_count(inner.width).min(u16::MAX as usize) as u16;
|
||||||
|
let top = self.scroll.resolve(total, inner.height);
|
||||||
|
f.render_widget(p.scroll((top, 0)), inner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crossterm::event::{KeyEventKind, KeyEventState, KeyModifiers};
|
||||||
|
|
||||||
|
fn k(c: KeyCode) -> KeyEvent {
|
||||||
|
KeyEvent {
|
||||||
|
code: c,
|
||||||
|
modifiers: KeyModifiers::NONE,
|
||||||
|
kind: KeyEventKind::Press,
|
||||||
|
state: KeyEventState::NONE,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn long_text_scrolls_and_keys_pick() {
|
||||||
|
let lines = (0..100).map(|i| Line::raw(format!("line {i}"))).collect();
|
||||||
|
let mut v = DocView::new("plan", lines, vec![(KeyCode::Enter, "enter", "Implement")]);
|
||||||
|
let show = |v: &mut DocView| {
|
||||||
|
let mut t = ratatui::Terminal::new(ratatui::backend::TestBackend::new(60, 20)).unwrap();
|
||||||
|
t.draw(|f| v.draw(f)).unwrap();
|
||||||
|
format!("{}", t.backend())
|
||||||
|
};
|
||||||
|
let s = show(&mut v);
|
||||||
|
assert!(s.contains("line 0") && !s.contains("line 60"), "{s}");
|
||||||
|
v.key(k(KeyCode::Char('G')));
|
||||||
|
assert!(show(&mut v).contains("line 99"));
|
||||||
|
assert!(matches!(v.key(k(KeyCode::Enter)), Act::Pick(l) if l == "Implement"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
pub mod agents;
|
pub mod agents;
|
||||||
pub mod btw_view;
|
pub mod btw_view;
|
||||||
pub mod choose;
|
pub mod choose;
|
||||||
|
pub mod doc_view;
|
||||||
pub mod editor;
|
pub mod editor;
|
||||||
pub mod permit_view;
|
pub mod permit_view;
|
||||||
pub mod picker;
|
pub mod picker;
|
||||||
@@ -172,6 +173,12 @@ struct App {
|
|||||||
tokio::sync::oneshot::Sender<hx_core::permit::Decision>,
|
tokio::sync::oneshot::Sender<hx_core::permit::Decision>,
|
||||||
)>,
|
)>,
|
||||||
review: Option<review::Review>,
|
review: Option<review::Review>,
|
||||||
|
/// plan view and where the chosen label goes (Implement / Keep planning / Feedback)
|
||||||
|
doc: Option<(
|
||||||
|
doc_view::DocView,
|
||||||
|
hx_core::Question,
|
||||||
|
tokio::sync::oneshot::Sender<Option<Vec<String>>>,
|
||||||
|
)>,
|
||||||
/// `/keys`: show every key as hx receives it until esc
|
/// `/keys`: show every key as hx receives it until esc
|
||||||
probe: bool,
|
probe: bool,
|
||||||
/// keyboard protocol was switched on (shift-enter is distinguishable)
|
/// keyboard protocol was switched on (shift-enter is distinguishable)
|
||||||
@@ -382,6 +389,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
|||||||
choose: None,
|
choose: None,
|
||||||
perm: None,
|
perm: None,
|
||||||
review: None,
|
review: None,
|
||||||
|
doc: None,
|
||||||
probe: false,
|
probe: false,
|
||||||
kbd,
|
kbd,
|
||||||
stats: None,
|
stats: None,
|
||||||
@@ -496,6 +504,22 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
|||||||
let _ = reply.send(d);
|
let _ = reply.send(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TEvent::Key(k) if k.kind != KeyEventKind::Release && app.doc.is_some() => {
|
||||||
|
if let Some((v, _, _)) = &mut app.doc
|
||||||
|
&& let doc_view::Act::Pick(l) = v.key(k)
|
||||||
|
&& let Some((_, mut q, reply)) = app.doc.take()
|
||||||
|
{
|
||||||
|
if l == "Feedback" {
|
||||||
|
q.plan = None;
|
||||||
|
q.text = "What should change in the plan?".into();
|
||||||
|
q.options = vec![];
|
||||||
|
app.choose = Some((choose::Choose::new(q), reply));
|
||||||
|
} else {
|
||||||
|
app.t.push(Item::Info(format!("plan: {l}")));
|
||||||
|
let _ = reply.send(Some(vec![l]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
TEvent::Key(k) if k.kind != KeyEventKind::Release && app.choose.is_some() => {
|
TEvent::Key(k) if k.kind != KeyEventKind::Release && app.choose.is_some() => {
|
||||||
if let Some((c, _)) = &mut app.choose
|
if let Some((c, _)) = &mut app.choose
|
||||||
&& let choose::Act::Done(ans) = c.key(k)
|
&& let choose::Act::Done(ans) = c.key(k)
|
||||||
@@ -541,9 +565,21 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
|||||||
Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)),
|
Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)),
|
||||||
Ui::Choose(q, reply) => {
|
Ui::Choose(q, reply) => {
|
||||||
if let Some(p) = &q.plan {
|
if let Some(p) = &q.plan {
|
||||||
app.t.push(Item::Plan(p.clone()));
|
let mut md = vec![];
|
||||||
|
transcript::markdown(p, &mut md);
|
||||||
|
let v = doc_view::DocView::new(
|
||||||
|
"plan",
|
||||||
|
md,
|
||||||
|
vec![
|
||||||
|
(KeyCode::Enter, "enter", "Implement"),
|
||||||
|
(KeyCode::Char('f'), "f", "Feedback"),
|
||||||
|
(KeyCode::Esc, "esc", "Keep planning"),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
app.doc = Some((v, q, reply));
|
||||||
|
} else {
|
||||||
|
app.choose = Some((choose::Choose::new(q), reply))
|
||||||
}
|
}
|
||||||
app.choose = Some((choose::Choose::new(q), reply))
|
|
||||||
}
|
}
|
||||||
Ui::Ev(e) => {
|
Ui::Ev(e) => {
|
||||||
if let Event::SubUsage(u) = &e {
|
if let Event::SubUsage(u) = &e {
|
||||||
@@ -950,6 +986,7 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
|
|||||||
|| app.sessions.is_some()
|
|| app.sessions.is_some()
|
||||||
|| app.choose.is_some()
|
|| app.choose.is_some()
|
||||||
|| app.review.is_some()
|
|| app.review.is_some()
|
||||||
|
|| app.doc.is_some()
|
||||||
|| app.perm.is_some()
|
|| app.perm.is_some()
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -1023,6 +1060,9 @@ fn select_mouse(app: &mut App, m: crossterm::event::MouseEvent) {
|
|||||||
fn wheel(app: &mut App, m: crossterm::event::MouseEvent, up: bool) {
|
fn wheel(app: &mut App, m: crossterm::event::MouseEvent, up: bool) {
|
||||||
let s = if let Some(agents::View::Detail { scroll, .. }) = &mut app.agents {
|
let s = if let Some(agents::View::Detail { scroll, .. }) = &mut app.agents {
|
||||||
scroll
|
scroll
|
||||||
|
} else if let Some((v, _, _)) = &mut app.doc {
|
||||||
|
v.wheel(up);
|
||||||
|
return;
|
||||||
} else if app.picker.is_some() || app.sessions.is_some() || app.choose.is_some() {
|
} else if app.picker.is_some() || app.sessions.is_some() || app.choose.is_some() {
|
||||||
return;
|
return;
|
||||||
} else if app.panel.is_some() && inside(app.areas.panel, m.column, m.row) {
|
} else if app.panel.is_some() && inside(app.areas.panel, m.column, m.row) {
|
||||||
@@ -1981,6 +2021,9 @@ fn draw(f: &mut ratatui::Frame, app: &mut App) {
|
|||||||
if let Some(r) = &app.review {
|
if let Some(r) = &app.review {
|
||||||
r.draw(f);
|
r.draw(f);
|
||||||
}
|
}
|
||||||
|
if let Some((v, _, _)) = &mut app.doc {
|
||||||
|
v.draw(f);
|
||||||
|
}
|
||||||
if let Some((p, _)) = &app.perm {
|
if let Some((p, _)) = &app.perm {
|
||||||
p.draw(f, input.y);
|
p.draw(f, input.y);
|
||||||
}
|
}
|
||||||
@@ -2695,6 +2738,7 @@ mod tests {
|
|||||||
choose: None,
|
choose: None,
|
||||||
perm: None,
|
perm: None,
|
||||||
review: None,
|
review: None,
|
||||||
|
doc: None,
|
||||||
probe: false,
|
probe: false,
|
||||||
kbd: false,
|
kbd: false,
|
||||||
stats: None,
|
stats: None,
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ pub enum Item {
|
|||||||
started: Instant,
|
started: Instant,
|
||||||
took: Option<Duration>,
|
took: Option<Duration>,
|
||||||
},
|
},
|
||||||
/// A finished plan, shown in full.
|
|
||||||
Plan(String),
|
|
||||||
Info(String),
|
Info(String),
|
||||||
Error(String),
|
Error(String),
|
||||||
/// Hook reports injected at turn end (`[lens] …`), shown as the lens block.
|
/// Hook reports injected at turn end (`[lens] …`), shown as the lens block.
|
||||||
@@ -353,10 +351,6 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
|||||||
Item::Info(s) => out.push(Line::styled(format!("{} {s}", th.sym.info), th.dim())),
|
Item::Info(s) => out.push(Line::styled(format!("{} {s}", th.sym.info), th.dim())),
|
||||||
Item::Lens(texts) => lens_block(texts, expanded, width, out),
|
Item::Lens(texts) => lens_block(texts, expanded, width, out),
|
||||||
Item::Error(s) => out.push(Line::styled(format!("{} {s}", th.sym.err), th.err())),
|
Item::Error(s) => out.push(Line::styled(format!("{} {s}", th.sym.err), th.err())),
|
||||||
Item::Tool { name, args, .. } if name == "exit_plan" => {
|
|
||||||
plan_block(args["plan"].as_str().unwrap_or_default(), width, out)
|
|
||||||
}
|
|
||||||
Item::Plan(p) => plan_block(p, width, out),
|
|
||||||
Item::Tool {
|
Item::Tool {
|
||||||
name,
|
name,
|
||||||
args,
|
args,
|
||||||
@@ -378,15 +372,6 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn plan_block(plan: &str, width: usize, out: &mut Vec<Line<'static>>) {
|
|
||||||
let th = theme::t();
|
|
||||||
let mut md = vec![];
|
|
||||||
markdown(plan, &mut md);
|
|
||||||
out.push(Line::styled(th.rule("plan", width), th.accent()));
|
|
||||||
lead(md, " ", th.plain(), out);
|
|
||||||
out.push(Line::styled(th.rule("", width), th.dim()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `┄ lens ┄┄┄` rule, then per report a summary line (errors red, all-pass green) and,
|
/// `┄ lens ┄┄┄` rule, then per report a summary line (errors red, all-pass green) and,
|
||||||
/// collapsed, the first findings; `ctrl-o` shows all.
|
/// collapsed, the first findings; `ctrl-o` shows all.
|
||||||
fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line<'static>>) {
|
fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line<'static>>) {
|
||||||
@@ -570,7 +555,7 @@ fn advance(cur: Option<usize>, l: &str) -> Option<usize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal markdown: fenced code, headings, bullets, inline `code` and **bold**.
|
/// Minimal markdown: fenced code, headings, bullets, inline `code` and **bold**.
|
||||||
fn markdown(s: &str, out: &mut Vec<Line<'static>>) {
|
pub(crate) fn markdown(s: &str, out: &mut Vec<Line<'static>>) {
|
||||||
let mut code = false;
|
let mut code = false;
|
||||||
for l in s.lines() {
|
for l in s.lines() {
|
||||||
if l.trim_start().starts_with("```") {
|
if l.trim_start().starts_with("```") {
|
||||||
|
|||||||
Reference in new issue
Block a user