o
    i	                     @   sl   d Z ddlZddlZddlmZ ddlmZ G dd deZde	ded	 dB fd
dZ
ded	 fddZdS )a
  Shell detection utilities for completion generation.

This module provides functionality to detect the current shell type by inspecting
environment variables. This is useful for dynamically generating appropriate completion
scripts for different shell environments.
    N)Path)Literalc                   @   s   e Zd ZdZdS )ShellDetectionErrorz.Raised when the shell type cannot be detected.N)__name__
__module____qualname____doc__ r	   r	   `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/completion/detect.pyr      s    r   shell_stringreturn)zshbashfishc                 C   s0   |   }d|v r
dS d|v rdS d|v rdS dS )aD  Extract shell name from a string (path or process name).

    Parameters
    ----------
    shell_string : str
        String that may contain a shell name (e.g., "/bin/bash", "zsh", "-bash").

    Returns
    -------
    Literal["zsh", "bash", "fish"] | None
        The detected shell type, or None if not recognized.
    r   r   r   N)lower)r   shell_lowerr	   r	   r
   _extract_shell_name   s   r   c               
   C   s   t jdrdS t jdrdS t jdrdS z,t  } tjddt| d	d
gdddd}|jdkrC|jrC|j	 }t
|}|rC|W S W n tjttfyQ   Y nw t jdd}|rht|j}t
|}|rh|S td)a  Detect the current shell type using multiple detection methods.

    Returns
    -------
    Literal["zsh", "bash", "fish"]
        The detected shell type.

    Raises
    ------
    ShellDetectionError
        If the shell type cannot be determined from any detection method.

    Examples
    --------
    >>> shell = detect_shell()  # doctest: +SKIP
    >>> print(f"Detected shell: {shell}")  # doctest: +SKIP
    Detected shell: bash
    ZSH_VERSIONr   BASH_VERSIONr   FISH_VERSIONr   psz-pz-ozcomm=T   )capture_outputtexttimeoutr   SHELL zUnable to detect shell type.)osenvirongetgetppid
subprocessrunstr
returncodestdoutstripr   SubprocessErrorFileNotFoundErrorOSErrorr   namer   )ppidresultparent_processshell
shell_path
shell_namer	   r	   r
   detect_shell)   s<   

r1   )r   r   r!   pathlibr   typingr   	Exceptionr   r#   r   r1   r	   r	   r	   r
   <module>   s    