cstats-cli 0.1.1

Command line interface for cstats
//! Hook command implementation

use cstats_core::Result;
use tracing::info;

use crate::{HookArgs, ShellType};

/// Generate shell hooks
pub async fn hook_command(args: HookArgs) -> Result<()> {
    info!(
        "Generating {} shell hook",
        format!("{:?}", args.shell).to_lowercase()
    );

    let hook_content = match args.shell {
        ShellType::Bash => generate_bash_hook(),
        ShellType::Zsh => generate_zsh_hook(),
        ShellType::Fish => generate_fish_hook(),
        ShellType::Powershell => generate_powershell_hook(),
    };

    if let Some(output_path) = args.output {
        tokio::fs::write(&output_path, hook_content).await?;
        println!("Hook written to: {}", output_path.display());
    } else {
        println!("{}", hook_content);
    }

    Ok(())
}

/// Generate Bash shell hook
fn generate_bash_hook() -> String {
    r#"# cstats Bash hook
# Add this to your ~/.bashrc or ~/.bash_profile

# Function to wrap commands with cstats collection
cstats_wrap() {
    local cmd="$*"
    cstats collect --source "bash" --command "$cmd"
}

# Alias for easy usage
alias cs='cstats_wrap'

# Auto-collect for specific commands (optional)
# Uncomment and modify as needed:
# alias git='cstats_wrap git'
# alias make='cstats_wrap make'
# alias cargo='cstats_wrap cargo'

# Function to show recent statistics
cstats_recent() {
    cstats analyze --source "bash" --start-time "$(date -d '1 hour ago' -Iseconds)"
}

# Function to clear cstats cache
cstats_clear() {
    cstats cache clear
}

echo "cstats Bash hook loaded. Use 'cs <command>' to collect statistics."
"#
    .to_string()
}

/// Generate Zsh shell hook
fn generate_zsh_hook() -> String {
    r#"# cstats Zsh hook
# Add this to your ~/.zshrc

# Function to wrap commands with cstats collection
cstats_wrap() {
    local cmd="$*"
    cstats collect --source "zsh" --command "$cmd"
}

# Alias for easy usage
alias cs='cstats_wrap'

# Auto-collect for specific commands (optional)
# Uncomment and modify as needed:
# alias git='cstats_wrap git'
# alias make='cstats_wrap make'
# alias cargo='cstats_wrap cargo'

# Function to show recent statistics
cstats_recent() {
    cstats analyze --source "zsh" --start-time "$(date -d '1 hour ago' -Iseconds)"
}

# Function to clear cstats cache
cstats_clear() {
    cstats cache clear
}

# Zsh-specific: preexec hook for automatic collection (advanced)
# Uncomment to enable automatic collection for all commands:
# preexec() {
#     cstats collect --source "zsh" --command "$1" &
# }

echo "cstats Zsh hook loaded. Use 'cs <command>' to collect statistics."
"#
    .to_string()
}

/// Generate Fish shell hook
fn generate_fish_hook() -> String {
    r#"# cstats Fish hook
# Add this to your ~/.config/fish/config.fish

# Function to wrap commands with cstats collection
function cstats_wrap
    set cmd $argv
    cstats collect --source "fish" --command "$cmd"
end

# Alias for easy usage
alias cs='cstats_wrap'

# Auto-collect for specific commands (optional)
# Uncomment and modify as needed:
# alias git='cstats_wrap git'
# alias make='cstats_wrap make'
# alias cargo='cstats_wrap cargo'

# Function to show recent statistics
function cstats_recent
    cstats analyze --source "fish" --start-time (date -d '1 hour ago' -Iseconds)
end

# Function to clear cstats cache
function cstats_clear
    cstats cache clear
end

echo "cstats Fish hook loaded. Use 'cs <command>' to collect statistics."
"#
    .to_string()
}

/// Generate PowerShell hook
fn generate_powershell_hook() -> String {
    r#"# cstats PowerShell hook
# Add this to your PowerShell profile

# Function to wrap commands with cstats collection
function Invoke-CStatsWrap {
    param(
        [Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
        [string[]]$Command
    )
    
    $cmdString = $Command -join ' '
    cstats collect --source "powershell" --command $cmdString
}

# Alias for easy usage
Set-Alias -Name cs -Value Invoke-CStatsWrap

# Function to show recent statistics
function Show-CStatsRecent {
    $startTime = (Get-Date).AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ssZ")
    cstats analyze --source "powershell" --start-time $startTime
}

# Function to clear cstats cache
function Clear-CStatsCache {
    cstats cache clear
}

Write-Host "cstats PowerShell hook loaded. Use 'cs <command>' to collect statistics."
"#
    .to_string()
}