comparison cmd2.py @ 308:4e9011c3f732

tightening up history.search
author catherine@bothari
date Thu, 28 Jan 2010 08:52:04 -0500
parents 43273a4b517c
children 1941e54cb776
comparison
equal deleted inserted replaced
307:43273a4b517c 308:4e9011c3f732
945 """list [arg]: lists last command issued 945 """list [arg]: lists last command issued
946 946
947 no arg -> list most recent command 947 no arg -> list most recent command
948 arg is integer -> list one history item, by index 948 arg is integer -> list one history item, by index
949 a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end) 949 a..b, a:b, a:, ..b -> list spans from a (or start) to b (or end)
950 arg is string -> list last command matching string search 950 arg is string -> list all commands matching string search
951 arg is /enclosed in forward-slashes/ -> regular expression search 951 arg is /enclosed in forward-slashes/ -> regular expression search
952 """ 952 """
953 #TODO: totally failing to recognize args 953 try:
954 try: 954 history = self.history.span(arg or '-1')
955 #self.stdout.write(self.last_matching(arg).pr()) 955 except IndexError:
956 for hi in self.history.span(arg or '-1'): 956 history = self.history.search(arg)
957 self.poutput(hi.pr()) 957 for hi in history:
958 except: 958 self.poutput(hi.pr())
959 pass 959
960 do_hi = do_history 960 do_hi = do_history
961 do_l = do_list 961 do_l = do_list
962 do_li = do_list 962 do_li = do_list
963 963
964 def do_ed(self, arg): 964 def do_ed(self, arg):
1125 ['second', 'third', 'fourth'] 1125 ['second', 'third', 'fourth']
1126 >>> h.span('-1') 1126 >>> h.span('-1')
1127 ['fourth'] 1127 ['fourth']
1128 >>> h.span('-2..-3') 1128 >>> h.span('-2..-3')
1129 ['third', 'second'] 1129 ['third', 'second']
1130 >>> h.search('o')
1131 ['second', 'fourth']
1132 >>> h.search('/IR/')
1133 ['first', 'third']
1130 ''' 1134 '''
1131 def find(self, target):
1132
1133 def zero_based_index(self, onebased): 1135 def zero_based_index(self, onebased):
1134 result = onebased 1136 result = onebased
1135 if result > 0: 1137 if result > 0:
1136 result -= 1 1138 result -= 1
1137 return result 1139 return result
1139 if raw: 1141 if raw:
1140 result = self.zero_based_index(int(raw)) 1142 result = self.zero_based_index(int(raw))
1141 else: 1143 else:
1142 result = None 1144 result = None
1143 return result 1145 return result
1146 def search(self, target):
1147 target = target.strip()
1148 if target[0] == target[-1] == '/' and len(target) > 1:
1149 target = target[1:-1]
1150 else:
1151 target = re.escape(target)
1152 pattern = re.compile(target, re.IGNORECASE)
1153 return [s for s in self if pattern.search(s)]
1144 spanpattern = re.compile(r'^\s*(?P<start>\-?\d+)?\s*(?P<separator>:|(\.{2,}))?\s*(?P<end>\-?\d+)?\s*$') 1154 spanpattern = re.compile(r'^\s*(?P<start>\-?\d+)?\s*(?P<separator>:|(\.{2,}))?\s*(?P<end>\-?\d+)?\s*$')
1145 def span(self, raw): 1155 def span(self, raw):
1156 if raw.lower() in ('*', '-', 'all'):
1157 raw = ':'
1146 results = self.spanpattern.search(raw) 1158 results = self.spanpattern.search(raw)
1147 if not results: 1159 if not results:
1148 raise IndexError 1160 raise IndexError
1149 if not results.group('separator'): 1161 if not results.group('separator'):
1150 return [self[self.to_index(results.group('start'))]] 1162 return [self[self.to_index(results.group('start'))]]