-
-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathsheets.py
1206 lines (961 loc) · 45.5 KB
/
sheets.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
import itertools
from copy import copy, deepcopy
import textwrap
from visidata import VisiData, Extensible, globalCommand, ColumnAttr, ColumnItem, vd, ENTER, EscapeException, drawcache, drawcache_property, LazyChainMap, asyncthread, ExpectedException
from visidata import (options, Column, namedlist, SettableColumn,
TypedExceptionWrapper, BaseSheet, UNLOADED,
clipdraw, clipdraw_chunks, ColorAttr, update_attr, colors, undoAttrFunc, vlen, dispwidth)
import visidata
vd.activePane = 1 # pane numbering starts at 1; pane 0 means active pane
vd.option('name_joiner', '_', 'string to join sheet or column names', max_help=0)
vd.option('value_joiner', ' ', 'string to join display values', max_help=0)
@drawcache
def _splitcell(sheet, s, width=0, maxheight=1):
if width <= 0 or not sheet.options.textwrap_cells:
return [s]
ret = []
for attr, text in s:
for line in textwrap.wrap(
text, width=width, break_long_words=False, replace_whitespace=False
):
if len(ret) >= maxheight:
ret[-1][0][1] += ' ' + line
break
else:
ret.append([[attr, line]])
return ret
disp_column_fill = ' ' # pad chars after column value
# higher precedence color overrides lower; all non-color attributes combine
# coloropt is the color option name (like 'color_error')
# func(sheet,col,row,value) should return a true value if coloropt should be applied
# if coloropt is None, func() should return a coloropt (or None) instead
Colorizer = collections.namedtuple('Colorizer', 'precedence coloropt func')
RowColorizer = collections.namedtuple('RowColorizer', 'precedence coloropt func')
CellColorizer = collections.namedtuple('CellColorizer', 'precedence coloropt func')
ColumnColorizer = collections.namedtuple('ColumnColorizer', 'precedence coloropt func')
class RecursiveExprException(Exception):
pass
class LazyComputeRow:
'Calculate column values as needed.'
def __init__(self, sheet, row, col=None):
self.row = row
self.col = col
self.sheet = sheet
self._usedcols = set()
self._lcm.clear() # reset locals on lcm
@property
def _lcm(self):
lcmobj = self.col or self.sheet
if not hasattr(lcmobj, '_lcm'):
lcmobj._lcm = LazyChainMap(self.sheet, self.col, *vd.contexts)
return lcmobj._lcm
def keys(self):
return self.sheet._ordered_colnames + self._lcm.keys() + ['row', 'sheet', 'col']
def __str__(self):
return str(self.as_dict())
def as_dict(self):
return {c.name:self[c.name] for c in self.sheet.visibleCols}
def __getattr__(self, k):
return self.__getitem__(k)
def __getitem__(self, colid):
try:
i = self.sheet._ordered_colnames.index(colid)
c = self.sheet._ordered_cols[i]
if c is self.col: # ignore current column
j = self.sheet._ordered_colnames[i+1:].index(colid)
c = self.sheet._ordered_cols[i+j+1]
except ValueError:
try:
c = self._lcm[colid]
except (KeyError, AttributeError) as e:
if colid == 'sheet': return self.sheet
elif colid == 'row': c = self.row
elif colid == 'col': c = self.col
else:
raise KeyError(colid) from e
if not isinstance(c, Column): # columns calc in the context of the row of the cell being calc'ed
return c
if c in self._usedcols:
raise RecursiveExprException()
self._usedcols.add(c)
ret = c.getTypedValue(self.row)
self._usedcols.remove(c)
return ret
class BasicRow(collections.defaultdict):
def __init__(self, *args):
collections.defaultdict.__init__(self, lambda: None)
def __bool__(self):
return True
class TableSheet(BaseSheet):
'Base class for sheets with row objects and column views.'
_rowtype = lambda: BasicRow()
_coltype = SettableColumn
rowtype = 'rows'
guide = '# {sheet.help_title}\n{sheet.help_columns}\n'
@property
def help_title(self):
if isinstance(self.source, visidata.Path):
return 'Source Table'
else:
return 'Table Sheet'
@property
def help_columns(self):
hiddenCols = [c for c in self.columns if c.hidden]
if hiddenCols:
return f'- `gv` to unhide {len(hiddenCols)} hidden columns'
return ''
columns = [] # list of Column
colorizers = [ # list of Colorizer
CellColorizer(2, 'color_default_hdr', lambda s,c,r,v: r is None),
ColumnColorizer(2, 'color_current_col', lambda s,c,r,v: c is s.cursorCol),
ColumnColorizer(1, 'color_key_col', lambda s,c,r,v: c and c.keycol),
CellColorizer(0, 'color_default', lambda s,c,r,v: True),
RowColorizer(1, 'color_error', lambda s,c,r,v: isinstance(r, (Exception, TypedExceptionWrapper))),
CellColorizer(3, 'color_current_cell', lambda s,c,r,v: c is s.cursorCol and r is s.cursorRow)
]
nKeys = 0 # columns[:nKeys] are key columns
def __init__(self, *names, rows=UNLOADED, **kwargs):
super().__init__(*names, rows=rows, **kwargs)
self.cursorRowIndex = 0 # absolute index of cursor into self.rows
self.cursorVisibleColIndex = 0 # index of cursor into self.visibleCols
self._topRowIndex = 0 # cursorRowIndex of topmost row
self.leftVisibleColIndex = 0 # cursorVisibleColIndex of leftmost column
self.rightVisibleColIndex = 0
# as computed during draw()
self._rowLayout = {} # [rowidx] -> (y, w)
self._visibleColLayout = {} # [vcolidx] -> (x, w)
# list of all columns in display order
self.initialCols = kwargs.pop('columns', None) or type(self).columns
self.resetCols()
self._colorizers = self.classColorizers
self.recalc() # set .sheet on columns and start caches
self.__dict__.update(kwargs) # also done earlier in BaseSheet.__init__
@property
def topRowIndex(self):
return self._topRowIndex
@topRowIndex.setter
def topRowIndex(self, v):
self._topRowIndex = v
self._rowLayout.clear()
def addColorizer(self, c):
'Add Colorizer *c* to the list of colorizers for this sheet.'
self._colorizers.append(c)
self._colorizers = sorted(self._colorizers, key=lambda x: x.precedence, reverse=True)
def removeColorizer(self, c):
'Remove Colorizer *c* from the list of colorizers for this sheet.'
self._colorizers.remove(c)
@property
def classColorizers(self) -> list:
'List of all colorizers from sheet class hierarchy in precedence order (highest precedence first)'
# all colorizers must be in the same bucket
# otherwise, precedence does not get applied properly
_colorizers = set()
for b in [self] + list(type(self).superclasses()):
for c in getattr(b, 'colorizers', []):
_colorizers.add(c)
return sorted(_colorizers, key=lambda x: x.precedence, reverse=True)
def _colorize(self, col, row, value=None) -> ColorAttr:
'Return ColorAttr for the given colorizers/col/row/value'
colorstack = []
for colorizer in self._colorizers:
try:
r = colorizer.func(self, col, row, value)
if r:
colorstack.append((colorizer.precedence, colorizer.coloropt if colorizer.coloropt else r))
except Exception as e:
vd.exceptionCaught(e)
return colors.resolve_colors(tuple(colorstack))
def addRow(self, row, index=None):
'Insert *row* at *index*, or append at end of rows if *index* is None.'
if index is None:
self.rows.append(row)
else:
self.rows.insert(index, row)
if self.cursorRowIndex and self.cursorRowIndex >= index:
self.cursorRowIndex += 1
return row
def newRow(self):
'Return new blank row compatible with this sheet. Overridable.'
return type(self)._rowtype()
@drawcache_property
def colsByName(self):
'Return dict of colname:col'
# dict comprehension in reverse order so first column with the name is used
return {col.name:col for col in self.columns[::-1]}
def column(self, colname):
'Return first column whose name matches *colname*.'
return self.colsByName.get(colname) or vd.fail('no column matching "%s"' % colname)
def recalc(self):
'Clear caches and set the ``sheet`` attribute on all columns.'
for c in self.columns:
c.recalc(self)
@asyncthread
def reload(self):
'Load or reload rows and columns from ``self.source``. Async. Override resetCols() or loader() in subclass.'
with visidata.ScopedSetattr(self, 'loading', True):
self.resetCols()
self.beforeLoad()
try:
self.loader()
vd.debug(f'finished loading {self}')
finally:
self.afterLoad()
self.recalc()
def beforeLoad(self):
pass
def resetCols(self):
'Reset columns to class settings'
self.columns = []
for c in self.initialCols:
self.addColumn(deepcopy(c))
if self.options.disp_help > c.max_help:
c.hide()
self.setKeys(self.columns[:self.nKeys])
def loader(self):
'Reset rows and sync load ``source`` via iterload. Overrideable.'
self.rows = []
try:
with vd.Progress(gerund='loading', total=0):
for r in self.iterload():
self.addRow(r)
except FileNotFoundError:
return # let it be a blank sheet without error
def iterload(self):
'Generate rows from ``self.source``. Override in subclass.'
if False:
yield vd.fail('no iterload for this loader yet')
def afterLoad(self):
'hook for after loading has finished. Overrideable (be sure to call super).'
# if an ordering has been specified, sort the sheet
if self._ordering:
vd.sync(self.sort())
def iterrows(self):
if self.rows is UNLOADED:
try:
self.rows = []
for row in self.iterload():
self.addRow(row)
yield row
return
except ExpectedException:
vd.sync(self.reload())
for row in vd.Progress(self.rows):
yield row
def __iter__(self):
for row in self.iterrows():
yield LazyComputeRow(self, row)
def __copy__(self):
'Copy sheet design but remain unloaded. Deepcopy columns so their attributes (width, type, name) may be adjusted independently of the original.'
ret = super().__copy__()
ret.rows = UNLOADED
ret.columns = []
col_mapping = {}
for c in self.columns:
new_col = copy(c)
col_mapping[c] = new_col
ret.addColumn(new_col)
ret.setKeys([col_mapping[c] for c in self.columns if c.keycol])
ret._ordering = []
for sortcol,reverse in self._ordering:
if isinstance(sortcol, str):
ret._ordering.append((sortcol,reverse))
else:
ret._ordering.append((col_mapping[sortcol],reverse))
ret.topRowIndex = ret.cursorRowIndex = 0
return ret
@property
def bottomRowIndex(self):
return self.topRowIndex+self.nScreenRows-1
@bottomRowIndex.setter
def bottomRowIndex(self, newidx):
self._topRowIndex = newidx-self.nScreenRows+1
@drawcache_property
def rowHeight(self):
cols = self.visibleCols
return max(c.height for c in cols) if cols else 1
def __deepcopy__(self, memo):
'same as __copy__'
ret = self.__copy__()
memo[id(self)] = ret
return ret
def __str__(self):
return self.name
def __repr__(self):
return f'<{type(self).__name__}: {self.name}>'
def evalExpr(self, expr, row=None, col=None):
if row is not None:
# contexts are cached by sheet/rowid for duration of drawcycle
contexts = vd._evalcontexts.setdefault((self, self.rowid(row), col), LazyComputeRow(self, row, col=col))
else:
contexts = None
return eval(expr, vd.getGlobals(), contexts)
def rowid(self, row):
'Return a unique and stable hash of the *row* object. Must be fast. Overridable.'
return id(row)
@property
def nScreenRows(self):
'Number of visible rows at the current window height.'
return (self.windowHeight-self.nHeaderRows-self.nFooterRows)//self.rowHeight
@drawcache_property
def nHeaderRows(self):
vcols = self.visibleCols
return max(len(col.name.split('\n')) for col in vcols) if vcols else 0
@property
def nFooterRows(self):
'Number of lines reserved at the bottom, including status line.'
return 1
@property
def cursorCol(self):
'Current Column object.'
vcols = self.visibleCols
return vcols[min(self.cursorVisibleColIndex, len(vcols)-1)] if vcols else None
@property
def cursorRow(self):
'The row object at the row cursor.'
idx = self.cursorRowIndex
return self.rows[idx] if self.nRows > idx else None
@property
def visibleRows(self): # onscreen rows
'List of rows onscreen.'
return self.rows[self.topRowIndex:self.topRowIndex+self.nScreenRows]
@drawcache_property
def visibleCols(self): # non-hidden cols
'List of non-hidden columns in display order.'
return self.keyCols + [c for c in self.columns if not c.hidden and not c.keycol]
@drawcache_property
def keyCols(self):
'List of visible key columns.'
return sorted([c for c in self.columns if c.keycol and not c.hidden], key=lambda c:c.keycol)
@drawcache_property
def _ordered_cols(self):
'List of all columns, visible columns first.'
return self.visibleCols + [c for c in self.columns if c.hidden]
@drawcache_property
def _ordered_colnames(self):
'List of all column names, visible columns first.'
return [c.name for c in self._ordered_cols]
@property
def cursorColIndex(self):
'Index of current column into `Sheet.columns`. Linear search; prefer `cursorCol` or `cursorVisibleColIndex`.'
try:
return self.columns.index(self.cursorCol)
except ValueError:
return None
@property
def nonKeyVisibleCols(self):
'List of visible non-key columns.'
return [c for c in self.columns if not c.hidden and c not in self.keyCols]
@property
def keyColNames(self):
'String of key column names, for SheetsSheet convenience.'
return ' '.join(c.name for c in self.keyCols)
@keyColNames.setter
def keyColNames(self, v): #2122
'Set key columns on this sheet to the space-separated list of column names.'
newkeys = [self.column(colname) for colname in v.split()]
self.unsetKeys(self.keyCols)
self.setKeys(newkeys)
@property
def cursorCell(self):
'Displayed value (DisplayWrapper) at current row and column.'
return self.cursorCol.getCell(self.cursorRow)
@property
def cursorDisplay(self):
'Displayed value (DisplayWrapper.text) at current row and column.'
return self.cursorCol.getDisplayValue(self.cursorRow)
@property
def cursorTypedValue(self):
'Typed value at current row and column.'
return self.cursorCol.getTypedValue(self.cursorRow)
@property
def cursorValue(self):
'Raw value at current row and column.'
return self.cursorCol.getValue(self.cursorRow)
@property
def statusLine(self):
'Position of cursor and bounds of current sheet.'
rowinfo = 'row %d (%d selected)' % (self.cursorRowIndex, self.nSelectedRows)
colinfo = 'col %d (%d visible)' % (self.cursorVisibleColIndex, len(self.visibleCols))
return '%s %s' % (rowinfo, colinfo)
@property
def nRows(self):
'Number of rows on this sheet.'
return len(self.rows)
@property
def nCols(self):
'Number of columns on this sheet.'
return len(self.columns)
@property
def nVisibleCols(self):
'Number of visible columns on this sheet.'
return len(self.visibleCols)
def cursorDown(self, n=1):
'Move cursor down `n` rows (or up if `n` is negative).'
self.cursorRowIndex += n
def cursorRight(self, n=1):
'Move cursor right `n` visible columns (or left if `n` is negative).'
self.cursorVisibleColIndex += n
self.calcColLayout()
def addColumn(self, *cols, index=None):
'''Insert all *cols* into columns at *index*, or append to end of columns if *index* is None.
If *index* is None, columns are being added by loader, instead of by user.
If added by user, mark sheet as modified.
Columns added by loader share sheet's defer status.
Columns added by user are not marked as deferred.
Return first column.'''
if not cols:
vd.warning('no columns to add')
return
if index is not None:
self.setModified()
for i, col in enumerate(cols):
col.name = self.maybeClean(col.name)
col.defer = self.defer
vd.addUndo(self.columns.remove, col)
idx = len(self.columns) if index is None else index
col.recalc(self)
self.columns.insert(idx+i, col)
Sheet.visibleCols.fget.cache_clear()
return cols[0]
def addColumnAtCursor(self, *cols):
'Insert all *cols* into columns after cursor. Return first column.'
index = 0
ccol = self.cursorCol
if ccol and not ccol.keycol:
index = self.columns.index(ccol)+1
self.addColumn(*cols, index=index)
firstnewcol = [c for c in cols if not c.hidden][0]
self.cursorVisibleColIndex = self.visibleCols.index(firstnewcol)
return firstnewcol
def setColNames(self, rows):
for c in self.visibleCols:
c.name = '\n'.join(str(c.getDisplayValue(r)) for r in rows)
def setKeys(self, cols):
'Make all *cols* into key columns.'
vd.addUndo(undoAttrFunc(cols, 'keycol'))
lastkeycol = 0
if self.keyCols:
lastkeycol = max(c.keycol for c in self.keyCols)
for col in cols:
if not col.keycol:
col.keycol = lastkeycol+1
lastkeycol += 1
def unsetKeys(self, cols):
'Make all *cols* non-key columns.'
vd.addUndo(undoAttrFunc(cols, 'keycol'))
for col in cols:
col.keycol = 0
def toggleKeys(self, cols):
for col in cols:
if col.keycol:
self.unsetKeys([col])
else:
self.setKeys([col])
def rowkey(self, row):
'Return tuple of the key for *row*.'
return tuple(c.getTypedValue(row) for c in self.keyCols)
def keystr(self, row):
'Return string of the key for *row*.'
return ','.join(map(str, self.rowkey(row)))
def checkCursor(self):
'Keep cursor in bounds of data and screen.'
# keep cursor within actual available rowset
if self.nRows == 0 or self.cursorRowIndex <= 0:
self.cursorRowIndex = 0
elif self.cursorRowIndex >= self.nRows:
self.cursorRowIndex = self.nRows-1
if self.cursorVisibleColIndex <= 0:
self.cursorVisibleColIndex = 0
elif self.cursorVisibleColIndex >= self.nVisibleCols:
self.cursorVisibleColIndex = self.nVisibleCols-1
if self.topRowIndex < 0:
self.topRowIndex = 0
elif self.topRowIndex > self.nRows-1:
self.topRowIndex = self.nRows-1
# check bounds, scroll if necessary
if self.topRowIndex > self.cursorRowIndex:
self.topRowIndex = self.cursorRowIndex
elif self.bottomRowIndex < self.cursorRowIndex:
self.bottomRowIndex = self.cursorRowIndex
if self.cursorCol and self.cursorCol.keycol:
return
if self.leftVisibleColIndex >= self.cursorVisibleColIndex:
self.leftVisibleColIndex = self.cursorVisibleColIndex
else:
while True:
if self.leftVisibleColIndex == self.cursorVisibleColIndex: # not much more we can do
break
self.calcColLayout()
if not self._visibleColLayout:
break
mincolidx, maxcolidx = min(self._visibleColLayout.keys()), max(self._visibleColLayout.keys())
if self.cursorVisibleColIndex < mincolidx:
self.leftVisibleColIndex -= max((self.cursorVisibleColIndex - mincolidx)//2, 1)
continue
elif self.cursorVisibleColIndex > maxcolidx:
self.leftVisibleColIndex += max((maxcolidx - self.cursorVisibleColIndex)//2, 1)
continue
cur_x, cur_w = self._visibleColLayout[self.cursorVisibleColIndex]
if cur_x+cur_w < self.windowWidth: # current columns fit entirely on screen
break
self.leftVisibleColIndex += 1 # once within the bounds, walk over one column at a time
def calcColLayout(self):
'Set right-most visible column, based on calculation.'
minColWidth = dispwidth(self.options.disp_more_left)+dispwidth(self.options.disp_more_right)+2
sepColWidth = dispwidth(self.options.disp_column_sep)
winWidth = self.windowWidth
self._visibleColLayout = {}
x = 0
vcolidx = 0
for vcolidx in range(0, self.nVisibleCols):
width = self.calcSingleColLayout(vcolidx, x, minColWidth)
if width:
x += width+sepColWidth
if x > winWidth-1:
break
self.rightVisibleColIndex = vcolidx
def calcSingleColLayout(self, vcolidx:int, x:int=0, minColWidth:int=4):
col = self.visibleCols[vcolidx]
if col.width is None and len(self.visibleRows) > 0:
vrows = self.visibleRows if self.nRows > 1000 else self.rows[:1000] #1964
# handle delayed column width-finding
col.width = max(col.getMaxWidth(vrows), minColWidth)
if vcolidx != self.nVisibleCols-1: # let last column fill up the max width
col.width = min(col.width, self.options.default_width)
width = col.width if col.width is not None else self.options.default_width
if col in self.keyCols:
width = max(width, 1) # keycols must all be visible
if col in self.keyCols or vcolidx >= self.leftVisibleColIndex: # visible columns
self._visibleColLayout[vcolidx] = [x, min(width, self.windowWidth-x)]
return width
def drawColHeader(self, scr, y, h, vcolidx):
'Compose and draw column header for given vcolidx.'
col = self.visibleCols[vcolidx]
# hdrattr highlights whole column header
# sepattr is for header separators and indicators
sepcattr = colors.get_color('color_column_sep')
hdrcattr = self._colorize(col, None)
if vcolidx == self.cursorVisibleColIndex:
hdrcattr = update_attr(hdrcattr, colors.color_current_hdr, 2)
C = self.options.disp_column_sep
if (self.keyCols and col is self.keyCols[-1]) or vcolidx == self.rightVisibleColIndex:
C = self.options.disp_keycol_sep
x, colwidth = self._visibleColLayout[vcolidx]
# AnameTC
T = vd.getType(col.type).icon
if T is None: # still allow icon to be explicitly non-displayed ''
T = '?'
hdrs = col.name.split('\n')
for i in range(h):
name = ''
if colwidth > 2:
name = ' ' # save room at front for LeftMore or sorted arrow
if h-i-1 < len(hdrs):
name += hdrs[::-1][h-i-1]
if i == h-1:
hdrcattr = update_attr(hdrcattr, colors.color_bottom_hdr, 5)
clipdraw(scr, y+i, x, name, hdrcattr, w=colwidth)
vd.onMouse(scr, x, y+i, colwidth, 1, BUTTON3_RELEASED='rename-col')
if C and x+colwidth+len(C) < self.windowWidth and y+i < self.windowWidth:
scr.addstr(y+i, x+colwidth, C, sepcattr.attr)
clipdraw(scr, y+h-1, x+colwidth-len(T), T, hdrcattr)
try:
if vcolidx == self.leftVisibleColIndex and col not in self.keyCols and self.nonKeyVisibleCols.index(col) > 0:
A = self.options.disp_more_left
scr.addstr(y, x, A, sepcattr.attr)
except ValueError: # from .index
pass
try:
A = ''
for j, (sortcol, sortdir) in enumerate(self._ordering):
if isinstance(sortcol, str):
sortcol = self.column(sortcol)
if col is sortcol:
A = self.options.disp_sort_desc[j] if sortdir else self.options.disp_sort_asc[j]
scr.addstr(y+h-1, x, A, hdrcattr.attr)
break
except IndexError:
pass
def isVisibleIdxKey(self, vcolidx):
'Return boolean: is given column index a key column?'
return self.visibleCols[vcolidx] in self.keyCols
def draw(self, scr):
'Draw entire screen onto the `scr` curses object.'
if not self.columns:
return
drawparams = {
'isNull': self.isNullFunc(),
'topsep': self.options.disp_rowtop_sep,
'midsep': self.options.disp_rowmid_sep,
'botsep': self.options.disp_rowbot_sep,
'endsep': self.options.disp_rowend_sep,
'keytopsep': self.options.disp_keytop_sep,
'keymidsep': self.options.disp_keymid_sep,
'keybotsep': self.options.disp_keybot_sep,
'endtopsep': self.options.disp_endtop_sep,
'endmidsep': self.options.disp_endmid_sep,
'endbotsep': self.options.disp_endbot_sep,
'colsep': self.options.disp_column_sep,
'keysep': self.options.disp_keycol_sep,
'selectednote': self.options.disp_selected_note,
'disp_truncator': self.options.disp_truncator,
}
self._rowLayout = {} # [rowidx] -> (y, height)
self.calcColLayout()
numHeaderRows = self.nHeaderRows
vcolidx = 0
headerRow = 0
for vcolidx, colinfo in sorted(self._visibleColLayout.items()):
self.drawColHeader(scr, headerRow, numHeaderRows, vcolidx)
y = headerRow + numHeaderRows
rows = self.rows[self.topRowIndex:min(self.topRowIndex+self.nScreenRows+1, self.nRows)]
vd.callNoExceptions(self.checkCursor)
for rowidx, row in enumerate(rows):
if y >= self.windowHeight-1:
break
rowcattr = self._colorize(None, row)
y += self.drawRow(scr, row, self.topRowIndex+rowidx, y, rowcattr, maxheight=self.windowHeight-y-1, **drawparams)
if vcolidx+1 < self.nVisibleCols:
scr.addstr(headerRow, self.windowWidth-2, self.options.disp_more_right, colors.color_column_sep.attr)
def calc_height(self, row, displines=None, isNull=None, maxheight=1):
'render cell contents ifor row into displines'
if displines is None:
displines = {} # [vcolidx] -> list of lines in that cell
for vcolidx, (x, colwidth) in sorted(self._visibleColLayout.items()):
if x < self.windowWidth: # only draw inside window
vcols = self.visibleCols
if vcolidx >= len(vcols):
continue
col = vcols[vcolidx]
cellval = col.getCell(row)
cellval.display = col.display(cellval, colwidth)
try:
if isNull and isNull(cellval.value):
cellval.note = self.options.disp_note_none
cellval.notecolor = 'color_note_type'
except (TypeError, ValueError):
pass
if maxheight > 1:
lines = _splitcell(self, cellval.display, width=colwidth-2, maxheight=maxheight)
else:
lines = [cellval.display]
displines[vcolidx] = (col, cellval, lines)
if len(displines) == 0:
return 0
return max(len(lines) for _, _, lines in displines.values())
def drawRow(self, scr, row, rowidx, ybase, rowcattr: ColorAttr, maxheight,
isNull='',
topsep='',
midsep='',
botsep='',
endsep='',
keytopsep='',
keymidsep='',
keybotsep='',
endtopsep='',
endmidsep='',
endbotsep='',
colsep='',
keysep='',
selectednote='',
disp_truncator=''
):
# sepattr is the attr between cell/columns
sepcattr = update_attr(rowcattr, colors.color_column_sep, 1)
# apply current row here instead of in a colorizer, because it needs to know dispRowIndex
if rowidx == self.cursorRowIndex:
color_current_row = colors.get_color('color_current_row', 2)
basecellcattr = sepcattr = update_attr(rowcattr, color_current_row)
else:
basecellcattr = rowcattr
# calc_height renders cell contents into displines
displines = {} # [vcolidx] -> list of lines in that cell
self.calc_height(row, displines, maxheight=self.rowHeight)
height = min(self.rowHeight, maxheight) or 1 # display even empty rows
self._rowLayout[rowidx] = (ybase, height)
for vcolidx, (col, cellval, lines) in displines.items():
if vcolidx not in self._visibleColLayout:
continue
x, colwidth = self._visibleColLayout[vcolidx]
hoffset = col.hoffset
voffset = col.voffset
cattr = self._colorize(col, row, cellval)
cattr = update_attr(cattr, basecellcattr)
note = getattr(cellval, 'note', None)
notewidth = 1 if note else 0
if note:
notecattr = update_attr(cattr, colors.get_color(cellval.notecolor), 10)
scr.addstr(ybase, x+colwidth-notewidth, note, notecattr.attr)
lines = lines[voffset:]
if len(lines) > height:
lines = lines[:height]
elif len(lines) < height:
lines.extend([[('', '')]]*(height-len(lines)))
for i, chunks in enumerate(lines):
y = ybase+i
if vcolidx == self.rightVisibleColIndex: # right edge of sheet
if len(lines) == 1:
sepchars = endsep
else:
if i == 0:
sepchars = endtopsep
elif i == len(lines)-1:
sepchars = endbotsep
else:
sepchars = endmidsep
elif (self.keyCols and col is self.keyCols[-1]): # last keycol
if len(lines) == 1:
sepchars = keysep
else:
if i == 0:
sepchars = keytopsep
elif i == len(lines)-1:
sepchars = keybotsep
else:
sepchars = keymidsep
else:
if len(lines) == 1:
sepchars = colsep
else:
if i == 0:
sepchars = topsep
elif i == len(lines)-1:
sepchars = botsep
else:
sepchars = midsep
pre = disp_truncator if hoffset != 0 else disp_column_fill
prechunks = []
if colwidth > 2:
prechunks.append(('', pre))
for attr, text in chunks:
prechunks.append((attr, text[hoffset:]))
clipdraw_chunks(scr, y, x, prechunks, cattr, w=colwidth-notewidth)
vd.onMouse(scr, x, y, colwidth, 1, BUTTON3_RELEASED='edit-cell')
if x+colwidth+len(sepchars) <= self.windowWidth:
scr.addstr(y, x+colwidth, sepchars, sepcattr.attr)
for notefunc in vd.rowNoters:
ch = notefunc(self, row)
if ch:
clipdraw(scr, ybase, 0, ch, colors.color_note_row)
break
return height
vd.rowNoters = [
# f(sheet, row) -> character to be displayed on the left side of row
]
Sheet = TableSheet # deprecated in 2.0 but still widely used internally
class SequenceSheet(Sheet):
'Sheets with ``ColumnItem`` columns, and rows that are Python sequences (list, namedtuple, etc).'
def setCols(self, headerrows):
self.columns = []
vd.clearCaches() #1997
for i, colnamelines in enumerate(itertools.zip_longest(*headerrows, fillvalue='')):
colnamelines = ['' if c is None else c for c in colnamelines]
self.addColumn(ColumnItem(''.join(map(str, colnamelines)), i))
self._rowtype = namedlist('tsvobj', [(c.name or '_') for c in self.columns])
def newRow(self):
return self._rowtype()
def addRow(self, row, index=None):
for i in range(len(self.columns), len(row)): # no-op if already done
self.addColumn(ColumnItem('', i))
self._rowtype = namedlist('tsvobj', [(c.name or '_') for c in self.columns])
if type(row) is not self._rowtype:
row = self._rowtype(row)
super().addRow(row, index=index)
def optlines(self, it, optname):
'Generate next options.<optname> elements from iterator with exceptions wrapped.'
for i in range(self.options.getobj(optname, self)):
try:
yield next(it)
except StopIteration:
break
def loader(self):
'Skip first options.skip rows; set columns from next options.header rows.'
itsource = self.iterload()
# skip the first options.skip rows
list(self.optlines(itsource, 'skip'))
# use the next options.header rows as columns
self.setCols(list(self.optlines(itsource, 'header')))
self.rows = []
# add the rest of the rows
for r in vd.Progress(itsource, gerund='loading', total=0):
self.addRow(r)
@VisiData.property
@drawcache
def _evalcontexts(vd):
return {}
## VisiData sheet manipulation
@VisiData.api
def replace(vd, vs):
'Replace top sheet with the given sheet `vs`.'
vd.sheets.pop(0)
return vd.push(vs)
@VisiData.api
def remove(vd, vs):
'Remove *vs* from sheets stack, without asking for confirmation.'
if vs in vd.sheets:
vd.sheets.remove(vs)
if vs in vd.allSheets:
vd.allSheets.remove(vs)
vd.allSheets.append(vs)
else:
vd.fail('sheet not on stack')
@VisiData.api