2026-07-11 13:16:10 +07:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
use super::types::Overlay;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DirCache {
|
|
|
|
|
entries: Arc<RwLock<Vec<PathBuf>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DirCache {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
DirCache {
|
|
|
|
|
entries: Arc::new(RwLock::new(Vec::new())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn set(&self, paths: Vec<PathBuf>) {
|
|
|
|
|
let mut w = self.entries.write().await;
|
|
|
|
|
*w = paths;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ScrollState {
|
|
|
|
|
pub offset: usize,
|
|
|
|
|
pub max_visible: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScrollState {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
ScrollState {
|
|
|
|
|
offset: 0,
|
|
|
|
|
max_visible: 30,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scroll_up(&mut self) {
|
|
|
|
|
if self.offset > 0 {
|
|
|
|
|
self.offset -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scroll_down(&mut self, total: usize) {
|
|
|
|
|
let max_offset = total.saturating_sub(self.max_visible);
|
|
|
|
|
if self.offset < max_offset {
|
|
|
|
|
self.offset += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_max_visible(&mut self, max: usize) {
|
|
|
|
|
self.max_visible = max;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct InputState {
|
|
|
|
|
pub buffer: String,
|
|
|
|
|
pub cursor: usize,
|
|
|
|
|
pub history: Vec<String>,
|
|
|
|
|
pub history_idx: Option<usize>,
|
2026-07-11 22:10:17 +07:00
|
|
|
pub autocomplete_prefix: String,
|
|
|
|
|
pub autocomplete_candidates: Vec<String>,
|
|
|
|
|
pub autocomplete_idx: usize,
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-11 22:10:17 +07:00
|
|
|
const COMMANDS: &[&str] = &[
|
|
|
|
|
"/help",
|
|
|
|
|
"/quit",
|
|
|
|
|
"/clear",
|
|
|
|
|
"/lesson",
|
|
|
|
|
"/lesson ls",
|
|
|
|
|
"/lesson export",
|
|
|
|
|
"/lesson import",
|
|
|
|
|
"/mode chat",
|
|
|
|
|
"/mode bash",
|
|
|
|
|
"/mode help",
|
|
|
|
|
"/mode settings",
|
|
|
|
|
"/login",
|
|
|
|
|
];
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
impl InputState {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
InputState {
|
|
|
|
|
buffer: String::new(),
|
|
|
|
|
cursor: 0,
|
|
|
|
|
history: Vec::new(),
|
|
|
|
|
history_idx: None,
|
2026-07-11 22:10:17 +07:00
|
|
|
autocomplete_prefix: String::new(),
|
|
|
|
|
autocomplete_candidates: Vec::new(),
|
|
|
|
|
autocomplete_idx: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn tab_complete(&mut self) {
|
|
|
|
|
let trimmed = self.buffer.trim().to_string();
|
|
|
|
|
if trimmed.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !trimmed.starts_with('/') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let prefix = trimmed.to_lowercase();
|
|
|
|
|
|
|
|
|
|
if prefix != self.autocomplete_prefix || self.autocomplete_candidates.is_empty() {
|
|
|
|
|
self.autocomplete_candidates = COMMANDS
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|c| c.starts_with(&prefix))
|
|
|
|
|
.map(|c| c.to_string())
|
|
|
|
|
.collect();
|
|
|
|
|
self.autocomplete_prefix = prefix;
|
|
|
|
|
self.autocomplete_idx = 0;
|
|
|
|
|
} else {
|
|
|
|
|
self.autocomplete_idx = (self.autocomplete_idx + 1) % self.autocomplete_candidates.len();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(candidate) = self.autocomplete_candidates.get(self.autocomplete_idx) {
|
|
|
|
|
self.buffer = candidate.clone();
|
|
|
|
|
self.cursor = self.buffer.len();
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn char_left(&mut self) {
|
|
|
|
|
if self.cursor > 0 {
|
|
|
|
|
self.cursor -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn char_right(&mut self) {
|
|
|
|
|
if self.cursor < self.buffer.len() {
|
|
|
|
|
self.cursor += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn insert(&mut self, c: char) {
|
|
|
|
|
self.buffer.insert(self.cursor, c);
|
|
|
|
|
self.cursor += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete_left(&mut self) {
|
|
|
|
|
if self.cursor > 0 {
|
|
|
|
|
self.cursor -= 1;
|
|
|
|
|
self.buffer.remove(self.cursor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete_right(&mut self) {
|
|
|
|
|
if self.cursor < self.buffer.len() {
|
|
|
|
|
self.buffer.remove(self.cursor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn submit(&mut self) -> String {
|
|
|
|
|
let result = self.buffer.clone();
|
|
|
|
|
if !result.is_empty() {
|
|
|
|
|
self.history.push(result.clone());
|
|
|
|
|
self.history_idx = None;
|
|
|
|
|
}
|
|
|
|
|
self.buffer.clear();
|
|
|
|
|
self.cursor = 0;
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn history_up(&mut self) {
|
|
|
|
|
if self.history.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let idx = match self.history_idx {
|
|
|
|
|
Some(i) if i > 0 => i - 1,
|
|
|
|
|
None => self.history.len() - 1,
|
|
|
|
|
Some(_) => return,
|
|
|
|
|
};
|
|
|
|
|
self.history_idx = Some(idx);
|
|
|
|
|
self.buffer = self.history[idx].clone();
|
|
|
|
|
self.cursor = self.buffer.len();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn history_down(&mut self) {
|
|
|
|
|
match self.history_idx {
|
|
|
|
|
Some(i) if i < self.history.len() - 1 => {
|
|
|
|
|
let idx = i + 1;
|
|
|
|
|
self.history_idx = Some(idx);
|
|
|
|
|
self.buffer = self.history[idx].clone();
|
|
|
|
|
self.cursor = self.buffer.len();
|
|
|
|
|
}
|
|
|
|
|
Some(_) => {
|
|
|
|
|
self.history_idx = None;
|
|
|
|
|
self.buffer.clear();
|
|
|
|
|
self.cursor = 0;
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct MiscState {
|
|
|
|
|
pub overlay: Overlay,
|
|
|
|
|
pub toasts: Vec<super::types::Toast>,
|
|
|
|
|
pub yolo_armed: bool,
|
|
|
|
|
pub security_armed: bool,
|
|
|
|
|
pub esc_press_count: u32,
|
2026-07-11 21:06:22 +07:00
|
|
|
pub last_staleness_sweep_ms: i64,
|
2026-07-11 23:45:13 +07:00
|
|
|
pub thinking: bool,
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MiscState {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
MiscState {
|
|
|
|
|
overlay: Overlay::None,
|
|
|
|
|
toasts: Vec::new(),
|
|
|
|
|
yolo_armed: false,
|
|
|
|
|
security_armed: false,
|
|
|
|
|
esc_press_count: 0,
|
2026-07-11 21:06:22 +07:00
|
|
|
last_staleness_sweep_ms: 0,
|
2026-07-11 23:45:13 +07:00
|
|
|
thinking: false,
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn push_toast(&mut self, toast: super::types::Toast) {
|
|
|
|
|
self.toasts.push(toast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn drain_expired_toasts(&mut self, now_ms: i64) -> Vec<super::types::Toast> {
|
|
|
|
|
let expired: Vec<_> = self.toasts.iter().filter(|t| t.expired(now_ms)).cloned().collect();
|
|
|
|
|
self.toasts.retain(|t| !t.expired(now_ms));
|
|
|
|
|
expired
|
|
|
|
|
}
|
|
|
|
|
}
|