use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, Instant};
use crossterm::event::{
Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};
use serde_json::Value;
use crate::agent::{self, Account, AgentSpec, Change, ChangeKind, Format, Panel, Request, Snapshot, Who};
use crate::document::Document;
use crate::editor::Editor;
use crate::finder::{self, Finder};
use crate::git::{self, GitInfo};
use crate::lsp::{find_root, Client, Reply, ServerSpec};
use crate::runner::{self, Output};
use crate::screen::{fit, overlay, place, widen, Row, Screen, Span, Style, Zone};
use crate::theme::Theme;
use crate::search::{self, Hit};
use crate::terminal::Terminal;
use crate::tree::Tree;
mod files;
mod servers;
mod git_menu;
mod run_panel;
mod commands;
mod assistants;
mod events;
mod draw;
#[cfg(test)]
mod tests;
use git_menu::*;
use commands::*;
use assistants::*;
const LSP_DEBOUNCE: Duration = Duration::from_millis(30);
const TREE_WIDTH: usize = 30;
const WHEEL_LINES: isize = 3;
const DOUBLE_CLICK: Duration = Duration::from_millis(400);
const MORE_TABS: &str = " < ";
const CLOSE_ICON: char = '×';
const AGENT_TICK: Duration = Duration::from_millis(120);
const AGENT_SIDE_MIN: usize = 100;
const TEXT_MIN_WIDTH: usize = 40;
const AGENT_MIN_WIDTH: usize = 30;
const MAX_SELECTION_CHARS: usize = 8000;
const FINDER_ROWS: usize = 8;
const POPUP_WIDTH: usize = 80;
const POPUP_HINT: &str = "↑↓ choose Enter pick Esc close ";
#[derive(Clone, Copy, Debug, PartialEq)]
enum Picker {
Files,
Search,
Results,
Git,
Branches,
Commit,
NewBranch,
Sessions,
Run,
NewFile,
NewFolder,
Rename,
Commands,
Slash,
Attach,
Model,
ModelName,
Agent,
}
impl Picker {
fn prompt(self) -> &'static str {
match self {
Self::Files => " Open file: ",
Self::Search => " Search project: ",
Self::Results => " Matches: ",
Self::Git => " Git: ",
Self::Branches => " Switch to branch: ",
Self::Commit => " Commit message: ",
Self::NewBranch => " New branch: ",
Self::Sessions => " Earlier conversations: ",
Self::Run => " Run: ",
Self::NewFile => " New file: ",
Self::NewFolder => " New folder: ",
Self::Rename => " Rename to: ",
Self::Commands => " Command: ",
Self::Slash => " /",
Self::Attach => " Attach file: ",
Self::Model => " Model: ",
Self::ModelName => " Model name: ",
Self::Agent => " Assistant: ",
}
}
fn is_for_claude(self) -> bool {
matches!(
self,
Self::Slash | Self::Attach | Self::Model | Self::ModelName | Self::Sessions | Self::Agent
)
}
fn is_question(self) -> bool {
matches!(
self,
Self::Search
| Self::Commit
| Self::NewBranch
| Self::Run
| Self::NewFile
| Self::NewFolder
| Self::Rename
| Self::ModelName
)
}
}
pub const INPUT_WAIT: Duration = Duration::from_millis(250);
pub enum AppEvent {
Input(Event),
Lsp(usize, Option<Value>),
Git(Option<GitInfo>),
Committed(String, Option<Vec<String>>),
Head(Option<String>),
GitDone(String),
Agent(u64, Option<String>),
Run(u64, runner::Event),
Account(usize, String, bool),
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Focus {
Editor,
Tree,
Agent,
Output,
}
struct Server {
id: usize,
key: String,
client: Rc<RefCell<Client>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Popup {
x: usize,
width: usize,
input_y: usize,
list_y: usize,
capacity: usize,
}
struct Layout {
tabs: usize,
tree: usize,
output: usize,
agent: usize,
agent_side: usize,
pane_width: usize,
pane_height: usize,
}
pub struct Workspace {
editors: Vec<Editor>,
active: usize,
tree: Tree,
show_tree: bool,
focus: Focus,
git: Option<GitInfo>,
git_head: Option<Option<String>>,
finder: Option<Finder>,
picker: Picker,
hits: Vec<Hit>,
clipboard: String,
output: Output,
show_output: bool,
run_child: Option<std::process::Child>,
run_number: u64,
file_target: PathBuf,
delete_pending: Option<PathBuf>,
help: Option<usize>,
agent: Panel,
show_agent: bool,
agent_rows: Option<usize>,
agent_cols: Option<usize>,
agent_sessions: Vec<agent::SessionInfo>,
agent_home: Option<PathBuf>,
resizing_agent: bool,
agent_child: Option<std::process::Child>,
agent_snapshot: Option<Snapshot>,
agent_changes: Vec<Change>,
agent_run: u64,
pub agent_setting: Option<String>,
agents: Vec<AgentSpec>,
pub agents_file: Option<PathBuf>,
parked: Vec<Parked>,
next_run: u64,
agent_tabs: Vec<(usize, usize, usize)>,
agent_index: usize,
terminal_job: Option<(Vec<String>, String)>,
pub input_paused: std::sync::Arc<std::sync::atomic::AtomicBool>,
last_click: Option<(Instant, usize, usize)>,
clipboard_out: Option<String>,
servers: Vec<Server>,
next_server: usize,
events: Option<Sender<AppEvent>>,
lsp_setting: Option<String>,
message: String,
quit_pending: bool,
close_pending: bool,
should_quit: bool,
size: (usize, usize),
screen: Screen,
}
impl Workspace {
pub fn new(
root: PathBuf,
events: Option<Sender<AppEvent>>,
lsp_setting: Option<String>,
) -> Self {
Self {
editors: Vec::new(),
active: 0,
tree: Tree::new(root),
show_tree: false,
focus: Focus::Editor,
git: None,
git_head: None,
finder: None,
picker: Picker::Files,
hits: Vec::new(),
clipboard: String::new(),
output: Output::default(),
show_output: false,
run_child: None,
run_number: 0,
file_target: PathBuf::new(),
delete_pending: None,
help: None,
agent: panel_for(&AgentSpec::claude()),
agent_snapshot: None,
agent_changes: Vec::new(),
show_agent: false,
agent_rows: None,
agent_cols: None,
agent_sessions: Vec::new(),
agent_home: None,
resizing_agent: false,
agent_child: None,
agent_run: 0,
agent_setting: None,
agents: vec![AgentSpec::claude(), AgentSpec::cursor(), AgentSpec::opencode()],
agents_file: None,
parked: [AgentSpec::claude(), AgentSpec::cursor(), AgentSpec::opencode()]
.iter()
.enumerate()
.map(|(index, spec)| match index {
0 => Parked::default(),
_ => Parked { panel: panel_for(spec), ..Parked::default() },
})
.collect(),
next_run: 0,
agent_tabs: Vec::new(),
agent_index: 0,
terminal_job: None,
input_paused: Default::default(),
last_click: None,
clipboard_out: None,
servers: Vec::new(),
next_server: 0,
events,
lsp_setting,
message: String::new(),
quit_pending: false,
close_pending: false,
should_quit: false,
size: (0, 0),
screen: Screen::default(),
}
}
pub fn open_empty(&mut self) {
self.editors.push(Editor::new(Document::default()));
self.active = self.editors.len() - 1;
self.focus = Focus::Editor;
}
pub fn show_folder(&mut self) {
self.show_tree = true;
self.focus = Focus::Tree;
}
fn show_branch(&mut self) {
let branch = self.git.as_ref().map(|git| git.branch.clone());
for editor in &mut self.editors {
editor.branch = branch.clone();
}
}
pub fn set_theme(&mut self, theme: Theme) {
self.screen = Screen::with_theme(theme);
}
pub fn has_files(&self) -> bool {
!self.editors.is_empty()
}
}