77
88from typing import List , Union
99
10+ import attr
11+
1012from . import utils
1113from .parsing import Statement
1214
13-
15+ @ attr . s ( frozen = True )
1416class HistoryItem ():
1517 """Class used to represent one command in the History list"""
1618 _listformat = ' {:>4} {}\n '
1719 _ex_listformat = ' {:>4}x {}\n '
1820
19- # def __new__(cls, statement: Statement):
20- # """Create a new instance of HistoryItem
21+ statement = attr . ib ( default = None , validator = attr . validators . instance_of ( Statement ))
22+ idx = attr . ib ( default = None , validator = attr . validators . instance_of ( int ))
2123
22- # We must override __new__ because we are subclassing `str` which is
23- # immutable and takes a different number of arguments as Statement.
24- # """
25- # hi = super().__new__(cls, statement.raw)
26- # hi.statement = statement
27- # hi.idx = None
28- # return hi
24+ def __str__ (self ):
25+ """A convenient human readable representation of the history item"""
26+ if self .statement :
27+ return self .statement .raw
28+ else :
29+ return ''
2930
30- def __init__ (self , statement : Statement , idx : int ):
31- self .statement = statement
32- self .idx = idx
31+ @property
32+ def raw (self ) -> str :
33+ """Return the raw input from the user for this item"""
34+ return self .statement .raw
3335
3436 @property
3537 def expanded (self ) -> str :
@@ -43,24 +45,23 @@ def pr(self, script=False, expanded=False, verbose=False) -> str:
4345
4446 :return: pretty print string version of a HistoryItem
4547 """
46- expanded_command = self .statement .expanded_command_line
4748 if verbose :
48- ret_str = self ._listformat .format (self .idx , self .statement . raw )
49- if self .statement . raw != expanded_command .rstrip ():
50- ret_str += self ._ex_listformat .format (self .idx , expanded_command )
49+ ret_str = self ._listformat .format (self .idx , self .raw )
50+ if self .raw != self . expanded .rstrip ():
51+ ret_str += self ._ex_listformat .format (self .idx , self . expanded )
5152 else :
5253 if script :
5354 # display without entry numbers
5455 if expanded or self .statement .multiline_command :
55- ret_str = expanded_command .rstrip ()
56+ ret_str = self . expanded .rstrip ()
5657 else :
57- ret_str = self .statement . raw
58+ ret_str = self .raw . rstrip ()
5859 else :
5960 # display a numbered list
6061 if expanded or self .statement .multiline_command :
61- ret_str = self ._listformat .format (self .idx , expanded_command .rstrip ())
62+ ret_str = self ._listformat .format (self .idx , self . expanded .rstrip ())
6263 else :
63- ret_str = self ._listformat .format (self .idx , self .statement . raw .rstrip ())
64+ ret_str = self ._listformat .format (self .idx , self .raw .rstrip ())
6465 return ret_str
6566
6667
@@ -210,8 +211,8 @@ def str_search(self, search: str) -> List[HistoryItem]:
210211 def isin (history_item ):
211212 """filter function for string search of history"""
212213 sloppy = utils .norm_fold (search )
213- inraw = sloppy in utils .norm_fold (history_item .statement . raw )
214- inexpanded = sloppy in utils .norm_fold (history_item .statement . expanded_command_line )
214+ inraw = sloppy in utils .norm_fold (history_item .raw )
215+ inexpanded = sloppy in utils .norm_fold (history_item .expanded )
215216 return inraw or inexpanded
216217 return [item for item in self if isin (item )]
217218
@@ -228,7 +229,7 @@ def regex_search(self, regex: str) -> List[HistoryItem]:
228229
229230 def isin (hi ):
230231 """filter function for doing a regular expression search of history"""
231- return finder .search (hi .statement . raw ) or finder .search (hi .statement . expanded_command_line )
232+ return finder .search (hi .raw ) or finder .search (hi .expanded )
232233 return [itm for itm in self if isin (itm )]
233234
234235 def truncate (self , max_length : int ) -> None :
0 commit comments