
    kj&*                     ^   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ  G d dej$                        Z G d d      Z G d dej$                        Z G d dej,                  j.                        Zy)    N)flow)commands)common)eventlog)flowlist)flowview)
grideditor)help)keybindings)options)overlay)signals)	statusbarc                   2     e Zd Z fdZ fdZ fdZ xZS )StackWidgetc                     || _         || _        |r.t        j                  t        j                  |      |rdnd      }nd }t
        |   ||       y )Nheadingheading_inactive)header)
is_focusedwindowurwidAttrMapTextsuper__init__)selfr   widgettitlefocusr   	__class__s         g/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/tools/console/window.pyr   zStackWidget.__init__   sN    ]]

5!9;MF F/    c                     |dk(  r+|dk(  r&| j                   s| j                  j                          t        |   ||||||      S )Nmouse press   )r   r   switchr   mouse_event)r   sizeeventbuttoncolrowr    r!   s          r"   r(   zStackWidget.mouse_event"   sA    M!fk$//KK w"4S%HHr#   c                 n    t         |   ||      }| j                  |   }|r|j                  d      ry |S )Ncursor)r   keypress_command_map
startswith)r   r)   keyretcommandr!   s        r"   r0   zStackWidget.keypress'   sC     gtS)##
 w))(3
r#   )__name__
__module____qualname__r   r(   r0   __classcell__r!   s   @r"   r   r      s    
0I

 
r#   r   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	WindowStackc                 8   || _         t        di dt        j                  |      dt	        j
                  |      dt        j                  |      dt        j                  |      dt        j                  |      dt        j                  |      dt        j                  |      dt!        j"                  |      d	t!        j$                  |      d
t!        j&                  |      dt!        j(                  |      dt!        j*                  |      dt!        j,                  |      dt!        j.                  |      dt!        j0                  |      dt!        j2                  |      dt!        j4                  |      | _        |g| _        d | _        y )Nr   r   r   r   r   r
   r   edit_focus_queryedit_focus_commentedit_focus_cookiesedit_focus_setcookiesedit_focus_setcookie_attrsedit_focus_multipart_formedit_focus_urlencoded_formedit_focus_pathedit_focus_request_headersedit_focus_response_headers )masterdictr   FlowListBoxr   FlowViewr   Commandsr   KeyBindingsr   Optionsr
   HelpViewr   EventLogr	   QueryEditorCommentEditorCookieEditorSetCookieEditorCookieAttributeEditorRequestMultipartEditorRequestUrlEncodedEditor
PathEditorRequestHeaderEditorResponseHeaderEditorwindowsstackr   )r   rI   bases      r"   r   zWindowStack.__init__5   s    
))&1
&&v.
 &&v.
 $//7	

 OOF+
 v&
 &&v.
 (33F;
  *77?
  *66v>
 #-"<"<V"D
 (2'G'G'O
 '1&G&G&O
 (2'I'I&'Q
 '11&9
  (2'E'Ef'M!
" )3(G(G(O#
& V
r#   c                 p    t        j                  | || j                         |j                  fi || _         y N)r   SimpleOverlay
top_widgetwidthr   okwargss      r"   set_overlayzWindowStack.set_overlayM   s5    ,,OOGG	

 
r#   c                 :    | j                   | j                  d      S )z<
        The current top window, ignoring overlays.
        )r\   r]   r   s    r"   
top_windowzWindowStack.top_windowV   s     ||DJJrN++r#   c                 R    | j                   r| j                   S | j                         S )zQ
        The current top widget - either a window or the active overlay.
        )r   rk   rj   s    r"   rb   zWindowStack.top_widget\   s"     <<<<  r#   c                     | j                   d   |k(  ry | j                         }| j                   j                  |       | j                  d|       y )Nri   layout_pushed)r]   rk   appendcall)r   wnameprevs      r"   pushzWindowStack.pushd   sA    ::b>U" 

% 		/4(r#   c                     | j                   st        | j                        dk(  ry| j                  d       | j                   rd| _         y| j                  j	                          y)zM
        Pop off the stack, return True if we're already at the top.
        r&   Tlayout_poppingN)r   lenr]   rp   pop)r   argsrf   s      r"   rw   zWindowStack.popk   sF     ||DJJ1 4		"#<<DLJJNNr#   c                      t        | j                         |      |i | | j                  r t        | j                  |      |i | yy)z
        Call a function on both the top window, and the overlay if there is
        one. If the widget has a key_responder, we call the function on the
        responder instead.
        N)getattrrk   r   )r   namerx   rf   s       r"   rp   zWindowStack.callw   sG     	)!4($9&9<<'GDLL$'88 r#   N)
