-
Notifications
You must be signed in to change notification settings - Fork 0
/
explain.py
369 lines (292 loc) · 10.5 KB
/
explain.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
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
365
366
367
368
369
"""
Sketch of https://explainshell.com/ like output, in plain text
$ python explain.py
git diff-tree -M -r --name-status <commit>
└───┬───┘ ├┘ ├┘ └─────┬─────┘ └──┬───┘
┌───────┘ │ │ │ │
│┌────────────┘ │ │ │
││┌──────────────┘ │ │
│││┌──────────────────────┘ │
││││┌────────────────────────────────┘
└┼┼┼┼─ Compares the content and mode of blobs found via two tree objects
└┼┼┼─ Detect renames
└┼┼─ recurse into subtrees
└┼─ Show only names and status of changed files
│ for example:
│ M foo.py
└─ show differences between this commit and preceding one
NB: if you're using a good font like `Menlo` with support for Unicode box-drawing
characters, you can pass `usePens=True` to alternate each line bold/normal to
make it easier to read.
(With many fonts, the bold characters default to variable width.)
"""
import itertools
import math
from itertools import (accumulate, chain, repeat, cycle, groupby, zip_longest)
from functools import reduce
from textwrap import (wrap, dedent)
from operator import itemgetter
import string
import re
'''
Take a list of descriptions, which are either a literal string or a tuple of
string + description, and return a list of tuples of
(string, desc, length, offset)
e.g.
["ls ",
("*.py", "wildcard search for files that end with `.py`")]
would become:
[("ls ", None, 3, 0),
("*.py", "wildcard search for files that end with `.py`", 4, 3)]
'''
def normalise_tokens(tokens):
def norm(token):
(t,c) = token if type(token) is tuple else (token, None)
l = len(t)
# return tuple of token, comment, length, and a dummy offset of 0
return (t,c,l,0)
def offset(a, b):
(_, _, alength, aoffset) = a
return (*b[0:3], aoffset + alength)
hasComment = itemgetter(1)
ntokens = [norm(desc) for desc in descs]
header = ''.join([ntoken[0] for ntoken in ntokens])
lanes = list(filter(hasComment,
accumulate(ntokens, offset)))
return (header, lanes)
boxes = {
'ew': '─', 'EW': '━', 'ns': '│', 'NS': '┃', 'se': '┌',
'sE': '┍', 'Se': '┎', 'SE': '┏', 'sw': '┐', 'sW': '┑',
'Sw': '┒', 'SW': '┓', 'ne': '└', 'nE': '┕', 'Ne': '┖',
'NE': '┗', 'nw': '┘', 'nW': '┙', 'Nw': '┚', 'NW': '┛',
'nse': '├', 'nsE': '┝', 'Nse': '┞', 'nSe': '┟', 'NSe': '┠',
'NsE': '┡', 'nSE': '┢', 'NSE': '┣', 'nsw': '┤', 'nsW': '┥',
'Nsw': '┦', 'nSw': '┧', 'NSw': '┨', 'NsW': '┩', 'nSW': '┪',
'NSW': '┫', 'sew': '┬', 'seW': '┭', 'sEw': '┮', 'sEW': '┯',
'Sew': '┰', 'SeW': '┱', 'SEw': '┲', 'SEW': '┳', 'new': '┴',
'neW': '┵', 'nEw': '┶', 'nEW': '┷', 'New': '┸', 'NeW': '┹',
'NEw': '┺', 'NEW': '┻', 'nsew': '┼', 'nseW': '┽', 'nsEw': '┾',
'nsEW': '┿', 'Nsew': '╀', 'nSew': '╁', 'NSew': '╂', 'NseW': '╃',
'NsEw': '╄', 'nSeW': '╅', 'nSEw': '╆', 'NsEW': '╇', 'nSEW': '╈',
'NSeW': '╉', 'NSEw': '╊', 'NSEW': '╋', 'w': '╴', 'n': '╵',
'e': '╶', 's': '╷', 'W': '╸', 'N': '╹', 'E': '╺',
'S': '╻', 'Ew': '╼', 'nS': '╽', 'eW': '╾', 'Ns': '╿'}
box2recipe = {v: k for (k,v) in boxes.items() }
def overlayc(a,b):
# anything beats a space
if a == ' ':
return b
elif b == ' ':
return a
# anything that isn't a box character wins
ar = box2recipe.get(a, None)
br = box2recipe.get(b, None)
if not ar:
return a
elif not br:
return b
# otherwise we have two box recipes! Compose them
return box(ar, br)
def overlay2(aa,bb):
return ''.join([overlayc(a, b)
for (a,b)
in zip_longest(aa,bb, fillvalue=' ')])
def overlay(items):
return reduce(overlay2, items, '')
def box(*items):
def norm(item):
if type(item) is tuple:
(dirs, case) = item
return dirs.upper() if case else dirs.lower()
else:
return item
dirs = ''.join([norm(item) for item in items])
def aux(a,b):
return {**a,
b.lower(): b}
rose = reduce(aux, dirs, {})
dir = ''.join([rose.get(d, '')
for d in 'nsew'])
return boxes.get(dir, ' ')
def flatten(lol):
return sum(lol, [])
def reflow(text, width):
return flatten([wrap(para, width)
for para
in text.split("\n")])
descs = ['git',
' ',
('diff-tree', 'Compares the content and mode of blobs found via two tree objects'),
' ',
('-M', 'Detect renames'),
' ',
('-r', 'recurse into subtrees'),
' ',
('--name-status',
"""Show only names and status of changed files
for example:
M foo.py"""),
' ',
('<commit>', 'show differences between this commit and preceding one')]
def drawat(pos, string):
return (' ' * pos) + string
def drawhorizontal(a,b,pen):
(a,b) = sorted((a,b))
l = b-a
if l:
line = ''.join([box(('e',pen)),
box(('ew',pen)) * (l-1),
box(('w',pen))])
return drawat(a, line)
else:
return ''
def drawverticals(point, items):
return overlay([drawat(pos, box((point, pen))) for (pos,pen,*_) in items])
def resolve(existing, instructions, width=65):
fst = itemgetter(0)
snd = itemgetter(1)
n = drawverticals('n', existing)
pens = {pos: pen for (pos,pen,*_) in existing}
texts = {pos: text for (pos,_,text,*_) in existing}
ew = overlay([drawhorizontal(i['pos'], i['to'], pens[i['pos']])
for i
in instructions
if 'to' in i])
explicit = {i['pos'] for i in instructions}
implicit = [(pos, *rest)
for (pos, *rest)
in existing
if not pos in explicit]
def cont(i):
f = i['pos']
t = i['to']
return (t, pens[f], texts[f])
continuation = [cont(i)
for i in instructions
if not 'go' in i]\
+ implicit
# sorted and merged paths
def aux(k,v):
v = list(v)
pen = any(map(snd, v))
[(_,_,comment), *_] = v
return (k, pen, comment)
nextState = [aux(k,v)
for (k, v)
in groupby(sorted(continuation, key=fst), fst)]
s = drawverticals('s', nextState)
lines = overlay([n, ew, s])
gone = next(filter(lambda i: 'go' in i, instructions), None)
if gone:
l = len(lines)
text = reflow(texts[gone['pos']], width - l - 1)
(then, _) = resolve(nextState, [])
first = lines + ' '
then = then + ' ' * (len(first) - len(then))
lines = ("\n".join(
[h+t
for (h,t)
in zip(chain([first], repeat(then)),
text)]))
return (lines, nextState)
def move(f, t):
return {'pos': f, 'to': t}
def go(f, t):
return {'pos': f, 'to': t, 'go': True}
def nmarkers(ts):
usePens = True # TODO pass in
def aux(t):
(_,c,l,o) = t
end = o + l - 1
mean = o + int(l/2)
return (o, mean, end, c, None)
ps = list(map(aux, ts))
pens = cycle([False, True]) if usePens else repeat(False)
pre = flatten([[(start, pen, comment, None),
(end, pen, comment, None)]
for ((start, _, end, comment, _), pen)
in zip(ps, pens)])
instructions = flatten([[move(start, mean),
move(end, mean)]
for (start, mean, end, comment, _)
in ps])
return resolve(pre, instructions)
def toleft_instructions(lanes):
def aux(lanes, idx):
pos = lanes[idx][0]
return move(pos, idx)
return [[aux(lanes, idx)] for idx in range(0, len(lanes))]
def define_instructions(lanes):
l = len(lanes) + 1
def aux(lane):
pos = lane[0]
return go(pos, l)
return [[aux(lane)] for lane in lanes]
def toleft(lanes):
return resolve_list(lanes, toleft_instructions(lanes))
def define(lanes):
return resolve_list(lanes, define_instructions(lanes))
def resolve_list(lanes, instructions):
def aux(output_lanes, i):
(o, lanes) = output_lanes
(o2, lanes2) = resolve(lanes, i)
return (o + [o2], lanes2)
(output, lanes) = reduce(aux, instructions, ([], lanes))
return ('\n'.join(output), lanes)
def unit(data):
return ("", data)
def bind(t, f):
(o, x) = t
(o2, x2) = f(x)
return (o + o2 + "\n", x2)
def sequence(t, fs):
return reduce(bind, fs, t)
def run(t, fs):
(output, _) = sequence(unit(t), fs)
print(output)
def explain(tokens):
run(tokens,
[normalise_tokens,
nmarkers,
toleft,
define ])
def parse1(string):
(command, markers, *data) = string.split("\n")
groups = groupby(zip(command, markers), key=itemgetter(1))
def aux(g):
group = g[0]
command = ''.join(list(map(itemgetter(0), g[1])))
return (group, command)
print([aux(g) for g in groups])
explain(descs)
def parseres(string, rxs):
def aux(acc, token):
last = acc[-1][-1]
rx = re.escape(token)
match = re.compile(rx).search(string, last)
if match:
return acc + [(token, match.start(), match.end())]
else:
raise Exception("BAD PROGRAMMER")
slices = reduce(aux, rxs, [(0,)])[1:]
t = len(string)
def fill(i, ss):
if i == t:
return []
if not ss:
return [(string[i:t], i,t)]
[(token, s,e),*rest] = ss
if i == s:
return [(token, s,e)] + fill(e, rest)
else:
return [(string[i:s], i,s), (token, s,e)] + fill(e, rest)
return fill(0, slices)
def parse(string):
...
parse("""\
git log -m thingy
# git
version control ting
# log
do some logging innit""")
# print(parseres("git log -m thingy", ["git", "log", "-m"]))