1010from typing import (
1111 Callable ,
1212 Optional ,
13- Union ,
13+ Union , List , Iterable , overload ,
1414)
1515
1616import attr
2323)
2424
2525
26- @attr .s (frozen = True )
26+ @attr .s (auto_attribs = True , frozen = True )
2727class HistoryItem :
2828 """Class used to represent one command in the history list"""
2929
3030 _listformat = ' {:>4} {}'
3131 _ex_listformat = ' {:>4}x {}'
3232
33- statement = attr .ib (default = None , validator = attr .validators .instance_of (Statement ))
33+ statement : Statement = attr .ib (default = None , validator = attr .validators .instance_of (Statement ))
3434
35- def __str__ (self ):
35+ def __str__ (self ) -> str :
3636 """A convenient human readable representation of the history item"""
3737 return self .statement .raw
3838
@@ -66,11 +66,11 @@ def pr(self, idx: int, script: bool = False, expanded: bool = False, verbose: bo
6666 """
6767 if verbose :
6868 raw = self .raw .rstrip ()
69- expanded = self .expanded
69+ expanded_command = self .expanded
7070
7171 ret_str = self ._listformat .format (idx , raw )
72- if raw != expanded :
73- ret_str += '\n ' + self ._ex_listformat .format (idx , expanded )
72+ if raw != expanded_command :
73+ ret_str += '\n ' + self ._ex_listformat .format (idx , expanded_command )
7474 else :
7575 if expanded :
7676 ret_str = self .expanded
@@ -92,7 +92,7 @@ def pr(self, idx: int, script: bool = False, expanded: bool = False, verbose: bo
9292 return ret_str
9393
9494
95- class History (list ):
95+ class History (List [ HistoryItem ] ):
9696 """A list of :class:`~cmd2.history.HistoryItem` objects with additional methods
9797 for searching and managing the list.
9898
@@ -106,8 +106,8 @@ class History(list):
106106 class to gain access to the historical record.
107107 """
108108
109- def __init__ (self , seq = ()) -> None :
110- super ().__init__ (seq )
109+ def __init__ (self , seq : Iterable [ HistoryItem ] = ()) -> None :
110+ super (History , self ).__init__ (seq )
111111 self .session_start_index = 0
112112
113113 def start_session (self ) -> None :
@@ -122,14 +122,25 @@ def _zero_based_index(self, onebased: Union[int, str]) -> int:
122122 result -= 1
123123 return result
124124
125+ @overload
126+ def append (self , new : HistoryItem ) -> None :
127+ ...
128+
129+ @overload
125130 def append (self , new : Statement ) -> None :
131+ ...
132+
133+ def append (self , new : Union [Statement , HistoryItem ]) -> None :
126134 """Append a new statement to the end of the History list.
127135
128136 :param new: Statement object which will be composed into a HistoryItem
129137 and added to the end of the list
130138 """
131- history_item = HistoryItem (new )
132- super ().append (history_item )
139+ if isinstance (new , Statement ):
140+ history_item = HistoryItem (new )
141+ else :
142+ history_item = new
143+ super (History , self ).append (history_item )
133144
134145 def clear (self ) -> None :
135146 """Remove all items from the History list."""
@@ -206,17 +217,17 @@ def span(self, span: str, include_persisted: bool = False) -> 'OrderedDict[int,
206217 # our regex doesn't match the input, bail out
207218 raise ValueError ('History indices must be positive or negative integers, and may not be zero.' )
208219
209- start = results .group ('start' )
210- if start :
211- start = min (self ._zero_based_index (start ), len (self ) - 1 )
220+ start_token = results .group ('start' )
221+ if start_token :
222+ start = min (self ._zero_based_index (start_token ), len (self ) - 1 )
212223 if start < 0 :
213224 start = max (0 , len (self ) + start )
214225 else :
215226 start = 0 if include_persisted else self .session_start_index
216227
217- end = results .group ('end' )
218- if end :
219- end = min (int (end ), len (self ))
228+ end_token = results .group ('end' )
229+ if end_token :
230+ end = min (int (end_token ), len (self ))
220231 if end < 0 :
221232 end = max (0 , len (self ) + end + 1 )
222233 else :
0 commit comments