<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals , division <EOL> import copy <EOL> import math <EOL> import operator <EOL> import sys <EOL> from collections import deque , namedtuple <EOL> from itertools import islice , repeat <EOL> from . import modes as mo , graphics as g , charsets as cs <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> from future_builtins import map <EOL> range = xrange <EOL> def take ( n , iterable ) : <EOL> """<STR_LIT>""" <EOL> return list ( islice ( iterable , n ) ) <EOL> Margins = namedtuple ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> Savepoint = namedtuple ( "<STR_LIT>" , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> _Char = namedtuple ( "<STR_LIT>" , [ <EOL> "<STR_LIT:data>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> class Char ( _Char ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def __new__ ( cls , data , fg = "<STR_LIT:default>" , bg = "<STR_LIT:default>" , bold = False , <EOL> italics = False , underscore = False , reverse = False , <EOL> strikethrough = False ) : <EOL> return _Char . __new__ ( cls , data , fg , bg , bold , italics , underscore , <EOL> strikethrough , reverse ) <EOL> class Cursor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x , y , attrs = Char ( "<STR_LIT:U+0020>" ) ) : <EOL> self . x , self . y , self . attrs , self . hidden = x , y , attrs , False <EOL> class Screen ( object ) : <EOL> """<STR_LIT>""" <EOL> default_char = Char ( data = "<STR_LIT:U+0020>" , fg = "<STR_LIT:default>" , bg = "<STR_LIT:default>" ) <EOL> default_line = repeat ( default_char ) <EOL> def __init__ ( self , columns , lines ) : <EOL> self . savepoints = [ ] <EOL> self . lines , self . columns = lines , columns <EOL> self . buffer = [ ] <EOL> self . reset ( ) <EOL> def __repr__ ( self ) : <EOL> return ( "<STR_LIT>" . format ( self . __class__ . __name__ , <EOL> self . columns , self . lines ) ) <EOL> def __before__ ( self , command ) : <EOL> """<STR_LIT>""" <EOL> def __after__ ( self , command ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . lines , self . columns <EOL> @ property <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" . join ( map ( operator . attrgetter ( "<STR_LIT:data>" ) , line ) ) <EOL> for line in self . buffer ] <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . buffer [ : ] = ( take ( self . columns , self . default_line ) <EOL> for _ in range ( self . lines ) ) <EOL> self . mode = set ( [ mo . DECAWM , mo . DECTCEM ] ) <EOL> self . margins = Margins ( <NUM_LIT:0> , self . lines - <NUM_LIT:1> ) <EOL> self . charset = <NUM_LIT:0> <EOL> self . g0_charset = cs . IBMPC_MAP <EOL> self . g1_charset = cs . VT100_MAP <EOL> self . tabstops = set ( range ( <NUM_LIT:7> , self . columns , <NUM_LIT:8> ) ) <EOL> self . cursor = Cursor ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . cursor_position ( ) <EOL> def resize ( self , lines = None , columns = None ) : <EOL> """<STR_LIT>""" <EOL> lines = lines or self . lines <EOL> columns = columns or self . columns <EOL> diff = self . lines - lines <EOL> if diff < <NUM_LIT:0> : <EOL> self . buffer . extend ( take ( self . columns , self . default_line ) <EOL> for _ in range ( diff , <NUM_LIT:0> ) ) <EOL> elif diff > <NUM_LIT:0> : <EOL> self . buffer [ : diff ] = ( ) <EOL> diff = self . columns - columns <EOL> if diff < <NUM_LIT:0> : <EOL> for y in range ( lines ) : <EOL> self . buffer [ y ] . extend ( take ( abs ( diff ) , self . default_line ) ) <EOL> elif diff > <NUM_LIT:0> : <EOL> for line in self . buffer : <EOL> del line [ columns : ] <EOL> self . lines , self . columns = lines , columns <EOL> self . margins = Margins ( <NUM_LIT:0> , self . lines - <NUM_LIT:1> ) <EOL> self . reset_mode ( mo . DECOM ) <EOL> def set_margins ( self , top = None , bottom = None ) : <EOL> """<STR_LIT>""" <EOL> if top is None or bottom is None : <EOL> return <EOL> top = max ( <NUM_LIT:0> , min ( top - <NUM_LIT:1> , self . lines - <NUM_LIT:1> ) ) <EOL> bottom = max ( <NUM_LIT:0> , min ( bottom - <NUM_LIT:1> , self . lines - <NUM_LIT:1> ) ) <EOL> if bottom - top >= <NUM_LIT:1> : <EOL> self . margins = Margins ( top , bottom ) <EOL> self . cursor_position ( ) <EOL> def set_charset ( self , code , mode ) : <EOL> """<STR_LIT>""" <EOL> if code in cs . MAPS : <EOL> setattr ( self , { "<STR_LIT:(>" : "<STR_LIT>" , "<STR_LIT:)>" : "<STR_LIT>" } [ mode ] , <EOL> cs . MAPS [ code ] ) <EOL> def set_mode ( self , * modes , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> modes = [ mode << <NUM_LIT:5> for mode in modes ] <EOL> self . mode . update ( modes ) <EOL> if mo . DECCOLM in modes : <EOL> self . resize ( columns = <NUM_LIT> ) <EOL> self . erase_in_display ( <NUM_LIT:2> ) <EOL> self . cursor_position ( ) <EOL> if mo . DECOM in modes : <EOL> self . cursor_position ( ) <EOL> if mo . DECSCNM in modes : <EOL> self . buffer [ : ] = ( [ char . _replace ( reverse = True ) for char in line ] <EOL> for line in self . buffer ) <EOL> self . select_graphic_rendition ( g . _SGR [ "<STR_LIT>" ] ) <EOL> if mo . DECTCEM in modes : <EOL> self . cursor . hidden = False <EOL> def reset_mode ( self , * modes , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> modes = [ mode << <NUM_LIT:5> for mode in modes ] <EOL> self . mode . difference_update ( modes ) <EOL> if mo . DECCOLM in modes : <EOL> self . resize ( columns = <NUM_LIT> ) <EOL> self . erase_in_display ( <NUM_LIT:2> ) <EOL> self . cursor_position ( ) <EOL> if mo . DECOM in modes : <EOL> self . cursor_position ( ) <EOL> if mo . DECSCNM in modes : <EOL> self . buffer [ : ] = ( [ char . _replace ( reverse = False ) for char in line ] <EOL> for line in self . buffer ) <EOL> self . select_graphic_rendition ( g . _SGR [ "<STR_LIT>" ] ) <EOL> if mo . DECTCEM in modes : <EOL> self . cursor . hidden = True <EOL> def shift_in ( self ) : <EOL> """<STR_LIT>""" <EOL> self . charset = <NUM_LIT:0> <EOL> def shift_out ( self ) : <EOL> """<STR_LIT>""" <EOL> self . charset = <NUM_LIT:1> <EOL> def draw ( self , char ) : <EOL> """<STR_LIT>""" <EOL> char = char . translate ( [ self . g0_charset , <EOL> self . g1_charset ] [ self . charset ] ) <EOL> if self . cursor . x == self . columns : <EOL> if mo . DECAWM in self . mode : <EOL> self . carriage_return ( ) <EOL> self . linefeed ( ) <EOL> else : <EOL> self . cursor . x -= <NUM_LIT:1> <EOL> if mo . IRM in self . mode : <EOL> self . insert_characters ( <NUM_LIT:1> ) <EOL> self . buffer [ self . cursor . y ] [ self . cursor . x ] = self . cursor . attrs . _replace ( data = char ) <EOL> self . cursor . x += <NUM_LIT:1> <EOL> def carriage_return ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . x = <NUM_LIT:0> <EOL> def index ( self ) : <EOL> """<STR_LIT>""" <EOL> top , bottom = self . margins <EOL> if self . cursor . y == bottom : <EOL> self . buffer . pop ( top ) <EOL> self . buffer . insert ( bottom , take ( self . columns , self . default_line ) ) <EOL> else : <EOL> self . cursor_down ( ) <EOL> def reverse_index ( self ) : <EOL> """<STR_LIT>""" <EOL> top , bottom = self . margins <EOL> if self . cursor . y == top : <EOL> self . buffer . pop ( bottom ) <EOL> self . buffer . insert ( top , take ( self . columns , self . default_line ) ) <EOL> else : <EOL> self . cursor_up ( ) <EOL> def linefeed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index ( ) <EOL> if mo . LNM in self . mode : <EOL> self . carriage_return ( ) <EOL> self . ensure_bounds ( ) <EOL> def tab ( self ) : <EOL> """<STR_LIT>""" <EOL> for stop in sorted ( self . tabstops ) : <EOL> if self . cursor . x < stop : <EOL> column = stop <EOL> break <EOL> else : <EOL> column = self . columns - <NUM_LIT:1> <EOL> self . cursor . x = column <EOL> def backspace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cursor_back ( ) <EOL> def save_cursor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . savepoints . append ( Savepoint ( copy . copy ( self . cursor ) , <EOL> self . g0_charset , <EOL> self . g1_charset , <EOL> self . charset , <EOL> mo . DECOM in self . mode , <EOL> mo . DECAWM in self . mode ) ) <EOL> def restore_cursor ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . savepoints : <EOL> savepoint = self . savepoints . pop ( ) <EOL> self . g0_charset = savepoint . g0_charset <EOL> self . g1_charset = savepoint . g1_charset <EOL> self . charset = savepoint . charset <EOL> if savepoint . origin : <EOL> self . set_mode ( mo . DECOM ) <EOL> if savepoint . wrap : <EOL> self . set_mode ( mo . DECAWM ) <EOL> self . cursor = savepoint . cursor <EOL> self . ensure_bounds ( use_margins = True ) <EOL> else : <EOL> self . reset_mode ( mo . DECOM ) <EOL> self . cursor_position ( ) <EOL> def insert_lines ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> count = count or <NUM_LIT:1> <EOL> top , bottom = self . margins <EOL> if top <= self . cursor . y <= bottom : <EOL> for line in range ( self . cursor . y , <EOL> min ( bottom + <NUM_LIT:1> , self . cursor . y + count ) ) : <EOL> self . buffer . pop ( bottom ) <EOL> self . buffer . insert ( line , take ( self . columns , self . default_line ) ) <EOL> self . carriage_return ( ) <EOL> def delete_lines ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> count = count or <NUM_LIT:1> <EOL> top , bottom = self . margins <EOL> if top <= self . cursor . y <= bottom : <EOL> for _ in range ( min ( bottom - self . cursor . y + <NUM_LIT:1> , count ) ) : <EOL> self . buffer . pop ( self . cursor . y ) <EOL> self . buffer . insert ( bottom , list ( <EOL> repeat ( self . cursor . attrs , self . columns ) ) ) <EOL> self . carriage_return ( ) <EOL> def insert_characters ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> count = count or <NUM_LIT:1> <EOL> for _ in range ( min ( self . columns - self . cursor . y , count ) ) : <EOL> self . buffer [ self . cursor . y ] . insert ( self . cursor . x , self . cursor . attrs ) <EOL> self . buffer [ self . cursor . y ] . pop ( ) <EOL> def delete_characters ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> count = count or <NUM_LIT:1> <EOL> for _ in range ( min ( self . columns - self . cursor . x , count ) ) : <EOL> self . buffer [ self . cursor . y ] . pop ( self . cursor . x ) <EOL> self . buffer [ self . cursor . y ] . append ( self . cursor . attrs ) <EOL> def erase_characters ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> count = count or <NUM_LIT:1> <EOL> for column in range ( self . cursor . x , <EOL> min ( self . cursor . x + count , self . columns ) ) : <EOL> self . buffer [ self . cursor . y ] [ column ] = self . cursor . attrs <EOL> def erase_in_line ( self , type_of = <NUM_LIT:0> , private = False ) : <EOL> """<STR_LIT>""" <EOL> interval = ( <EOL> range ( self . cursor . x , self . columns ) , <EOL> range ( <NUM_LIT:0> , self . cursor . x + <NUM_LIT:1> ) , <EOL> range ( <NUM_LIT:0> , self . columns ) <EOL> ) [ type_of ] <EOL> for column in interval : <EOL> self . buffer [ self . cursor . y ] [ column ] = self . cursor . attrs <EOL> def erase_in_display ( self , type_of = <NUM_LIT:0> , private = False ) : <EOL> """<STR_LIT>""" <EOL> interval = ( <EOL> range ( self . cursor . y + <NUM_LIT:1> , self . lines ) , <EOL> range ( <NUM_LIT:0> , self . cursor . y ) , <EOL> range ( <NUM_LIT:0> , self . lines ) <EOL> ) [ type_of ] <EOL> for line in interval : <EOL> self . buffer [ line ] [ : ] = ( self . cursor . attrs for _ in range ( self . columns ) ) <EOL> if type_of in [ <NUM_LIT:0> , <NUM_LIT:1> ] : <EOL> self . erase_in_line ( type_of ) <EOL> def set_tab_stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tabstops . add ( self . cursor . x ) <EOL> def clear_tab_stop ( self , type_of = None ) : <EOL> """<STR_LIT>""" <EOL> if not type_of : <EOL> self . tabstops . discard ( self . cursor . x ) <EOL> elif type_of == <NUM_LIT:3> : <EOL> self . tabstops = set ( ) <EOL> def ensure_bounds ( self , use_margins = None ) : <EOL> """<STR_LIT>""" <EOL> if use_margins or mo . DECOM in self . mode : <EOL> top , bottom = self . margins <EOL> else : <EOL> top , bottom = <NUM_LIT:0> , self . lines - <NUM_LIT:1> <EOL> self . cursor . x = min ( max ( <NUM_LIT:0> , self . cursor . x ) , self . columns - <NUM_LIT:1> ) <EOL> self . cursor . y = min ( max ( top , self . cursor . y ) , bottom ) <EOL> def cursor_up ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . y -= count or <NUM_LIT:1> <EOL> self . ensure_bounds ( use_margins = True ) <EOL> def cursor_up1 ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor_up ( count ) <EOL> self . carriage_return ( ) <EOL> def cursor_down ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . y += count or <NUM_LIT:1> <EOL> self . ensure_bounds ( use_margins = True ) <EOL> def cursor_down1 ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor_down ( count ) <EOL> self . carriage_return ( ) <EOL> def cursor_back ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . x -= count or <NUM_LIT:1> <EOL> self . ensure_bounds ( ) <EOL> def cursor_forward ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . x += count or <NUM_LIT:1> <EOL> self . ensure_bounds ( ) <EOL> def cursor_position ( self , line = None , column = None ) : <EOL> """<STR_LIT>""" <EOL> column = ( column or <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> line = ( line or <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> if mo . DECOM in self . mode : <EOL> line += self . margins . top <EOL> if not self . margins . top <= line <= self . margins . bottom : <EOL> return <EOL> self . cursor . x , self . cursor . y = column , line <EOL> self . ensure_bounds ( ) <EOL> def cursor_to_column ( self , column = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . x = ( column or <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> self . ensure_bounds ( ) <EOL> def cursor_to_line ( self , line = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . y = ( line or <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> if mo . DECOM in self . mode : <EOL> self . cursor . y += self . margins . top <EOL> self . ensure_bounds ( ) <EOL> def bell ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> def alignment_display ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in self . buffer : <EOL> for column , char in enumerate ( line ) : <EOL> line [ column ] = char . _replace ( data = "<STR_LIT:E>" ) <EOL> def select_graphic_rendition ( self , * attrs ) : <EOL> """<STR_LIT>""" <EOL> replace = { } <EOL> for attr in attrs or [ <NUM_LIT:0> ] : <EOL> if attr in g . FG : <EOL> replace [ "<STR_LIT>" ] = g . FG [ attr ] <EOL> elif attr in g . BG : <EOL> replace [ "<STR_LIT>" ] = g . BG [ attr ] <EOL> elif attr in g . TEXT : <EOL> attr = g . TEXT [ attr ] <EOL> replace [ attr [ <NUM_LIT:1> : ] ] = attr . startswith ( "<STR_LIT:+>" ) <EOL> elif not attr : <EOL> replace = self . default_char . _asdict ( ) <EOL> self . cursor . attrs = self . cursor . attrs . _replace ( ** replace ) <EOL> class DiffScreen ( Screen ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> self . dirty = set ( ) <EOL> super ( DiffScreen , self ) . __init__ ( * args ) <EOL> def set_mode ( self , * modes , ** kwargs ) : <EOL> if mo . DECSCNM >> <NUM_LIT:5> in modes and kwargs . get ( "<STR_LIT>" ) : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . set_mode ( * modes , ** kwargs ) <EOL> def reset_mode ( self , * modes , ** kwargs ) : <EOL> if mo . DECSCNM >> <NUM_LIT:5> in modes and kwargs . get ( "<STR_LIT>" ) : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . reset_mode ( * modes , ** kwargs ) <EOL> def reset ( self ) : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . reset ( ) <EOL> def resize ( self , * args , ** kwargs ) : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . resize ( * args , ** kwargs ) <EOL> def draw ( self , * args ) : <EOL> super ( DiffScreen , self ) . draw ( * args ) <EOL> self . dirty . add ( self . cursor . y ) <EOL> def index ( self ) : <EOL> if self . cursor . y == self . margins . bottom : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . index ( ) <EOL> def reverse_index ( self ) : <EOL> if self . cursor . y == self . margins . top : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . reverse_index ( ) <EOL> def insert_lines ( self , * args ) : <EOL> self . dirty . update ( range ( self . cursor . y , self . lines ) ) <EOL> super ( DiffScreen , self ) . insert_lines ( * args ) <EOL> def delete_lines ( self , * args ) : <EOL> self . dirty . update ( range ( self . cursor . y , self . lines ) ) <EOL> super ( DiffScreen , self ) . delete_lines ( * args ) <EOL> def insert_characters ( self , * args ) : <EOL> self . dirty . add ( self . cursor . y ) <EOL> super ( DiffScreen , self ) . insert_characters ( * args ) <EOL> def delete_characters ( self , * args ) : <EOL> self . dirty . add ( self . cursor . y ) <EOL> super ( DiffScreen , self ) . delete_characters ( * args ) <EOL> def erase_characters ( self , * args ) : <EOL> self . dirty . add ( self . cursor . y ) <EOL> super ( DiffScreen , self ) . erase_characters ( * args ) <EOL> def erase_in_line ( self , * args ) : <EOL> self . dirty . add ( self . cursor . y ) <EOL> super ( DiffScreen , self ) . erase_in_line ( * args ) <EOL> def erase_in_display ( self , type_of = <NUM_LIT:0> ) : <EOL> self . dirty . update ( ( <EOL> range ( self . cursor . y + <NUM_LIT:1> , self . lines ) , <EOL> range ( <NUM_LIT:0> , self . cursor . y ) , <EOL> range ( <NUM_LIT:0> , self . lines ) <EOL> ) [ type_of ] ) <EOL> super ( DiffScreen , self ) . erase_in_display ( type_of ) <EOL> def alignment_display ( self ) : <EOL> self . dirty . update ( range ( self . lines ) ) <EOL> super ( DiffScreen , self ) . alignment_display ( ) <EOL> History = namedtuple ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> class HistoryScreen ( DiffScreen ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , columns , lines , history = <NUM_LIT:100> , ratio = <NUM_LIT> ) : <EOL> self . history = History ( deque ( maxlen = history // <NUM_LIT:2> ) , <EOL> deque ( maxlen = history ) , <EOL> float ( ratio ) , <EOL> history , <EOL> history ) <EOL> super ( HistoryScreen , self ) . __init__ ( columns , lines ) <EOL> def __before__ ( self , command ) : <EOL> """<STR_LIT>""" <EOL> if command not in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> while self . history . position < self . history . size : <EOL> self . next_page ( ) <EOL> super ( HistoryScreen , self ) . __before__ ( command ) <EOL> def __after__ ( self , command ) : <EOL> """<STR_LIT>""" <EOL> if command in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> for idx , line in enumerate ( self . buffer ) : <EOL> if len ( line ) > self . columns : <EOL> self . buffer [ idx ] = line [ : self . columns ] <EOL> elif len ( line ) < self . columns : <EOL> self . buffer [ idx ] = line + take ( self . columns - len ( line ) , <EOL> self . default_line ) <EOL> self . cursor . hidden = not ( <EOL> abs ( self . history . position - self . history . size ) < self . lines and <EOL> mo . DECTCEM in self . mode <EOL> ) <EOL> super ( HistoryScreen , self ) . __after__ ( command ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( HistoryScreen , self ) . reset ( ) <EOL> self . history . top . clear ( ) <EOL> self . history . bottom . clear ( ) <EOL> self . history = self . history . _replace ( position = self . history . size ) <EOL> def index ( self ) : <EOL> """<STR_LIT>""" <EOL> top , bottom = self . margins <EOL> if self . cursor . y == bottom : <EOL> self . history . top . append ( self . buffer [ top ] ) <EOL> super ( HistoryScreen , self ) . index ( ) <EOL> def reverse_index ( self ) : <EOL> """<STR_LIT>""" <EOL> top , bottom = self . margins <EOL> if self . cursor . y == top : <EOL> self . history . bottom . append ( self . buffer [ bottom ] ) <EOL> super ( HistoryScreen , self ) . reverse_index ( ) <EOL> def prev_page ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . history . position > self . lines and self . history . top : <EOL> mid = min ( len ( self . history . top ) , <EOL> int ( math . ceil ( self . lines * self . history . ratio ) ) ) <EOL> self . history . bottom . extendleft ( reversed ( self . buffer [ - mid : ] ) ) <EOL> self . history = self . history . _replace ( position = self . history . position - self . lines ) <EOL> self . buffer [ : ] = list ( reversed ( [ <EOL> self . history . top . pop ( ) for _ in range ( mid ) <EOL> ] ) ) + self . buffer [ : - mid ] <EOL> self . dirty = set ( range ( self . lines ) ) <EOL> def next_page ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . history . position < self . history . size and self . history . bottom : <EOL> mid = min ( len ( self . history . bottom ) , <EOL> int ( math . ceil ( self . lines * self . history . ratio ) ) ) <EOL> self . history . top . extend ( self . buffer [ : mid ] ) <EOL> self . history = self . history . _replace ( position = self . history . position + self . lines ) <EOL> self . buffer [ : ] = self . buffer [ mid : ] + [ <EOL> self . history . bottom . popleft ( ) for _ in range ( mid ) <EOL> ] <EOL> self . dirty = set ( range ( self . lines ) ) </s>
