vkit 0.1.4

Fast Rust dev CLI: manage git worktrees, Node ports, run scripts, install & sync VS Code / Cursor extensions.
pub mod config;
pub mod diff_hl;
pub mod git;
pub mod mr;
pub mod name;
pub mod placement;
pub mod shell;
pub mod state;
pub mod switch;
pub mod ui;

use std::env;
use std::io::{self, Write};

use anyhow::Result;
use clap::{Parser, Subcommand};
use ratatui::DefaultTerminal;

#[derive(Debug, Clone, Parser)]
#[command(name = "worktree", about = "管理 git worktree")]
pub struct WtCli {
    #[command(subcommand)]
    pub command: Option<WtCommand>,
}

#[derive(Debug, Clone, Subcommand)]
pub enum WtCommand {
    /// 切换到 worktree(`-` 上一个 · `^` Main · `mr:N`)
    Switch(switch::SwitchArgs),
    /// 打印 shell 集成脚本(eval 后使用 `vwt`)
    #[command(name = "shell-init")]
    ShellInit {
        /// zsh / bash / fish(默认 zsh)
        #[arg(default_value = "zsh")]
        shell_name: String,
    },
}

/// 是否需要 TUI / 主题探测(纯命令行子命令跳过)。
pub fn needs_tui(cli: &WtCli) -> bool {
    cli.command.is_none()
}

/// CLI 入口。
pub fn run_cli(cli: WtCli) -> Result<()> {
    match cli.command {
        None => run_tui(),
        Some(WtCommand::Switch(args)) => switch::run(args),
        Some(WtCommand::ShellInit { shell_name }) => {
            shell::print_init(&shell_name)?;
            let _ = io::stdout().flush();
            Ok(())
        }
    }
}

fn run_tui() -> Result<()> {
    let config = config::load()?;
    let cwd = env::current_dir()?;
    let mut app = ui::load_app(&cwd, config)?;
    let mut terminal = ratatui::init();
    let result = app.run(&mut terminal);
    ratatui::restore();
    result
}

/// Dashboard 复用同一终端会话。
pub fn run_in_terminal(terminal: &mut DefaultTerminal) -> Result<()> {
    let config = config::load()?;
    let cwd = env::current_dir()?;
    let mut app = ui::load_app(&cwd, config)?;
    app.run(terminal)
}