Files
hx/crates/hx-tui/src/permit_view.rs
T
johannesandClaude Sonnet 5.5 a94632ea3d chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record.
Tautology/stale/excuse comments are deleted (excuses filed as issues
#196-#236), decisions moved to docs/adr/0001-0034.

- scripts/lint-comments.sh + .comment-baseline: per-file comment count
  may only go down; wired into tests/all.sh
- .claude/skills/delete-comment: repeatable audit workflow
- cargo fmt applied to touched crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:37:32 +01:00

146 lines
4.8 KiB
Rust

use crate::theme;
use crossterm::event::{KeyCode, KeyEvent};
use hx_core::permit::{Decision, Request};
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, Paragraph, Wrap};
pub struct Prompt {
pub req: Request,
sel: usize,
}
impl Prompt {
pub fn new(req: Request) -> Self {
Self { req, sel: 0 }
}
fn can_always(&self) -> bool {
!self.req.rule.is_empty()
}
fn opts(&self) -> Vec<(Decision, String)> {
let mut v = vec![(Decision::Once, "[y] allow once".to_string())];
if self.can_always() {
let r = self
.req
.rule
.strip_prefix("bash:")
.unwrap_or(&self.req.rule);
let r = r.replace("\nbash:", ", ");
v.push((Decision::Always, format!("[a] allow always: {r}")));
}
if matches!(self.req.tool.as_str(), "write" | "edit") {
v.push((Decision::Edits, "[e] allow all edits this session".into()));
}
v.push((Decision::Deny, "[n] deny".into()));
v
}
pub fn key(&mut self, k: KeyEvent) -> Option<Decision> {
match k.code {
KeyCode::Char('y' | 'Y') => Some(Decision::Once),
KeyCode::Char('a' | 'A') if self.can_always() => Some(Decision::Always),
KeyCode::Char('e' | 'E') if matches!(self.req.tool.as_str(), "write" | "edit") => {
Some(Decision::Edits)
}
KeyCode::Char('n' | 'N') | KeyCode::Esc => Some(Decision::Deny),
KeyCode::Up | KeyCode::Left => {
let n = self.opts().len();
self.sel = (self.sel + n - 1) % n;
None
}
KeyCode::Down | KeyCode::Right | KeyCode::Tab => {
self.sel = (self.sel + 1) % self.opts().len();
None
}
KeyCode::Enter => Some(self.opts()[self.sel].0),
_ => None,
}
}
pub fn label(d: Decision) -> &'static str {
match d {
Decision::Once => "allowed",
Decision::Always => "allowed always",
Decision::Edits => "allowed all edits this session:",
Decision::Deny => "denied",
}
}
pub fn draw(&self, f: &mut ratatui::Frame, bottom: u16) {
let th = theme::t();
let a = f.area();
let w = (a.width * 3 / 5).clamp(44, 100).min(a.width);
let opts: Vec<String> = self.opts().into_iter().map(|o| o.1).collect();
let mut lines = vec![
Line::styled("hx wants to run:", th.dim()),
Line::default(),
Line::from(vec![
Span::styled(format!("{} ", self.req.tool), th.accent()),
Span::raw(self.req.detail.clone()),
]),
Line::default(),
];
lines.extend(opts.iter().enumerate().map(|(i, o)| {
let (mark, st) = if i == self.sel {
("▶ ", th.sel())
} else {
(" ", th.plain())
};
Line::styled(format!("{mark}{o}"), st)
}));
let inner = w.saturating_sub(4).max(1);
let h = (Paragraph::new(lines.clone())
.wrap(Wrap { trim: false })
.line_count(inner) as u16
+ 2)
.min(bottom)
.max(3);
let r = Rect::new((a.width - w) / 2, (bottom - h) / 2, w, h);
f.render_widget(Clear, r);
f.render_widget(
Paragraph::new(lines).wrap(Wrap { trim: false }).block(
Block::bordered()
.border_style(th.warn())
.padding(ratatui::widgets::Padding::horizontal(1))
.title(Span::styled(" permission ", th.warn()))
.title_bottom(Span::styled(" ↑↓ select · ⏎ confirm ", th.dim())),
),
r,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crossterm::event::KeyModifiers;
fn p() -> Prompt {
Prompt::new(Request {
tool: "bash".into(),
detail: "rm -rf x".into(),
rule: "bash:rm -rf x".into(),
})
}
fn k(c: KeyCode) -> KeyEvent {
KeyEvent::new(c, KeyModifiers::NONE)
}
#[test]
fn letters_and_selection() {
assert_eq!(p().key(k(KeyCode::Char('y'))), Some(Decision::Once));
assert_eq!(p().key(k(KeyCode::Char('a'))), Some(Decision::Always));
assert_eq!(p().key(k(KeyCode::Esc)), Some(Decision::Deny));
let mut q = p();
assert_eq!(q.key(k(KeyCode::Down)), None);
assert_eq!(q.key(k(KeyCode::Enter)), Some(Decision::Always));
let mut c = p();
c.req.rule.clear();
assert_eq!(c.key(k(KeyCode::Char('a'))), None);
c.key(k(KeyCode::Down));
assert_eq!(c.key(k(KeyCode::Enter)), Some(Decision::Deny));
}
}