comparison cmd2.py @ 307:43273a4b517c

throw error from .span'
author catherine@bothari
date Thu, 28 Jan 2010 07:55:34 -0500
parents 1802f8c75265
children 4e9011c3f732
comparison
equal deleted inserted replaced
306:1802f8c75265 307:43273a4b517c
942 except IndexError: 942 except IndexError:
943 return None 943 return None
944 def do_list(self, arg): 944 def do_list(self, arg):
945 """list [arg]: lists last command issued 945 """list [arg]: lists last command issued
946 946
947 no arg -> list absolute last 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 - arg, arg - (integer) -> list up to or after #arg 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 last command 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 #TODO: totally failing to recognize args
954 try: 954 try:
955 #self.stdout.write(self.last_matching(arg).pr()) 955 #self.stdout.write(self.last_matching(arg).pr())
956 for hi in self.history.get(arg or -1): 956 for hi in self.history.span(arg or '-1'):
957 self.poutput(hi.pr()) 957 self.poutput(hi.pr())
958 except: 958 except:
959 pass 959 pass
960 do_hi = do_history 960 do_hi = do_history
961 do_l = do_list 961 do_l = do_list
1106 def __init__(self, instr): 1106 def __init__(self, instr):
1107 str.__init__(self) 1107 str.__init__(self)
1108 self.lowercase = self.lower() 1108 self.lowercase = self.lower()
1109 self.idx = None 1109 self.idx = None
1110 def pr(self): 1110 def pr(self):
1111 return listformat % (self.idx, str(self)) 1111 return self.listformat % (self.idx, str(self))
1112 1112
1113 class History(list): 1113 class History(list):
1114 '''A list of HistoryItems that knows how to respond to user requests. 1114 '''A list of HistoryItems that knows how to respond to user requests.
1115 >>> h = History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')]) 1115 >>> h = History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')])
1116 >>> h.span('-2..') 1116 >>> h.span('-2..')
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 ''' 1130 '''
1131 def find(self, target):
1132
1131 def zero_based_index(self, onebased): 1133 def zero_based_index(self, onebased):
1132 result = onebased 1134 result = onebased
1133 if result > 0: 1135 if result > 0:
1134 result -= 1 1136 result -= 1
1135 return result 1137 return result
1140 result = None 1142 result = None
1141 return result 1143 return result
1142 spanpattern = re.compile(r'^\s*(?P<start>\-?\d+)?\s*(?P<separator>:|(\.{2,}))?\s*(?P<end>\-?\d+)?\s*$') 1144 spanpattern = re.compile(r'^\s*(?P<start>\-?\d+)?\s*(?P<separator>:|(\.{2,}))?\s*(?P<end>\-?\d+)?\s*$')
1143 def span(self, raw): 1145 def span(self, raw):
1144 results = self.spanpattern.search(raw) 1146 results = self.spanpattern.search(raw)
1147 if not results:
1148 raise IndexError
1145 if not results.group('separator'): 1149 if not results.group('separator'):
1146 return [self[self.to_index(results.group('start'))]] 1150 return [self[self.to_index(results.group('start'))]]
1147 start = self.to_index(results.group('start')) 1151 start = self.to_index(results.group('start'))
1148 end = self.to_index(results.group('end')) 1152 end = self.to_index(results.group('end'))
1149 reverse = False 1153 reverse = False