curd-editor 0.27.1

A terminal code editor: file tree, tabs, git, language servers, run and jump to errors, and a panel for Claude or any other assistant
//! The Claude panel: ask Claude about the project, or to change it.
//!
//! `curd` does not talk to Anthropic itself and never sees a login. It
//! runs the `claude` command the user has installed and signed in to
//! (their Claude subscription), once per question, the way the docs
//! describe for other programs: `claude -p ... --output-format stream-json`.
//! The command prints one JSON object per line as it works, which is read
//! here and shown in the panel.

use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use std::process::{Child, Command, Stdio};
use std::time::Instant;

use crossterm::style::Color;

use serde_json::{json, Value};

use crate::highlight::{self, LineState};
use crate::screen::{fit, widen, Decoration, Row, Span, Style, WIDE_PAD};
use crate::snippets::Language;

mod spec;
mod account;
mod stream;
mod sessions;
mod snapshot;
mod panel;
mod markdown;
#[cfg(test)]
mod tests;

pub use spec::*;
pub use account::*;
pub use stream::*;
pub use sessions::*;
pub use snapshot::*;
pub use panel::*;
pub use markdown::*;

/// The tools Claude is refused while the panel is read-only.
const WRITING_TOOLS: &str = "Edit,Write,NotebookEdit,Bash";

/// The most characters shown of something Claude did.
const STEP_CHARS: usize = 70;

/// One question for Claude.
pub struct Request<'a> {
    pub prompt: &'a str,
    /// The conversation to carry on, if one was started.
    pub session: Option<&'a str>,
    /// Whether Claude may change files.
    pub can_edit: bool,
    /// Whether it may also run commands (builds, tests) in the project.
    pub can_run: bool,
    /// What the editor is showing, for Claude to know.
    pub context: &'a str,
    /// The model to use, if not the command's own choice: a short name
    /// such as `opus`, or a model's full name.
    pub model: Option<&'a str>,
}

/// Starts `words` (a command and its arguments) in `dir`. Every line it
/// prints is passed to `forward`, and `None` when it has finished.
pub fn start(
    words: &[String],
    dir: &Path,
    forward: impl Fn(Option<String>) + Send + Clone + 'static,
) -> std::io::Result<Child> {
    let mut child = Command::new(&words[0])
        .args(&words[1..])
        .current_dir(dir)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    // What it says when it fails is passed on in the same form as the rest.
    if let Some(errors) = child.stderr.take() {
        let forward = forward.clone();
        std::thread::spawn(move || {
            for line in BufReader::new(errors).lines().map_while(Result::ok) {
                forward(Some(json!({"type": "stderr", "text": line}).to_string()));
            }
        });
    }
    if let Some(output) = child.stdout.take() {
        std::thread::spawn(move || {
            for line in BufReader::new(output).lines().map_while(Result::ok) {
                forward(Some(line));
            }
            forward(None);
        });
    }
    Ok(child)
}