TUI: permission prompt, /diff review, /stats dashboard, lens/context/MCP side panel
This commit is contained in:
1 parent
4a8e920ab8
commit
5fd7044a2f
15 files changed
+945
-2
No files matched your search
@@ -0,0 +1,121 @@
|
||||
//! Permission popup for bash/write/edit.
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
/// Some = decided.
|
||||
pub fn key(&mut self, k: KeyEvent) -> Option<Decision> {
|
||||
match k.code {
|
||||
KeyCode::Char('y' | 'Y') => Some(Decision::Once),
|
||||
KeyCode::Char('a' | 'A') => Some(Decision::Always),
|
||||
KeyCode::Char('n' | 'N') | KeyCode::Esc => Some(Decision::Deny),
|
||||
KeyCode::Up | KeyCode::Left => {
|
||||
self.sel = (self.sel + 2) % 3;
|
||||
None
|
||||
}
|
||||
KeyCode::Down | KeyCode::Right | KeyCode::Tab => {
|
||||
self.sel = (self.sel + 1) % 3;
|
||||
None
|
||||
}
|
||||
KeyCode::Enter => Some([Decision::Once, Decision::Always, Decision::Deny][self.sel]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(d: Decision) -> &'static str {
|
||||
match d {
|
||||
Decision::Once => "allowed",
|
||||
Decision::Always => "allowed always",
|
||||
Decision::Deny => "denied",
|
||||
}
|
||||
}
|
||||
|
||||
/// Centered in the first `bottom` rows.
|
||||
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 = [
|
||||
"[y] allow once".to_string(),
|
||||
format!("[a] allow always: {}", self.req.rule),
|
||||
"[n] deny".to_string(),
|
||||
];
|
||||
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".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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user