o
    i                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ded d	ed
efddZded	eded d
efddZdedef defddZdS )zShell completion installation utilities.

This module handles the installation of completion scripts to shell-specific
locations and the updating of shell RC files to load completions.
    N)Callable)Path)	AnnotatedLiteral)	Parametershellzshbashfish	prog_namereturnc                 C   s   t  }| dkr|d d }|jddd |d|  S | dkr5|d d	 d
 d }|jddd || S | dkrO|d d d }|jddd || d S td|  )a  Get the default completion script path for a given shell.

    Parameters
    ----------
    shell : Literal["zsh", "bash", "fish"]
        Shell type.
    prog_name : str
        Program name for the completion script.

    Returns
    -------
    Path
        Default installation path for the shell.

    Raises
    ------
    ValueError
        If shell type is unsupported.
    r	   z.zshcompletionsT)parentsexist_ok_r
   z.localsharezbash-completionr   z.configz.fishzUnsupported shell: )r   homemkdir
ValueError)r   r   r   zsh_completionsbash_completionsfish_completions r   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/completion/install.pyget_default_completion_path   s   r   script_path)r
   r	   c                 C   s   |dkrt  d }d|  d|  d}n|dkr(t  d }d|  d|  d}nt| }| rE| }||v r<dS |oC|d	 }nd}|d
}|rT|d	 |d| d| d	 W d   dS 1 skw   Y  dS )a  Add source line to shell RC file to ensure completion is loaded.

    Parameters
    ----------
    script_path : Path
        Path to the completion script.
    prog_name : str
        Program name for display in comments.
    shell : Literal["bash", "zsh"]
        Shell type.

    Returns
    -------
    bool
        True if the source line was added, False if it already existed or on error.
    r
   .bashrcz[ -f "z
" ] && . ""r	   .zshrcF
az# Load z completion
NT)	r   r   NotImplementedErrorresolveexists	read_textendswithopenwrite)r   r   r   rc_filesource_linecontentneeds_newlinefr   r   r   add_to_rc_file4   s,   

r.   install_completion_fn.add_to_startupc              	      sL   ddddt td dB t f dt tdB tddgdf f fd	d
}|S )a  Create a command function for installing shell completion.

    Parameters
    ----------
    install_completion_fn : Callable
        Function that performs the actual installation (typically App.install_completion).
        Should accept (shell, output, add_to_startup) and return the installation path.
    add_to_startup : bool
        Whether to add source line to shell RC file.

    Returns
    -------
    Callable
        Command function that can be registered with App.command().
    N)r   outputr   r   r1   z-oz--output)namec                    st  ddl m}m} | du r'z| } W n |y&   tdtjd td Y nw | | d}td|  | d	kro rNt d
 }td|  td dS |j	}td| d td td| d td td dS | dkr rt d }td|  td dS td td td td td td td td dS | dkrtd  td! dS t
)"aR  Install shell completion for this application.

        This command generates and installs the completion script to the appropriate
        location for your shell. After installation, you may need to restart your
        shell or source your shell configuration file.

        Parameters
        ----------
        shell : Literal["zsh", "bash", "fish"] | None
            Shell type for completion. If not specified, attempts to auto-detect current shell.
        output : Path | None
            Output path for the completion script. If not specified, uses shell-specific default.
        r   )ShellDetectionErrordetect_shellNz?Could not auto-detect shell. Please specify --shell explicitly.)file   )r   r1   r0   u#   ✓ Completion script installed to r	   r   u   ✓ Added completion loader to z+
Restart your shell or run: source ~/.zshrcz
To enable completions, ensure z is in your $fpath.z@Add this to your ~/.zshrc or ~/.zprofile if not already present:z    fpath=(z $fpath)z%    autoload -Uz compinit && compinitz)
Then restart your shell or run: exec zshr
   r   z,
Restart your shell or run: source ~/.bashrcz=
Completions will be automatically loaded by bash-completion.zIf completions don't work:z0  1. Ensure bash-completion is installed (v2.8+)z)  2. Restart your shell or run: exec bashz2
Note: bash-completion is typically installed via:z)  - macOS: brew install bash-completion@2z.  - Debian/Ubuntu: apt install bash-completionz,  - Fedora/RHEL: dnf install bash-completionr   z.
Completions are automatically loaded in fish.z<Restart your shell or run: source ~/.config/fish/config.fish)cyclopts.completion.detectr3   r4   printsysstderrexitr   r   parentr"   )r   r1   r3   r4   install_pathzshrccompletion_dirbashrcr0   r/   r   r   _install_completion_commandt   sR   
zFcreate_install_completion_command.<locals>._install_completion_command)r   r   r   r   )r/   r0   rB   r   rA   r   !create_install_completion_command`   s   BrC   )__doc__r9   collections.abcr   pathlibr   typingr   r   cyclopts.parameterr   strr   boolr.   rC   r   r   r   r   <module>   s    %,
