-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist_plugin.py
184 lines (160 loc) · 4.84 KB
/
hist_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''history:
Prints recent command history
usage: history
history substitution supported as
![event][:word]
allowable event
! last command
N execute line N
-N execute line N ago
string execute line starting with string
?string? execute line containing string
word modifiers
^ first argument,( not including command)
$ last argument
* all args (not including command)
x where x is number: argument x. 0 is cmd
x-y args x through y. can omit x or y, default to 0,$ respectively.
'''
alias=['history']
from collections import deque
from ... tools.toolbox import bash
import re
import sys
class BashHistory(deque):
def __init__(self,HISTSIZE=500):
self.HISTSIZE = HISTSIZE
deque.__init__(self, maxlen=HISTSIZE)
def history(self,N=15):
"""show last N items"""
for i,s in zip(range(len(list(self))),list(self))[-N:]:
print i,':',' '.join(s)
return ''
# try:
# i=int(raw_input(':'))
# except ValueError:
# i=-1
# try:
# print i, self[i]
# return ' '.join(self[i].replace(' ',"' '"))
# except:
# return '\n'
def history_popup(self,N=15):
try:
import ui
except ImportError:
return self.history(N)
v=ui.TableView()
v.data_source=ui.ListDataSource( [' '.join([s.replace(' ',"' '") for s in x]) for x in list(self)[-N:]])
v.delegate=v.data_source
v.height=v.row_height*N+10
v.width=400
v.data_source.autoresizing=True
def _rowselected(sender):
sender.tableview.close()
v.delegate.action= _rowselected
v.present('popover')
v.wait_modal()
print v.selected_row[1]+len(self)-min(len(self),N), ':',v.data_source.items[v.selected_row[1]]
try:
return v.data_source.items[v.selected_row[1]]
except:
return ''
def history_replace(self, line):
"""expand each occurance of ![event][:word]
===TODO not yet implemented===
^STRING^string replace STRING with string
:p print and add to history, but dont execute
"""
# match inital bang, then one event identifier,
# either !, [-]N, string, or ?string?
linepat=(r'!((?P<linebang>!)' #!
'|(?P<linenum>-?\d+)' #N, or -N
'|(?P<stringstart>\w+)' #str
'|\?(?P<stringsearch>\w+)\?)') #?str?
# for word modifier, match starting :, then either special char, or range
wordpat=('(:('
'(?P<wordchar>[\^\*%$])' # ^ or * or $
'|(?P<wordrange>\d*-?\d*)' # x-y, x , x-, or -y
'))?')
pat=linepat+wordpat
import pdb
for m in re.finditer(pat,line):
#pdb.set_trace()
line=self._process_match(line,m)
return line
def _select_line(self,m):
'''returns line number of selected history substitution'''
g=m.groupdict()
#print g
if g.get('linebang'):
histline=-1 # !! == !-1
elif g.get('linenum'):
histline=int(g['linenum']) #!n, or !-n
elif g.get('stringstart'):
histline=[-i-1 for i,v in enumerate(reversed([' '.join(s) for s in list(self)])) if v.startswith(g['stringstart'])][0]
elif g.get('stringsearch'):
histline=[-i-1 for i,v in enumerate(reversed([' '.join(s) for s in list(self)])) if v.find(g['stringsearch'])][0]
else:
histline=None
return histline
def _select_args(self,histline, m):
lineargs = self[histline]
g=m.groupdict()
if g['wordchar']:
if g['wordchar'][0]==r'^':
return [lineargs[1]]
elif g['wordchar'][0]=='*':
return [lineargs[1:]]
elif g['wordchar'][0]=='$':
return [lineargs[-1]]
elif g['wordrange']: #x, x-y, x-, -y
if g['wordrange'].find('-')>=0:
start,end=g['wordrange'].split('-')
if (start == ''):
start=0
if (end == ''):
end=len(lineargs)
if (end=='$'):
end=len(lineargs)
return lineargs[int(start):int(end)+1]
else:
return [lineargs[int(g['wordrange'])]]
else: # no range specified.. entire line
return lineargs
def _process_match(self,line,m):
"""replace each history expression in line with expanded form """
# self contains parsed lists, i.e [cmd,arg1,arg2]
if not any(m.groupdict().itervalues()):
return line
else:
lineidx=self._select_line(m)
if lineidx:
lineargs=self._select_args(lineidx, m)
try:
hist= lineargs
return re.sub(re.escape(m.group(0)),' '.join(hist),line ) #replace portion of line with expansion
except:
return line
#TODO: consider storing history file, and loading previous history
_bashhistory=BashHistory()
def main(self, line):
"""print history"""
args = bash( line)
print len(args)
if args is None:
return
elif len(args)>0:
_bashhistory.history()
else:
_bashhistory.history_popup()
# intercept precmd, replace with hist subst
def precmd(self,line):
line= _bashhistory.history_replace(line)
return line
# store completed line after bashing.
# todo: consider doing this is precmd instead
def postcmd(self,stop,line):
_bashhistory.append(bash(line))
precmdhook=[precmd]
postcmdhook=[postcmd]