-
Notifications
You must be signed in to change notification settings - Fork 4
/
rgg
executable file
·364 lines (314 loc) · 8.94 KB
/
rgg
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
import os
import sys
import math
import glob
import subprocess
import argparse
from string import Template
from collections import defaultdict
import shlex
import json
import base64
from urllib.parse import urlencode
import signal
# The RGV_EDITOR environmental variable
# default: $EDITOR, then 'vi'
# suggested:
# RGV_EDITOR='vv $file:$line:$col'
# RGV_EDITOR='vim +$line $file'
# escape: '$$' (but shell may recognize it!)
# unknown -> ''
def get_editor():
editor = os.environ.get('RGV_EDITOR')
if not editor:
editor = os.environ.get('AGV_EDITOR', os.environ.get('EDITOR', 'vi'))
if editor in ('vi', 'vim'):
editor += ' +$line'
if '$file' not in editor:
editor += ' $file'
editor = Template(editor)
return editor
if 'XDG_RUNTIME_DIR' in os.environ:
logfile_base = '%s/.rgg.log' % os.environ['XDG_RUNTIME_DIR']
else:
logfile_dir = '/%s/%d' % (
os.environ.get('TMPDIR', '/tmp'), os.getuid())
if not os.path.isdir(logfile_dir):
os.mkdir(logfile_dir)
logfile_base = logfile_dir + '/.rgg.log'
DISPLAY_TYPES = ['context', 'match']
def get_logfile_by_tty() -> str:
try:
ttyname = os.ttyname(sys.stderr.fileno()).replace('/', '_')
except OSError:
try:
ttyname = os.ttyname(sys.stdout.fileno()).replace('/', '_')
except OSError:
ttyname = os.ttyname(sys.stdin.fileno()).replace('/', '_')
return logfile_base + '.' + ttyname
def get_logfile(index):
'''
index == 0: try to choose one depending on ttyname
index > 0: file index after sorting
index < 0: return all files
'''
files = None
if index <= 0:
try_file = get_logfile_by_tty()
if os.path.exists(try_file):
if index == 0:
return try_file
else:
files = [try_file]
if files:
files += [x for x in glob.glob(logfile_base + '*') if x != files[0]]
else:
files = glob.glob(logfile_base + '*')
files.sort(reverse=True, key=lambda f: os.stat(f).st_mtime)
if index > 0:
return files[index-1]
elif index == 0:
return files[0]
else:
return files
def search():
try:
import setproctitle
setproctitle.setproctitle('rgg')
except ImportError:
pass
cmd = ['rg', '--hidden', '-g', '!.git', '--json'] + sys.argv[1:]
import ctypes
try:
ctypes.CDLL('libopen_noatime.so')
env = os.environ.copy()
env['LD_PRELOAD'] = 'libopen_noatime.so'
except OSError:
env = None
logfile = get_logfile_by_tty()
searcher = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, text=True)
cwd = os.environ.get('PWD', os.getcwd())
isatty = sys.stdout.isatty()
display = Display(cwd, isatty)
if isatty:
# less handles it, we wait less
signal.signal(signal.SIGINT, signal.SIG_IGN)
with open(logfile, 'w') as f:
print(cwd, file=f)
print(subprocess.list2cmdline(sys.argv[1:]), file=f)
for line in searcher.stdout:
f.write(line)
display.feed(line)
searcher.wait()
sys.exit(searcher.returncode)
def decode(data) -> str:
try:
s = data['text']
except KeyError:
s = base64.b64decode(data['bytes']).decode(errors='replace')
return s
def chomp(s: str) -> str:
if s.endswith('\n'):
return s[:-1]
else:
return s
def colored(s: str, c: int) -> str:
return f'\x1b[38;5;{c}m{s}\x1b[0m'
def linked(s: str, abspath: str, line: int, col: int) -> str:
url = 'vv://?' + urlencode({
'path': abspath,
'line': line,
'col': col,
})
return f'\x1b]8;;{url}\x1b\\{s}\x1b]8;;\x1b\\'
class Display:
def __init__(self, basepath: str, isatty: bool) -> None:
if isatty:
if pager := os.environ.get('PAGER'):
pager_cmd = shlex.split(pager)
else:
pager_cmd = ['less', '-RFXM']
self.pager = subprocess.Popen(pager_cmd, stdin=subprocess.PIPE, text=True)
output = self.pager.stdin
else:
self.pager = None
output = sys.stdout
self.output = output
self.counter = 0
self.broken = False
self.basepath = basepath
if isatty:
self.printer = self.nice_printer
else:
self.printer = self.plain_printer
def __del__(self) -> None:
if self.pager:
try:
self.pager.stdin.close()
except BrokenPipeError:
pass
self.pager.wait()
def feed(self, line: str) -> bool:
if self.broken:
return False
j = json.loads(line)
try:
self.printer(j)
return True
except BrokenPipeError:
self.broken = True
return False
def nice_printer(self, j) -> None:
type = j['type']
if type not in DISPLAY_TYPES:
return
if type == 'context':
sep = '-'
else:
sep = ':'
self.counter += 1
print(f'{self.counter:5} ', end='', file=self.output)
data = j['data']
path = decode(data['path'])
abspath = os.path.join(self.basepath, path)
path = colored(path, 5)
text = chomp(decode(data['lines']))
line_number = colored(data['line_number'], 2)
if ms := data['submatches']:
column = colored(ms[0]['start'] + 1, 2)
colored_text = []
try:
textb = data['lines']['text'].encode('utf-8')
except KeyError:
textb = base64.b64decode(data['lines']['bytes'])
last = 0
for m in ms:
start = m['start']
end = m['end']
colored_text.append(textb[last:start])
colored_text.append(
colored(
linked(
textb[start:end].decode(errors='replace'),
abspath, data['line_number'], start + 1),
9,
).encode())
last = end
colored_text.append(textb[end:])
text = b''.join(colored_text).decode(errors='replace')
print(path, line_number, column, chomp(text), sep=sep, file=self.output)
else:
print(path, line_number, text, sep=sep, file=self.output)
def plain_printer(self, j) -> None:
type = j['type']
if type not in DISPLAY_TYPES:
return
if type == 'context':
sep = '-'
else:
sep = ':'
self.counter += 1
print(f'{self.counter:5} ', end='', file=self.output)
data = j['data']
path = decode(data['path'])
text = chomp(decode(data['lines']))
if ms := data['submatches']:
column = ms[0]['start'] + 1
print(path, data['line_number'], column, text, sep=sep, file=self.output)
else:
print(path, data['line_number'], text, sep=sep, file=self.output)
def edit(data):
path = decode(data['path'])
line = data['line_number']
if ms := data['submatches']:
col = ms[0]['start'] + 1
else:
col = 1
info = defaultdict(str)
info.update({
'file': path,
'line': line,
'col': col,
})
print('File: %(file)s, Line %(line)s, Col %(col)s.' % info)
cmd = get_editor().substitute(info)
os.system(cmd)
def edit_entry(n, logfile):
try:
with open(logfile, encoding='utf-8') as f:
os.chdir(f.readline()[:-1])
f.readline() # args
i = 0
for l in f:
j = json.loads(l)
if j['type'] not in DISPLAY_TYPES:
continue
i += 1
if i == n:
edit(j['data'])
break
else:
sys.exit('out of index.')
except IOError as e:
print('Error opening logfile %s: %s' % (logfile, e), file=sys.stderr)
sys.exit(2)
def cat(logfile: str) -> None:
with open(logfile) as f:
basedir = f.readline()[:-1]
f.readline() # args
isatty = sys.stdout.isatty()
display = Display(basedir, isatty)
for l in f:
if not display.feed(l):
return
def list_logfiles(logfiles):
args = []
dirs = []
for log in logfiles:
with open(log, errors='replace') as f:
dirs.append(f.readline()[:-1])
args.append(f.readline()[:-1])
fmt = '%%%dd: %%-%ds (dir: %%s)' % (
math.ceil(math.log10(len(logfiles))),
min(max(len(x) for x in args), 40),
)
for i in range(len(args)):
print(fmt % (i+1, args[i], dirs[i]))
def view():
try:
import setproctitle
setproctitle.setproctitle('rgv')
except ImportError:
pass
parser = argparse.ArgumentParser(description='view and go to rgg results')
parser.add_argument('indices', metavar='N', type=int, nargs='*',
help='index of result entry to go to')
parser.add_argument('-l', action='store_true', default=False,
help='list results (same without args)')
parser.add_argument('-L', '--list', action='store_true', default=False,
help='list all log files')
parser.add_argument('-f', '--file', type=int, default=0, metavar='NUMBER',
help='explicitly select a log file')
args = parser.parse_args()
if args.list:
logfiles = get_logfile(-1)
if not logfiles:
sys.exit('no rgg log files')
list_logfiles(logfiles)
else:
try:
logfile = get_logfile(args.file)
except IndexError:
sys.exit('no rgg log files')
if args.indices:
for index in args.indices:
edit_entry(index, logfile)
else:
cat(logfile)
if __name__ == '__main__':
progname = os.path.basename(sys.argv[0])
if progname in ['rgv', 'agv']:
view()
else:
search()