r6   r7   r8   r   rg   rk   rb   rs   rw   rp   rH   r#   r"   r<   r<   4   s%    0
,!)
9r#   r<   c                        e Zd Z fdZd Zd Zd Zdej                  ddfdZ	d	 Z
d
 Zd Zd ZddZd Zd Zd Zd Zd Z fdZ fdZ xZS )Windowc                    t        j                  |      | _         t        |   d d t	        j
                  | j                   d             || _        | j                  j                  j                  j                  | j                         | j                  j                  j                  j                  | j                         | j                  j                  j                  j                  | j                         | j                  j                  j                  j                  | j                         | j                  j                  j                  j                  j                  | j                         | j                  j                  j                  j                  j                  | j                          t"        j                  j                  | j$                         t"        j&                  j                  | j(                         t"        j*                  j                  | j,                         | j                  j.                  j1                  | j2                  ddg       d| _        t7        |d      t7        |d      g| _        y )N
background)r   footerconsole_layoutconsole_layout_headersr   r   r   )r   	StatusBarr   r   r   r   rI   viewsig_view_refreshconnectview_changedsig_view_addsig_view_removesig_view_updater    
sig_changefocus_changedr   	sig_focusflow_changeflow_changedpop_view_staterw   r   	subscribe	configurepaner<   stacks)r   rI   r!   s     r"   r   zWindow.__init__   s   ",,V4emmDNNL&Q 	 	
 ))11$2C2CD%%--d.?.?@((001B1BC((001B1BC))11$2C2CD))11$2D2DEdnn-##D$5$56&&txx0%%NN-/GH	
 	"6:6FJ8WXr#   c                 4    | j                   | j                     S r`   )r   r   rj   s    r"   focus_stackzWindow.focus_stack   s    {{499%%r#   c                 $    | j                          y r`   )refresh)r   r   updateds      r"   r   zWindow.configure   s    r#   c           	      T     j                   j                  j                  }|dk(  rd _         fd}d}|dk(  r	 |d      }n|dk(  rNt	        j
                  t         j                        D cg c]  \  }} ||       c}} j                        }nNt	        j                  t         j                        D cg c]  \  }} ||       c}}d j                        }t	        j                  |d	       _
        t        j                  j                          yc c}}w c c}}w )
