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 {
Switch(switch::SwitchArgs),
#[command(name = "shell-init")]
ShellInit {
#[arg(default_value = "zsh")]
shell_name: String,
},
}
pub fn needs_tui(cli: &WtCli) -> bool {
cli.command.is_none()
}
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
}
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)
}