def save(self, *rows): changes = [] deletes = {} for r in list( rows or self.rows): # copy list because elements may be removed if r in self.toBeDeleted: deletes[id(r)] = r else: for col in self.visibleCols: if self.changed(col, r): changes.append((col, r)) if not changes and not deletes: fail('nothing to save') cstr = '' if changes: cstr += 'change %d attributes' % len(changes) if deletes: if cstr: cstr += ' and ' cstr += 'delete %d files' % len(deletes) confirm('really %s? ' % cstr) self._commit(changes, deletes)
def setValue(self, row, v): srcSheet, srcRow = row srcCol = self.colsBySheet.get(srcSheet, None) if srcCol: srcCol.setValue(srcRow, v) else: fail('column not on source sheet')
def syscopyRows(sheet, rows): if not rows: fail('no %s selected' % sheet.rowtype) filetype = vd.input("copy %d %s to system clipboard as filetype: " % (len(rows), sheet.rowtype), value=options.save_filetype) saveToClipboard(sheet, rows, filetype) status('copied %d %s to system clipboard' % (len(rows), sheet.rowtype))
def nextColRegex(sheet, colregex): 'Go to first visible column after the cursor matching `colregex`.' pivot = sheet.cursorVisibleColIndex for i in itertools.chain(range(pivot+1, len(sheet.visibleCols)), range(0, pivot+1)): c = sheet.visibleCols[i] if re.search(colregex, c.name, regex_flags()): return i fail('no column name matches /%s/' % colregex)
def to_tabulate_table(sheet, fmt): if fmt not in SUPPORTED_FORMATS: fail(f"'{fmt}' is not a supported 'tabulate' format") headers = [col.name for col in sheet.visibleCols] def get_row_values(row): return [col.getDisplayValue(row) for col in sheet.visibleCols] return tabulate(map(get_row_values, Progress(sheet.rows)), headers, tablefmt=fmt)
def searchRegex(vd, sheet, moveCursor=False, reverse=False, **kwargs): 'Set row index if moveCursor, otherwise return list of row indexes.' def findMatchingColumn(sheet, row, columns, func): 'Find column for which func matches the displayed value in this row' for c in columns: if func(c.getDisplayValue(row)): return c vd.searchContext.update(kwargs) regex = kwargs.get("regex") if regex: vd.searchContext["regex"] = re.compile( regex, sheet.regex_flags()) or error('invalid regex: %s' % regex) regex = vd.searchContext.get("regex") or fail("no regex") columns = vd.searchContext.get("columns") if columns == "cursorCol": columns = [sheet.cursorCol] elif columns == "visibleCols": columns = tuple(sheet.visibleCols) elif isinstance(columns, Column): columns = [columns] if not columns: error('bad columns') searchBackward = vd.searchContext.get("backward") if reverse: searchBackward = not searchBackward matchingRowIndexes = 0 for rowidx in rotateRange(len(sheet.rows), sheet.cursorRowIndex, reverse=searchBackward): c = findMatchingColumn(sheet, sheet.rows[rowidx], columns, regex.search) if c: if moveCursor: sheet.cursorRowIndex = rowidx sheet.cursorVisibleColIndex = sheet.visibleCols.index(c) return else: matchingRowIndexes += 1 yield rowidx status('%s matches for /%s/' % (matchingRowIndexes, regex.pattern))
def input(self, prompt, type=None, defaultLast=False, history=[], **kwargs): '''Display prompt and return line of user input. type: list of previous items, or a string indicating the type of input. defaultLast: on empty input, if True, return last history item ''' if type: if isinstance(type, str): history = self.lastInputs[type] else: history = type sheet = self.sheets[0] rstatuslen = self.drawRightStatus(sheet._scr, sheet) attr = 0 promptlen = clipdraw(sheet._scr, sheet.windowHeight - 1, 0, prompt, attr, w=sheet.windowWidth - rstatuslen - 1) ret = self.editText(sheet.windowHeight - 1, promptlen, sheet.windowWidth - promptlen - rstatuslen - 2, attr=colors.color_edit_cell, unprintablechar=options.disp_unprintable, truncchar=options.disp_truncator, history=history, **kwargs) if ret: if isinstance(type, str): self.lastInputs[type].append(ret) elif defaultLast: history or fail("no previous input") ret = history[-1] return ret
def command(self): 'Return cmdline cmd+args (as list for Popen) to copy data to the system clipboard.' cmd = options.clipboard_copy_cmd or fail('options.clipboard_copy_cmd not set') return cmd.split()
def choose(choices, n=None): 'Return one of `choices` elements (if list) or values (if dict).' ret = chooseMany(choices) or fail('no choice made') if n and len(ret) > n: fail('can only choose %s' % n) return ret[0] if n == 1 else ret
def syscopyCells(sheet, col, rows): if not rows: fail('no %s selected' % sheet.rowtype) clipboard().copy("\n".join(col.getDisplayValue(r) for r in rows)) status('copied %s from %d %s to system clipboard' % (col.name, len(rows), sheet.rowtype))
def get_command(self, name): if name not in {'copy', 'paste'}: raise ValueError() name = 'clipboard_{}_cmd'.format(name) cmd = getattr(options, name) or fail('options.{} not set'.format(name)) return cmd.split()