-
-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathinplace.py
45 lines (34 loc) · 1.39 KB
/
inplace.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
from visidata import Column, vd, ColumnExpr, CompleteExpr, EscapeException, Sheet
@Column.api
def updateExpr(col, val):
col.name = val
try:
col.expr = val
except SyntaxError:
col.expr = None
col.sheet.draw(vd.scr)
@Column.api # expr.setter
def expr(self, expr):
try:
self.compiledExpr = compile(expr, '<expr>', 'eval') if expr else None
self._expr = expr
except SyntaxError as e:
self._expr = None
@Sheet.api
def addcol_expr(sheet):
try:
c = sheet.addColumnAtCursor(ColumnExpr("", width=sheet.options.default_width))
oldidx = sheet.cursorVisibleColIndex
sheet.cursorVisibleColIndex = sheet.visibleCols.index(c)
expr = sheet.editCell(sheet.cursorVisibleColIndex, -1,
completer=CompleteExpr(sheet),
updater=lambda val,col=c: col.updateExpr(val))
c.expr = expr or vd.fail("no expr")
c.name = expr
c.width = None
except (Exception, EscapeException):
sheet.columns.remove(c)
sheet.cursorVisibleColIndex = oldidx
raise
Sheet.addCommand(None, 'addcol-expr', 'sheet.addcol_expr()')
Sheet.addCommand(None, 'addcol-new', 'c=addColumnAtIndex(SettableColumn(width=options.default_width)); draw(vd.scr); cursorVisibleColIndex=visibleCols.index(c); c.name=editCell(cursorVisibleColIndex, -1); c.width=None')