z$
        Redraw the layout.
        singler   c                    j                   |    j                         }j                  j                  j                  r(j                   |    j                         j                  }nd }t        ||j                  | k(        S r`   )	r   rb   rI   r   r   rk   r   r   r   )idxr   r   r   s      r"   wrappedzWindow.refresh.<locals>.wrapped   si    [[%002F{{""99C(335;;tVUDII4DEEr#   Nvertical)
focus_itemr&   )dividecharsfocus_columnr   )rI   r   r   r   r   Pile	enumerater   Columnsr   bodyr   window_refreshsend)r   cr   wiss   `     r"   r   zWindow.refresh   s     KK..=DI	F =
A*_

(1$++(>?1?DIIA (1$++(>?1?!YYA MM!\2	##% @ @s   /D
=D$
r   returnNc                     | j                   j                  j                  j                  rS|j                  | j                   j                  j                  j                  j                  k(  r| j                          y y y r`   )rI   r   r    r   idr   )r   r   s     r"   r   zWindow.flow_changed   sY    ;;!!&&ww$++**0055888""$ 9 'r#   c                 H    | j                   D ]  }|j                  d        y)z
        Triggered when the focus changes - either when it's modified, or
        when it changes to a different flow altogether.
        r   Nr   rp   r   rx   rf   r   s       r"   r   zWindow.focus_changed   s#    
  	$AFF?#	$r#   c                 H    | j                   D ]  }|j                  d        y)z;
        Triggered when the view list has changed.
        r   Nr   r   s       r"   r   zWindow.view_changed   s#      	#AFF>"	#r#   c                 f     | j                         j                  |fi | | j                          y)z@
        Set an overlay on the currently focused stack.
        N)r   rg   r   rd   s      r"   rg   zWindow.set_overlay   s+     	'&&q3F3r#   c                     | j                         j                  |       | j                          | j                          | j	                          y)zA
        Push a window onto the currently focused stack.
        N)r   rs   r   r   r   )r   rq   s     r"   rs   zWindow.push   s<     	&r#   c                     | j                         j                         r| j                  j                          y| j	                          | j                          | j                          y)z
        Pop a window from the currently focused stack. If there is only one
        window on the stack, this prompts for exit.
        N)r   rw   rI   prompt_for_exitr   r   r   rj   s    r"   rw   z
Window.pop   sJ    
 !!#KK'')LLN r#   c                     | j                   j                         }|j                  d|j                  | j                               |S )zQ
        Returns:
            self.stacks, with the focused stack first.
        r   )r   copyinsertrw   r   )r   r   s     r"   stacks_sorted_by_focuszWindow.stacks_sorted_by_focus   s6    
 !!#aDII./r#   c                 t    | j                         D ]%  }|j                         }|j                  |k(  s#|c S  y)z
        Returns the active widget with a matching key context, including overlays.
        If multiple stacks have an active widget with a matching key context,
        the currently focused stack is preferred.
        N)r   rb   keyctxr   r   r   ts       r"   currentzWindow.current   8     ,,. 	AAxx6!	r#   c                 t    | j                         D ]%  }|j                         }|j                  |k(  s#|c S  y)z
        Returns the active window with a matching key context, ignoring overlays.
        If multiple stacks have an active widget with a matching key context,
        the currently focused stack is preferred.
        N)r   rk   r   r   s       r"   current_windowzWindow.current_window  r   r#   c                     || _         y r`   )focus_position)r   sections     r"   r   zWindow.sig_focus  s
    %r#   c                     | j                   j                  j                  dk(  rd| _        n*| j                  dz   t	        | j
                        z  | _        | j                          y)z/
        Switch between the two panes.
        r   r   r&   N)rI   r   r   r   rv   r   r   rj   s    r"   r'   zWindow.switch  sI     ;;--9DIQ#dkk*::DIr#   c                    t        |   |i |}|sw|d   dk(  r"t        j                  j	                  dd       y|d   dk(  r|d   dk(  r| j                  |d   d	       y|d   dk(  r|d   d
k(  r| j                  |d   d       yyy )Nr&   z
mouse dragz_Hold down fn, shift, alt or ctrl to select text or use the --set console_mouse=false parameter.)messageexpirer%         r   up   downFT)r   r(   r   status_messager   r0   )r   rx   rf   kr!   s       r"   r(   zWindow.mouse_event  s    G00Aw,&&&++} ,   aM)d1gld1gt,
 	 aM)d1gld1gv.   r#   c                     t         |   ||      }|rL| j                  j                  j	                  | j                         j                         j                  |      S y r`   )r   r0   rI   keymaphandler   rb   r   )r   r)   r   r!   s      r"   r0   zWindow.keypress.  sR    GT1%;;%%,,T-=-=-?-J-J-L-S-SUVWW r#   r   N)r6   r7   r8   r   r   r   r   r   Flowr   r   r   rg   rs   rw   r   r   r   r   r'   r(   r0   r9   r:   s   @r"   r}   r}      ss    Y.&&B% %t %
$#
!		&"X Xr#   r}   c                   *     e Zd Zd fdZ fdZ xZS )Screenc                 V    t         |           t        j                  d      | _        y )Nr   )r   r   logging	getLoggerlogger)r   r!   s    r"   r   zScreen.__init__5  s     ''0r#   c                 r    t         j                  rt        j                  dd|      }t        |   |       y )Nz[] )r   IS_WINDOWS_OR_WSLresubr   write)r   datar!   s     r"   r   zScreen.write9  s,    ## 66,D1Ddr#   r   )r6   r7   r8   r   r   r9   r:   s   @r"   r   r   4  s    1 r#   r   )r   r   r   	mitmproxyr   mitmproxy.tools.consoler   r   r   r   r   r	   r
   r   r   r   r   r   Framer   r<   r}   raw_displayr   rH   r#   r"   <module>r      s     	   , * , , , . ( / + + + -%++ >K9 K9\oXU[[ oXd
U%% 
r#   