
    	D,j                         d Z ddlZddlZddlZddlZddlmZ ddlmZ dZ	dZ
dZ e            ZddZ	 	 	 	 dd
ZddZddZddZdS )a1  
Editor module for handling system text editor interactions.

This module provides functionality to:
- Discover and launch the system's configured text editor
- Create and manage temporary files for editing
- Handle editor preferences from environment variables
- Support cross-platform editor operations
    N)Console)dumpvivimnotepadc                 l    || rdnd}t                               ||           t          d           dS )z
    Print a status message with appropriate styling.

    :param success: Whether the operation was successful
    :param message: The message to display
    :param style: Optional style override. If None, uses green for success and red for failure
    Nz
bold greenzbold red)style )consoleprint)successmessager	   s      [/home/rurouni/.local/share/uv/tools/aider-chat/lib/python3.11/site-packages/aider/editor.pyprint_status_messager      s<     } '7ZMM'M'''	"IIIII    r
   c                    ||d}|rd| |d<   t          j        di |\  }}	 t          j        |d          5 }|                    |            ddd           n# 1 swxY w Y   n## t
          $ r t          j        |            w xY w|S )a  
    Create a temporary file with the given input data.

    :param input_data: Content to write to the temporary file
    :param suffix: Optional file extension (without the dot)
    :param prefix: Optional prefix for the temporary filename
    :param dir: Optional directory to create the file in
    :return: Path to the created temporary file
    :raises: OSError if file creation or writing fails
    )prefixdir.suffixwN )tempfilemkstemposfdopenwrite	Exceptionclose)
input_datar   r   r   kwargsfdfilepathfs           r   write_temp_filer%   )   s      s++F ('v<<x#--f--LBYr3 	 1GGJ	  	  	  	  	  	  	  	  	  	  	  	  	  	  	    
 Os.   A( AA( A  A( #A $A( ( Bc                     t           j                            dt           j                            d|                     }|S )a  
    Fetches the preferred editor from the environment variables.

    This function checks the following environment variables in order to
    determine the user's preferred editor:

     - VISUAL
     - EDITOR

    :param default: The default editor to return if no environment variable is set.
    :type default: str or None
    :return: The preferred editor as specified by environment variables or the default value.
    :rtype: str or None
    VISUALEDITOR)r   environget)defaulteditors     r   get_environment_editorr-   F   s.     Z^^HbjnnXw&G&GHHFMr   c                     t          j                    }|dk    rt          }n|dk    rt          }nt          }| r| }nt          |          }|S )a	  
    Discovers and returns the appropriate editor command.

    Handles cases where the editor command includes arguments, including quoted arguments
    with spaces (e.g. 'vim -c "set noswapfile"').

    :return: The editor command as a string
    :rtype: str
    WindowsDarwin)platformsystemDEFAULT_EDITOR_WINDOWSDEFAULT_EDITOR_OS_XDEFAULT_EDITOR_NIXr-   )editor_overrider2   default_editorr,   s       r   discover_editorr8   Y   s[     _F/	8		,+ 8 '77Mr   c                 l   t          | |          }t          |          }|d|z   z  }t          j        |d           t	          |d          5 }|                                }ddd           n# 1 swxY w Y   	 t          j        |           n$# t          $ r t          dd|d           Y nw xY w|S )	ay  
    Opens the system editor with optional input data and returns the edited content.

    This function creates a temporary file with the provided input data, opens it in
    the system editor, waits for the user to make changes and close the editor, then
    reads and returns the modified content. The temporary file is deleted afterwards.

    :param input_data: Initial content to populate the editor with
    :type input_data: str
    :param suffix: Optional file extension for the temporary file (e.g. '.txt', '.md')
    :type suffix: str or None
    :return: The edited content after the editor is closed
    :rtype: str
     T)shellrNFz)WARNING: Unable to delete temporary file z%. You may need to delete it manually.)
r%   r8   
subprocesscallopenreadr   removePermissionErrorr   )r    r   r,   r#   command_strr$   output_datas          r   pipe_editorrE   s   s"    z622H!&))K3>!KOKt,,,,	h		 ffhh              	

	( 
 
 
H   	
 	
 	
 	
 	

 s$   A//A36A3;B B10B1)N)r
   NNN)r
   NN)__doc__r   r1   r=   r   rich.consoler   
aider.dumpr   r5   r4   r3   r   r   r%   r-   r8   rE   r   r   r   <module>rI      s     
			                          " 
'))    	   :   &   4           r   