<s> import urwid <EOL> from urwid . util import _target_encoding <EOL> def make_canvas ( txt , attr , maxcol , fill_attr = None ) : <EOL> processed_txt = [ ] <EOL> processed_attr = [ ] <EOL> processed_cs = [ ] <EOL> for line , line_attr in zip ( txt , attr ) : <EOL> line_attr = [ ( aname , l ) for aname , l in line_attr if l > <NUM_LIT:0> ] <EOL> diff = maxcol - len ( line ) <EOL> if diff > <NUM_LIT:0> : <EOL> line += "<STR_LIT:U+0020>" * diff <EOL> line_attr . append ( ( fill_attr , diff ) ) <EOL> else : <EOL> from urwid . util import rle_subseg <EOL> line = line [ : maxcol ] <EOL> line_attr = rle_subseg ( line_attr , <NUM_LIT:0> , maxcol ) <EOL> from urwid . util import apply_target_encoding <EOL> encoded_line , line_cs = apply_target_encoding ( line ) <EOL> def get_byte_line_attr ( line , line_attr ) : <EOL> i = <NUM_LIT:0> <EOL> for label , column_count in line_attr : <EOL> byte_count = len ( line [ i : i + column_count ] . encode ( _target_encoding ) ) <EOL> i += column_count <EOL> yield label , byte_count <EOL> line_attr = list ( get_byte_line_attr ( line , line_attr ) ) <EOL> processed_txt . append ( encoded_line ) <EOL> processed_attr . append ( line_attr ) <EOL> processed_cs . append ( line_cs ) <EOL> return urwid . TextCanvas ( <EOL> processed_txt , <EOL> processed_attr , <EOL> processed_cs , <EOL> maxcol = maxcol ) <EOL> def make_hotkey_markup ( s ) : <EOL> import re <EOL> match = re . match ( r"<STR_LIT>" , s ) <EOL> assert match is not None <EOL> return [ <EOL> ( None , match . group ( <NUM_LIT:1> ) ) , <EOL> ( "<STR_LIT>" , match . group ( <NUM_LIT:2> ) ) , <EOL> ( None , match . group ( <NUM_LIT:3> ) ) , <EOL> ] <EOL> def labelled_value ( label , value ) : <EOL> return urwid . AttrMap ( urwid . Text ( [ <EOL> ( "<STR_LIT:label>" , label ) , str ( value ) ] ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> class SelectableText ( urwid . Text ) : <EOL> def selectable ( self ) : <EOL> return True <EOL> def keypress ( self , size , key ) : <EOL> return key <EOL> class SignalWrap ( urwid . WidgetWrap ) : <EOL> def __init__ ( self , w , is_preemptive = False ) : <EOL> urwid . WidgetWrap . __init__ ( self , w ) <EOL> self . event_listeners = [ ] <EOL> self . is_preemptive = is_preemptive <EOL> def listen ( self , mask , handler ) : <EOL> self . event_listeners . append ( ( mask , handler ) ) <EOL> def keypress ( self , size , key ) : <EOL> result = key <EOL> if self . is_preemptive : <EOL> for mask , handler in self . event_listeners : <EOL> if mask is None or mask == key : <EOL> result = handler ( self , size , key ) <EOL> break <EOL> if result is not None : <EOL> result = self . _w . keypress ( size , key ) <EOL> if result is not None and not self . is_preemptive : <EOL> for mask , handler in self . event_listeners : <EOL> if mask is None or mask == key : <EOL> return handler ( self , size , key ) <EOL> return result <EOL> class StackFrame ( urwid . FlowWidget ) : <EOL> def __init__ ( self , is_current , name , class_name , filename , line ) : <EOL> self . is_current = is_current <EOL> self . name = name <EOL> self . class_name = class_name <EOL> self . filename = filename <EOL> self . line = line <EOL> def selectable ( self ) : <EOL> return True <EOL> def rows ( self , size , focus = False ) : <EOL> return <NUM_LIT:1> <EOL> def render ( self , size , focus = False ) : <EOL> maxcol = size [ <NUM_LIT:0> ] <EOL> if focus : <EOL> apfx = "<STR_LIT>" <EOL> else : <EOL> apfx = "<STR_LIT>" <EOL> if self . is_current : <EOL> apfx += "<STR_LIT>" <EOL> crnt_pfx = "<STR_LIT>" <EOL> else : <EOL> crnt_pfx = "<STR_LIT:U+0020>" <EOL> text = crnt_pfx + self . name <EOL> attr = [ ( apfx + "<STR_LIT>" , <NUM_LIT:3> + len ( self . name ) ) ] <EOL> if self . class_name is not None : <EOL> text += "<STR_LIT>" % self . class_name <EOL> attr . append ( ( apfx + "<STR_LIT>" , len ( self . class_name ) + <NUM_LIT:3> ) ) <EOL> loc = "<STR_LIT>" % ( self . filename , self . line ) <EOL> text += loc <EOL> attr . append ( ( apfx + "<STR_LIT>" , len ( loc ) ) ) <EOL> return make_canvas ( [ text ] , [ attr ] , maxcol , apfx + "<STR_LIT>" ) <EOL> def keypress ( self , size , key ) : <EOL> return key <EOL> class BreakpointFrame ( urwid . FlowWidget ) : <EOL> def __init__ ( self , is_current , filename , line ) : <EOL> self . is_current = is_current <EOL> self . filename = filename <EOL> self . line = line <EOL> def selectable ( self ) : <EOL> return True <EOL> def rows ( self , size , focus = False ) : <EOL> return <NUM_LIT:1> <EOL> def render ( self , size , focus = False ) : <EOL> maxcol = size [ <NUM_LIT:0> ] <EOL> if focus : <EOL> apfx = "<STR_LIT>" <EOL> else : <EOL> apfx = "<STR_LIT>" <EOL> if self . is_current : <EOL> apfx += "<STR_LIT>" <EOL> crnt_pfx = "<STR_LIT>" <EOL> else : <EOL> crnt_pfx = "<STR_LIT:U+0020>" <EOL> loc = "<STR_LIT>" % ( self . filename , self . line ) <EOL> text = crnt_pfx + loc <EOL> attr = [ ( apfx + "<STR_LIT>" , len ( loc ) ) ] <EOL> return make_canvas ( [ text ] , [ attr ] , maxcol , apfx + "<STR_LIT>" ) <EOL> def keypress ( self , size , key ) : <EOL> return key <EOL> class SearchController ( object ) : <EOL> def __init__ ( self , ui ) : <EOL> self . ui = ui <EOL> self . highlight_line = None <EOL> self . search_box = None <EOL> self . last_search_string = None <EOL> def cancel_highlight ( self ) : <EOL> if self . highlight_line is not None : <EOL> self . highlight_line . set_highlight ( False ) <EOL> self . highlight_line = None <EOL> def cancel_search ( self ) : <EOL> self . cancel_highlight ( ) <EOL> self . hide_search_ui ( ) <EOL> def hide_search_ui ( self ) : <EOL> self . search_box = None <EOL> del self . ui . lhs_col . contents [ <NUM_LIT:0> ] <EOL> self . ui . lhs_col . set_focus ( self . ui . lhs_col . widget_list [ <NUM_LIT:0> ] ) <EOL> def open_search_ui ( self ) : <EOL> lhs_col = self . ui . lhs_col <EOL> if self . search_box is None : <EOL> _ , self . search_start = self . ui . source . get_focus ( ) <EOL> self . search_box = SearchBox ( self ) <EOL> self . search_AttrMap = urwid . AttrMap ( <EOL> self . search_box , "<STR_LIT>" ) <EOL> lhs_col . item_types . insert ( <EOL> <NUM_LIT:0> , ( "<STR_LIT>" , None ) ) <EOL> lhs_col . widget_list . insert ( <NUM_LIT:0> , self . search_AttrMap ) <EOL> self . ui . columns . set_focus ( lhs_col ) <EOL> lhs_col . set_focus ( self . search_AttrMap ) <EOL> else : <EOL> self . ui . columns . set_focus ( lhs_col ) <EOL> lhs_col . set_focus ( self . search_AttrMap ) <EOL> def perform_search ( self , dir , s = None , start = None , update_search_start = False ) : <EOL> self . cancel_highlight ( ) <EOL> if s is None : <EOL> s = self . last_search_string <EOL> if s is None : <EOL> self . ui . message ( "<STR_LIT>" ) <EOL> return False <EOL> else : <EOL> self . last_search_string = s <EOL> if start is None : <EOL> start = self . search_start <EOL> case_insensitive = s . lower ( ) == s <EOL> if start > len ( self . ui . source ) : <EOL> start = <NUM_LIT:0> <EOL> i = ( start + dir ) % len ( self . ui . source ) <EOL> if i >= len ( self . ui . source ) : <EOL> i = <NUM_LIT:0> <EOL> while i != start : <EOL> sline = self . ui . source [ i ] . text <EOL> if case_insensitive : <EOL> sline = sline . lower ( ) <EOL> if s in sline : <EOL> sl = self . ui . source [ i ] <EOL> sl . set_highlight ( True ) <EOL> self . highlight_line = sl <EOL> self . ui . source . set_focus ( i ) <EOL> if update_search_start : <EOL> self . search_start = i <EOL> return True <EOL> i = ( i + dir ) % len ( self . ui . source ) <EOL> return False <EOL> class SearchBox ( urwid . Edit ) : <EOL> def __init__ ( self , controller ) : <EOL> urwid . Edit . __init__ ( self , [ ( "<STR_LIT:label>" , "<STR_LIT>" ) ] , "<STR_LIT>" ) <EOL> self . controller = controller <EOL> def restart_search ( self ) : <EOL> from time import time <EOL> now = time ( ) <EOL> if self . search_start_time > <NUM_LIT:5> : <EOL> self . set_edit_text ( "<STR_LIT>" ) <EOL> self . search_time = now <EOL> def keypress ( self , size , key ) : <EOL> result = urwid . Edit . keypress ( self , size , key ) <EOL> txt = self . get_edit_text ( ) <EOL> if result is not None : <EOL> if key == "<STR_LIT>" : <EOL> self . controller . cancel_search ( ) <EOL> return None <EOL> elif key == "<STR_LIT>" : <EOL> if txt : <EOL> self . controller . hide_search_ui ( ) <EOL> self . controller . perform_search ( dir = <NUM_LIT:1> , s = txt , <EOL> update_search_start = True ) <EOL> else : <EOL> self . controller . cancel_search ( ) <EOL> return None <EOL> else : <EOL> if self . controller . perform_search ( dir = <NUM_LIT:1> , s = txt ) : <EOL> self . controller . search_AttrMap . set_attr_map ( { None : "<STR_LIT>" } ) <EOL> else : <EOL> self . controller . search_AttrMap . set_attr_map ( <EOL> { None : "<STR_LIT>" } ) <EOL> return result </s>
