sniplab 0.3.2

A filesystem-native snippet library and agent-friendly CLI, installed as `snip`
Documentation
use clap::CommandFactory;
use serde_json::json;
use snip::Library;
use snip::config::{
    AppConfig, ColorSetting, GitConfig, OutputSetting, PreviewRenderSetting, TuiConfig,
    TuiDensitySetting, TuiThemeSetting, config_path,
};
use snip::error::{Result, SnipError};
use snip::sort::SortMode;
use std::io;
use std::path::{Path, PathBuf};

use super::output::{print_record, resolve_color, resolve_output};
use crate::cli::{
    Cli, ColorMode, CompletionArgs, CompletionShell, ConfigArgs, ConfigCommand, ConfigKey,
    InitArgs, OutputMode,
};

pub fn command_init(
    args: &InitArgs,
    output: OutputMode,
    explicit_color: Option<ColorMode>,
) -> Result<()> {
    if super::onboard::is_init_interactive(args, output) {
        let config = AppConfig::load()?;
        if super::onboard::run_init(&config, args, resolve_color(explicit_color, &config))?
            .is_some()
        {
            super::onboard::print_init_next_steps();
            return Ok(());
        }
        return Err(SnipError::usage(
            "setup cancelled; pass a path or --yes to initialize non-interactively",
        ));
    }
    let path = args.path.clone().unwrap_or_else(|| ".".into());
    let library = Library::init(&path, args.name.as_deref())?;
    if args.git {
        snip::git::init(library.root())?;
    }
    let value = json!({
        "path": library.root(),
        "id": library.manifest().id,
        "name": library.manifest().name,
        "schema_version": library.manifest().schema_version,
        "git_initialized": args.git,
    });
    if output == OutputMode::Human {
        println!("initialized: {}", library.root().display());
        println!("library id: {}", library.manifest().id);
    } else {
        print_record(&value, output)?;
    }
    Ok(())
}

pub fn command_config(args: &ConfigArgs, explicit_output: Option<OutputMode>) -> Result<()> {
    let path = config_path()?;
    match &args.command {
        ConfigCommand::Path => {
            println!("{}", path.display());
            Ok(())
        }
        ConfigCommand::Init { library, force } => {
            if path.exists() && !force {
                return Err(SnipError::conflict(format!(
                    "config already exists: {}; pass --force to replace it",
                    path.display()
                )));
            }
            let mut config = AppConfig {
                output: Some(OutputSetting::Human),
                color: Some(ColorSetting::Auto),
                preview_render: Some(PreviewRenderSetting::Ansi),
                preview_pager: Some(false),
                default_language: Some("text".to_owned()),
                tui: Some(TuiConfig::default()),
                ..AppConfig::default()
            };
            if let Some(library) = library {
                config.default_library = Some(validated_library_path(library)?);
            }
            config.save_to(&path)?;
            let output = resolve_output(explicit_output, &config);
            print_config(&config, &path, output)
        }
        ConfigCommand::Show => {
            let config = AppConfig::load_from(&path)?;
            let output = resolve_output(explicit_output, &config);
            print_config(&config, &path, output)
        }
        ConfigCommand::Set { key, value } => {
            let mut config = AppConfig::load_from(&path)?;
            set_config_value(&mut config, *key, value)?;
            config.save_to(&path)?;
            if matches!(*key, ConfigKey::GitAutoPush)
                && config
                    .git
                    .as_ref()
                    .is_some_and(|git| git.auto_push && git.auto_commit_interval == 0)
            {
                eprintln!(
                    "warning: automatic push is enabled but git-auto-commit-interval is 0; automatic Git operations remain off"
                );
            }
            let output = resolve_output(explicit_output, &config);
            print_config(&config, &path, output)
        }
        ConfigCommand::Unset { key } => {
            let mut config = AppConfig::load_from(&path)?;
            unset_config_value(&mut config, *key);
            config.save_to(&path)?;
            let output = resolve_output(explicit_output, &config);
            print_config(&config, &path, output)
        }
    }
}

