euv-cli 0.50.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, new, and Hyperlane SSE dynamic updates across all supported platforms.
Documentation
use super::*;

/// Generates a pure-Rust shell tab-completion script for `shell` covering all `euv` subcommands, targets, groups, and flags.
///
/// # Arguments
///
/// - `ShellKindArg` - Requested shell dialect (`Bash`, `Zsh`, `Fish`, or `Powershell`).
///
/// # Returns
///
/// - `String` - Ready-to-source completion script text.
#[must_use]
pub fn generate_completions(shell: ShellKindArg) -> String {
    match shell {
        ShellKindArg::Bash => generate_bash_completions(),
        ShellKindArg::Zsh => generate_zsh_completions(),
        ShellKindArg::Fish => generate_fish_completions(),
        ShellKindArg::Powershell => generate_powershell_completions(),
    }
}

/// Generates the Bash completion function and `complete -F` registration for `euv`.
///
/// # Returns
///
/// - `String` - Bash completion script.
fn generate_bash_completions() -> String {
    format!(
        "_euv_completions() {{\n\
  local cur prev cmd\n\
  COMPREPLY=()\n\
  cur=\"${{COMP_WORDS[COMP_CWORD]}}\"\n\
  prev=\"${{COMP_WORDS[COMP_CWORD-1]}}\"\n\
  cmd=\"${{COMP_WORDS[1]}}\"\n\
  if [[ ${{COMP_CWORD}} -eq 1 ]]; then\n\
    COMPREPLY=( $(compgen -W \"{COMPLETION_SUBCOMMANDS}\" -- \"${{cur}}\") )\n\
    return 0\n\
  fi\n\
  case \"${{prev}}\" in\n\
    --target) COMPREPLY=( $(compgen -W \"{COMPLETION_TARGETS}\" -- \"${{cur}}\") ); return 0 ;;\n\
    --render) COMPREPLY=( $(compgen -W \"{COMPLETION_RENDER_MODES}\" -- \"${{cur}}\") ); return 0 ;;\n\
    --template) COMPREPLY=( $(compgen -W \"{COMPLETION_TEMPLATES}\" -- \"${{cur}}\") ); return 0 ;;\n\
  esac\n\
  case \"${{cmd}}\" in\n\
    update) COMPREPLY=( $(compgen -W \"{COMPLETION_UPDATE_ACTIONS}\" -- \"${{cur}}\") ) ;;\n\
    docs) COMPREPLY=( $(compgen -W \"{COMPLETION_DOCS_ACTIONS}\" -- \"${{cur}}\") ) ;;\n\
    completions) COMPREPLY=( $(compgen -W \"{COMPLETION_SHELLS}\" -- \"${{cur}}\") ) ;;\n\
    build) COMPREPLY=( $(compgen -W \"{COMPLETION_BUILD_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    dev) COMPREPLY=( $(compgen -W \"{COMPLETION_DEV_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    check) COMPREPLY=( $(compgen -W \"{COMPLETION_CHECK_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    doctor) COMPREPLY=( $(compgen -W \"{COMPLETION_DOCTOR_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    new) COMPREPLY=( $(compgen -W \"{COMPLETION_NEW_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    clean) COMPREPLY=( $(compgen -W \"{COMPLETION_CLEAN_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
    inspect) COMPREPLY=( $(compgen -W \"{COMPLETION_INSPECT_FLAGS}\" -- \"${{cur}}\") ) ;;\n\
  esac\n\
}}\n\
complete -F _euv_completions euv\n"
    )
}

/// Generates the Zsh `#compdef euv` completion function.
///
/// # Returns
///
/// - `String` - Zsh completion script.
fn generate_zsh_completions() -> String {
    format!(
        "#compdef euv\n\
_euv() {{\n\
  local -a subcommands update_actions docs_actions targets templates shells\n\
  subcommands=({COMPLETION_SUBCOMMANDS})\n\
  update_actions=({COMPLETION_UPDATE_ACTIONS})\n\
  docs_actions=({COMPLETION_DOCS_ACTIONS})\n\
  targets=({COMPLETION_TARGETS})\n\
  templates=({COMPLETION_TEMPLATES})\n\
  shells=({COMPLETION_SHELLS})\n\
  if (( CURRENT == 2 )); then\n\
    _describe 'command' subcommands\n\
    return\n\
  fi\n\
  case \"$words[CURRENT-1]\" in\n\
    --target) _describe 'target' targets; return ;;\n\
    --template) _describe 'template' templates; return ;;\n\
  esac\n\
  case \"$words[2]\" in\n\
    update) _describe 'update action' update_actions ;;\n\
    docs) _describe 'docs action' docs_actions ;;\n\
    completions) _describe 'shell' shells ;;\n\
    build) _values 'flags' {COMPLETION_BUILD_FLAGS} ;;\n\
    dev) _values 'flags' {COMPLETION_DEV_FLAGS} ;;\n\
    check) _values 'flags' {COMPLETION_CHECK_FLAGS} ;;\n\
    doctor) _values 'flags' {COMPLETION_DOCTOR_FLAGS} ;;\n\
    new) _values 'flags' {COMPLETION_NEW_FLAGS} ;;\n\
    clean) _values 'flags' {COMPLETION_CLEAN_FLAGS} ;;\n\
    inspect) _values 'flags' {COMPLETION_INSPECT_FLAGS} ;;\n\
  esac\n\
}}\n\
compdef _euv euv\n"
    )
}

/// Generates Fish shell completion definitions for `euv`.
///
/// # Returns
///
/// - `String` - Fish completion script.
fn generate_fish_completions() -> String {
    let mut out: String = String::new();
    for cmd in COMPLETION_SUBCOMMANDS.split_whitespace() {
        out.push_str(&format!(
            "complete -c euv -n '__fish_use_subcommand' -f -a '{cmd}'\n"
        ));
    }
    for action in COMPLETION_UPDATE_ACTIONS.split_whitespace() {
        out.push_str(&format!(
            "complete -c euv -n '__fish_seen_subcommand_from update' -f -a '{action}'\n"
        ));
    }
    for action in COMPLETION_DOCS_ACTIONS.split_whitespace() {
        out.push_str(&format!(
            "complete -c euv -n '__fish_seen_subcommand_from docs' -f -a '{action}'\n"
        ));
    }
    for shell in COMPLETION_SHELLS.split_whitespace() {
        out.push_str(&format!(
            "complete -c euv -n '__fish_seen_subcommand_from completions' -f -a '{shell}'\n"
        ));
    }
    out.push_str(&format!(
        "complete -c euv -n '__fish_seen_subcommand_from build check run targets' -l target -f -a '{COMPLETION_TARGETS}'\n"
    ));
    out.push_str(&format!(
        "complete -c euv -n '__fish_seen_subcommand_from new' -l template -f -a '{COMPLETION_TEMPLATES}'\n"
    ));
    out
}

/// Generates a PowerShell `Register-ArgumentCompleter` script for `euv`.
///
/// # Returns
///
/// - `String` - PowerShell completion script.
fn generate_powershell_completions() -> String {
    let subcommands_ps: String = format_ps_string_list(COMPLETION_SUBCOMMANDS);
    let update_ps: String = format_ps_string_list(COMPLETION_UPDATE_ACTIONS);
    let targets_ps: String = format_ps_string_list(COMPLETION_TARGETS);
    let templates_ps: String = format_ps_string_list(COMPLETION_TEMPLATES);
    let shells_ps: String = format_ps_string_list(COMPLETION_SHELLS);
    format!(
        "Register-ArgumentCompleter -Native -CommandName euv -ScriptBlock {{\n\
    param($wordToComplete, $commandAst, $cursorPosition)\n\
    $tokens = $commandAst.CommandElements | ForEach-Object {{ $_.ToString() }}\n\
    $subcommands = @({subcommands_ps})\n\
    $updateActions = @({update_ps})\n\
    $targets = @({targets_ps})\n\
    $templates = @({templates_ps})\n\
    $shells = @({shells_ps})\n\
    $candidates = $subcommands\n\
    if ($tokens.Count -ge 2) {{\n\
        $prev = $tokens[$tokens.Count - 1]\n\
        if ($prev -eq '--target') {{ $candidates = $targets }}\n\
        elseif ($prev -eq '--template') {{ $candidates = $templates }}\n\
        elseif ($tokens[1] -eq 'update') {{ $candidates = $updateActions }}\n\
        elseif ($tokens[1] -eq 'completions') {{ $candidates = $shells }}\n\
    }}\n\
    $candidates | Where-Object {{ $_ -like \"$wordToComplete*\" }} | ForEach-Object {{\n\
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)\n\
    }}\n\
}}\n"
    )
}

/// Formats space-delimited completion tokens as a PowerShell single-quoted array body.
///
/// # Arguments
///
/// - `&str` - Whitespace-delimited token list.
///
/// # Returns
///
/// - `String` - Comma-separated PowerShell literals (e.g. `'a', 'b'`).
fn format_ps_string_list(tokens: &str) -> String {
    let quoted: Vec<String> = tokens
        .split_whitespace()
        .map(|tok: &str| format!("'{tok}'"))
        .collect();
    quoted.join(", ")
}