curd-editor 0.10.0

A small terminal code editor: file tree, git status and change markers, tabs, undo, selection, search and replace, a git menu, and language-server colours, errors and completion
mod diff;
mod document;
mod editor;
mod finder;
mod folds;
mod git;
mod highlight;
mod history;
mod lsp;
mod screen;
mod search;
mod snippets;
mod terminal;
mod tree;
mod workspace;

use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;

use terminal::Terminal;
use workspace::{AppEvent, Workspace};

/// `curd`            an empty file
/// `curd file ...`   those files, in tabs
/// `curd folder`     the folder as a project, with its file tree
fn main() -> std::io::Result<()> {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let here = std::env::current_dir()?;

    let folder = args.first().map(PathBuf::from).filter(|path| path.is_dir());
    let files = &args[folder.is_some() as usize..];
    // The project is the folder given, or the one the first file belongs
    // to: its git repository, or else the folder it sits in.
    let root = match (&folder, files.first()) {
        (Some(folder), _) => folder.clone(),
        (None, Some(file)) => {
            let file = std::path::absolute(file)?;
            lsp::find_root(file.parent().unwrap_or(&here), &[])
        }
        (None, None) => here,
    };
    let root = std::fs::canonicalize(&root).unwrap_or(root);

    let _terminal = Terminal::start()?;

    // Keys, language-server messages and git results arrive on one queue.
    let (sender, events) = channel();
    let lsp_setting = std::env::var("CURD_LSP").ok();
    let mut workspace = Workspace::new(root, Some(sender.clone()), lsp_setting);
    for file in files {
        workspace.open(Path::new(file));
    }
    if folder.is_some() {
        workspace.show_folder();
    } else if args.is_empty() {
        workspace.open_empty();
    } else if !workspace.has_files() {
        // Nothing could be opened: show the folder, and why.
        workspace.show_folder();
    }

    std::thread::spawn(move || {
        while let Ok(event) = crossterm::event::read() {
            if sender.send(AppEvent::Input(event)).is_err() {
                break;
            }
        }
    });

    workspace.run(&events)
}