pub fn command_completion(args: &CompletionArgs) -> Result<()> {
    let mut command = Cli::command();
    let shell = match args.shell {
        CompletionShell::Bash => clap_complete::Shell::Bash,
        CompletionShell::Elvish => clap_complete::Shell::Elvish,
        CompletionShell::Fish => clap_complete::Shell::Fish,
        CompletionShell::Powershell => clap_complete::Shell::PowerShell,
        CompletionShell::Zsh => clap_complete::Shell::Zsh,
    };
    clap_complete::generate(shell, &mut command, "snip", &mut io::stdout());
    Ok(())
}

fn print_config(config: &AppConfig, path: &Path, output: OutputMode) -> Result<()> {
    if output == OutputMode::Human {
        println!("config: {}", path.display());
        print!("{}", toml::to_string_pretty(config)?);
        Ok(())
    } else {
        print_record(&json!({"path": path, "config": config}), output)
    }
}

fn set_config_value(config: &mut AppConfig, key: ConfigKey, value: &str) -> Result<()> {
    match key {
        ConfigKey::DefaultLibrary => {
            config.default_library = Some(validated_library_path(&expand_user_path(value)?)?);
        }
        ConfigKey::Output => {
            config.output = Some(match value.to_ascii_lowercase().as_str() {
                "human" => OutputSetting::Human,
                "json" => OutputSetting::Json,
                "jsonl" => OutputSetting::Jsonl,
                _ => return Err(SnipError::usage("output must be human, json, or jsonl")),
            });
        }
        ConfigKey::Color => {
            config.color = Some(match value.to_ascii_lowercase().as_str() {
                "auto" => ColorSetting::Auto,
                "always" => ColorSetting::Always,
                "never" => ColorSetting::Never,
                _ => return Err(SnipError::usage("color must be auto, always, or never")),
            });
        }
        ConfigKey::PreviewRender => {
            config.preview_render = Some(match value.to_ascii_lowercase().as_str() {
                "ansi" => PreviewRenderSetting::Ansi,
                "plain" => PreviewRenderSetting::Plain,
                "html" => PreviewRenderSetting::Html,
                _ => {
                    return Err(SnipError::usage(
                        "preview-render must be ansi, plain, or html",
                    ));
                }
            });
        }
        ConfigKey::PreviewPager => config.preview_pager = Some(parse_bool(value)?),
        ConfigKey::Editor => config.editor = Some(nonempty_value("editor", value)?),
        ConfigKey::Pager => config.pager = Some(nonempty_value("pager", value)?),
        ConfigKey::DefaultLanguage => {
            config.default_language = Some(nonempty_value("default-language", value)?)
        }
        ConfigKey::DefaultFolder => config.default_folder = Some(value.trim().to_owned()),
        ConfigKey::DefaultTags => {
            let tags = value.split(',').map(str::to_owned).collect::<Vec<_>>();
            config.default_tags = snip::filesystem::normalize_tags(&tags)?;
        }
        ConfigKey::TuiTheme => {
            config.tui.get_or_insert_with(TuiConfig::default).theme =
                match value.to_ascii_lowercase().as_str() {
                    "auto" => TuiThemeSetting::Auto,
                    "light" => TuiThemeSetting::Light,
                    "dark" => TuiThemeSetting::Dark,
                    _ => return Err(SnipError::usage("tui-theme must be auto, light, or dark")),
                };
        }
        ConfigKey::TuiLightTheme => {
            config
                .tui
                .get_or_insert_with(TuiConfig::default)
                .light_theme = Some(nonempty_value("tui-light-theme", value)?);
        }
        ConfigKey::TuiDarkTheme => {
            config.tui.get_or_insert_with(TuiConfig::default).dark_theme =
                Some(nonempty_value("tui-dark-theme", value)?);
        }
        ConfigKey::TuiSort => {
            config.tui.get_or_insert_with(TuiConfig::default).sort =
                match value.to_ascii_lowercase().as_str() {
                    "modified" => SortMode::Modified,
                    "title" => SortMode::Title,
                    "created" => SortMode::Created,
                    _ => {
                        return Err(SnipError::usage(
                            "tui-sort must be modified, created, or title",
                        ));
                    }
                };
        }
        ConfigKey::TuiDensity => {
            config.tui.get_or_insert_with(TuiConfig::default).density =
                match value.to_ascii_lowercase().as_str() {
                    "comfortable" => TuiDensitySetting::Comfortable,
                    "compact" => TuiDensitySetting::Compact,
                    _ => {
                        return Err(SnipError::usage(
                            "tui-density must be comfortable or compact",
                        ));
                    }
                };
        }
        ConfigKey::TuiLineNumbers => {
            config
                .tui
                .get_or_insert_with(TuiConfig::default)
                .line_numbers = parse_bool(value)?;
        }
        ConfigKey::GitAutoCommitInterval => {
            config
                .git
                .get_or_insert_with(GitConfig::default)
                .auto_commit_interval = value.parse::<u32>().map_err(|_| {
                SnipError::usage("git-auto-commit-interval must be a whole number of minutes")
            })?;
        }
        ConfigKey::GitAutoPush => {
            config.git.get_or_insert_with(GitConfig::default).auto_push = parse_bool(value)?;
        }
        ConfigKey::GitBackupOnQuit => {
            config
                .git
                .get_or_insert_with(GitConfig::default)
                .backup_on_quit = parse_bool(value)?;
        }
    }
    Ok(())
}

