
    Ki"                         d Z ddl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z  fdZd	e	d
   dedefdZdeded	e	d   defdZdedef   defdZy)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returnc                     t         j                  j                  d      } | r t        |       }|j	                         r|dz  S t         j                  j                  d      }|syt        |      }|j	                         r|dz  dz  S y)a}  Detect oh-my-zsh custom completions directory.

    Uses ``$ZSH_CUSTOM/completions`` (the recommended location for user
    completions in oh-my-zsh). Falls back to ``$ZSH/custom/completions``
    when ``$ZSH_CUSTOM`` is not set.

    Returns
    -------
    Path | None
        Path to the oh-my-zsh custom completions directory, or None if
        oh-my-zsh is not detected.
    
ZSH_CUSTOMcompletionsZSHNcustom)osenvirongetr   is_dir)zsh_custom_str
zsh_customzsh_dir_strzsh_dirs       g/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/completion/install.py_detect_omz_completions_dirr      sy     ZZ^^L1N.)
--**..'K;G~~!M11    shellzshbashfish	prog_namec                    t        j                         }| dk(  rJt               }||j                  dd       |d| z  S |dz  dz  }|j                  dd       |d| z  S | dk(  r&|dz  d	z  d
z  dz  }|j                  dd       ||z  S | dk(  r&|dz  dz  dz  }|j                  dd       || dz  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   T)parentsexist_ok_z.zshr   r   z.localsharezbash-completionr   z.configz.fishzUnsupported shell: )r   homer   mkdir
ValueError)r   r   r$   omz_completionszsh_completionsbash_completionsfish_completionss          r   get_default_completion_pathr+   +   s   ( 99;D~57&!!$!>"q_44--7dT:1YK00	&(?W47HH=Xtd;)++	&)+f4}Dtd;YKu"555.ug677r   script_path)r   r   c                    |dk(  r't        j                         dz  }d|  d|  d}d| d}nF|dk(  r;t               ry	t        j                         d
z  }| j                  }d| d}d| d}nt        |j                         }|j                         r6|j                         }|dk(  rt        | j                        |v rd|v ry	||v ry	d}|dk(  r|j                  | d| d|        y|xr |j                  d       }|j                  d      5 }	|r|	j                  d       |	j                  | d| d       ddd       y# 1 sw Y   yxY w)a  Add completion configuration to shell RC file.

    For bash, adds a source line to load the completion script.
    For zsh, adds the completion directory to fpath so compinit can find it.

    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 configuration was added, False if it already existed or on error.
    r   .bashrcz[ -f "z
" ] && . ""z# Load z completionr   F.zshrczfpath=( $fpath)z# z completionszfpath= 
aNT)r   r$   r   parentNotImplementedErrorresolveexists	read_textstr
write_textendswithopenwrite)
r,   r   r   rc_fileconfig_linecommentcompletion_dircontentneeds_newlinefs
             r   add_to_rc_filerF   U   sy   ( ))+	){m:k]!DI;k2	%&())+($++/x8yk.!!ooG~~##% E>c+"4"45@XQXEXG#~gYbRyAB   >(8(8(>$>\\# 	3!GGwir+b12	3
 	3
 s   +EEinstall_completion_fn.add_to_startupc           
           ddddt         t        d   dz  t               f   dt         t        dz  t        ddg      f   f f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   rJ   z-oz--output)namec                    ddl m}m} | 	  |       }  	| |      }t        d|        | d	k(  rt               rt        d
       t        d       yrKt        j                         dz  }|j                  }t        d| d|        t        d       t        d       y|j                  }t        d| d       t        d       t        d| d       t        d       t        d       y| dk(  rr1t        j                         dz  }t        d|        t        d       yt        d       t        d       t        d       t        d       t        d        t        d!       t        d"       t        d#       y| d$k(  rt        d%       t        d&       yt        # |$ r4 t        dt        j
                         t	        j                  d       Y w xY w)'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   rJ   rH   u#   ✓ Completion script installed to r   uC   ✓ Detected oh-my-zsh: completions directory is already in $fpath.z$
Restart your shell or run: exec zshr0   u
   ✓ Added z to fpath in zX
Note: Ensure compinit is configured in your .zshrc (most zsh setups already have this).z#Restart your shell or run: exec zshz
To enable completions, ensure z is in your $fpath.z@Add this to your ~/.zshrc or ~/.zprofile if not already present:z    fpath=(r1   z%    autoload -Uz compinit && compinitz)
Then restart your shell or run: exec zshr   r.   u   ✓ Added completion loader to 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.detectrM   rN   printsysstderrexitr   r   r$   r5   r6   )
r   rJ   rM   rN   install_pathzshrcrB   bashrcrH   rG   s
           r   _install_completion_commandzFcreate_install_completion_command.<locals>._install_completion_command   s   $ 	Q=$ -5Xfg3L>BCE>*,[\=>		h.!-!4!4
>"2-wGHqr;<!-!4!488HH[\]XYN#38<==>BCf_y07x@AEFVW23HIABKLABFGDEf_CDPQ%%] ' U s   F 5GG)r   r   r   r   )rG   rH   rY   s   `` r   !create_install_completion_commandrZ      s_    , PTMQE&!67$>	KLE& $+ytZ6H'IIJE&N '&r   )__doc__r   rS   collections.abcr   pathlibr   typingr   r   cyclopts.parameterr   r   r:   r+   boolrF   rZ    r   r   <module>rb      s    
 
 $  % (TD[ 6'8w/D'E '8RU '8Z^ '8T: : :W]=S :X\ :z['#CI.['['r   