52 lines
1.7 KiB
Rust
52 lines
1.7 KiB
Rust
//! `/btw` overlay: the side question and its answer; esc closes (and cancels a pending request).
|
|
|
|
use crate::theme;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::text::{Line, Span};
|
|
use ratatui::widgets::{Block, Clear, Paragraph, Wrap};
|
|
|
|
pub struct Btw {
|
|
pub q: String,
|
|
/// None while the request is running
|
|
pub a: Option<Result<String, String>>,
|
|
pub scroll: u16,
|
|
pub task: tokio::task::JoinHandle<()>,
|
|
}
|
|
|
|
impl Btw {
|
|
pub fn close(self) {
|
|
self.task.abort();
|
|
}
|
|
|
|
pub fn draw(&self, f: &mut ratatui::Frame) {
|
|
let th = theme::t();
|
|
let a = f.area();
|
|
let w = (a.width * 7 / 10).clamp(40, 110).min(a.width);
|
|
let h = (a.height * 7 / 10).clamp(8, 40).min(a.height);
|
|
let r = Rect::new(a.x + (a.width - w) / 2, a.y + (a.height - h) / 2, w, h);
|
|
let mut body = vec![
|
|
Line::styled(format!("> {}", self.q), th.dim()),
|
|
Line::default(),
|
|
];
|
|
match &self.a {
|
|
None => body.push(Line::styled("thinking…", th.dim())),
|
|
Some(Ok(t)) => body.extend(t.lines().map(|l| Line::raw(l.to_string()))),
|
|
Some(Err(e)) => body.push(Line::styled(e.clone(), th.err())),
|
|
}
|
|
f.render_widget(Clear, r);
|
|
f.render_widget(
|
|
Paragraph::new(body)
|
|
.wrap(Wrap { trim: false })
|
|
.scroll((self.scroll, 0))
|
|
.block(
|
|
Block::bordered()
|
|
.border_style(th.dim())
|
|
.padding(ratatui::widgets::Padding::horizontal(1))
|
|
.title(Span::styled(" btw ", th.dim()))
|
|
.title_bottom(Span::styled(" ↑↓ scroll · esc close ", th.dim())),
|
|
),
|
|
r,
|
|
);
|
|
}
|
|
}
|