fn unset_config_value(config: &mut AppConfig, key: ConfigKey) {
    match key {
        ConfigKey::DefaultLibrary => config.default_library = None,
        ConfigKey::Output => config.output = None,
        ConfigKey::Color => config.color = None,
        ConfigKey::PreviewRender => config.preview_render = None,
        ConfigKey::PreviewPager => config.preview_pager = None,
        ConfigKey::Editor => config.editor = None,
        ConfigKey::Pager => config.pager = None,
        ConfigKey::DefaultLanguage => config.default_language = None,
        ConfigKey::DefaultFolder => config.default_folder = None,
        ConfigKey::DefaultTags => config.default_tags.clear(),
        ConfigKey::TuiTheme => {
            config.tui.get_or_insert_with(TuiConfig::default).theme = TuiThemeSetting::Auto
        }
        ConfigKey::TuiLightTheme => {
            config
                .tui
                .get_or_insert_with(TuiConfig::default)
                .light_theme = None
        }
        ConfigKey::TuiDarkTheme => {
            config.tui.get_or_insert_with(TuiConfig::default).dark_theme = None
        }
        ConfigKey::TuiSort => {
            config.tui.get_or_insert_with(TuiConfig::default).sort = SortMode::Modified
        }
        ConfigKey::TuiDensity => {
            config.tui.get_or_insert_with(TuiConfig::default).density =
                TuiDensitySetting::Comfortable
        }
        ConfigKey::TuiLineNumbers => {
            config
                .tui
                .get_or_insert_with(TuiConfig::default)
                .line_numbers = TuiConfig::default().line_numbers
        }
        ConfigKey::GitAutoCommitInterval => {
            config
                .git
                .get_or_insert_with(GitConfig::default)
                .auto_commit_interval = 0
        }
        ConfigKey::GitAutoPush => {
            config.git.get_or_insert_with(GitConfig::default).auto_push = false
        }
        ConfigKey::GitBackupOnQuit => {
            config
                .git
                .get_or_insert_with(GitConfig::default)
                .backup_on_quit = false
        }
    }
}

fn validated_library_path(path: &Path) -> Result<PathBuf> {
    Ok(Library::open(path)?.root().to_path_buf())
}

fn expand_user_path(value: &str) -> Result<PathBuf> {
    if value == "~" || value.starts_with("~/") || value.starts_with("~\\") {
        let home = std::env::var_os("HOME")
            .or_else(|| std::env::var_os("USERPROFILE"))
            .ok_or_else(|| SnipError::io("cannot expand ~: HOME or USERPROFILE is not set"))?;
        let mut path = PathBuf::from(home);
        if value.len() > 2 {
            path.push(&value[2..]);
        }
        Ok(path)
    } else {
        Ok(PathBuf::from(value))
    }
}

fn parse_bool(value: &str) -> Result<bool> {
    match value.to_ascii_lowercase().as_str() {
        "true" | "yes" | "1" | "on" => Ok(true),
        "false" | "no" | "0" | "off" => Ok(false),
        _ => Err(SnipError::usage(
            "boolean value must be true/false, yes/no, on/off, or 1/0",
        )),
    }
}

fn nonempty_value(name: &str, value: &str) -> Result<String> {
    let value = value.trim();
    if value.is_empty() {
        Err(SnipError::usage(format!("{name} cannot be empty")))
    } else {
        Ok(value.to_owned())
    }
}