def setSubst(sheet, cols, rows): if not rows: warning('no %s selected' % sheet.rowtype) return modified = 'column' if len(cols) == 1 else 'columns' rex = vd.input("transform %s by regex: " % modified, type="regex-subst") setValuesFromRegex(cols, rows, rex)
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 confirm(vd, prompt, exc=EscapeException): yn = vd.input(prompt, value='no', record=False)[:1] if not yn or yn not in 'Yy': msg = 'disconfirmed: ' + prompt if exc: raise exc(msg) warning(msg) return False return True
def select_by_jmespath(sheet, unselect=False): action = "unselect" if unselect else "select" expr = vd.input( f"{action} by jmespath expression=", "jmespath-expr", completer=vd.CompleteExpr(sheet), ) match_func = partial(jmespath.search, expr) select_func = getattr(sheet, action) select_func(sheet.gatherBy(match_func), progress=False)
def chooseMany(choices): 'Return list of `choices` elements (if list) or values (if dict).' if isinstance(choices, dict): prompt = '/'.join(choices.keys()) chosen = [] for c in vd.input(prompt + ': ', completer=CompleteKey(choices)).split(): poss = [choices[p] for p in choices if str(p).startswith(c)] if not poss: warning('invalid choice "%s"' % c) else: chosen.extend(poss) else: prompt = '/'.join(str(x) for x in choices) chosen = [] for c in vd.input(prompt + ': ', completer=CompleteKey(choices)).split(): poss = [p for p in choices if str(p).startswith(c)] if not poss: warning('invalid choice "%s"' % c) else: chosen.extend(poss) return chosen
def addcol_jmespath(sheet): try: completer = vd.CompleteExpr except AttributeError: vd.debug("falling back to global for CompleteExpr") from visidata import CompleteExpr completer = CompleteExpr expr = vd.input( "new column jmespath expression=", "jmespath-expr", completer=completer(sheet), ) # Create a partial function based on the provided jmespath expression, # and add it as a sheet attribute. This is one way to avoid edge cases # when evaluating jmespath expressions that contain nested quotes. partial_attr = f"_jmespath_search_{sha1(expr.encode('utf8')).hexdigest()}" setattr(sheet, partial_attr, partial(jmespath.search, expr)) sheet.addColumnAtCursor( ExprColumn(expr, expr=f"sheet.{partial_attr}(row)", curcol=sheet.cursorCol) )
def inputExpr(self, prompt, *args, **kwargs): return vd.input(prompt, "expr", *args, completer=CompleteExpr(self), **kwargs)
def pageValueWith(col, row): pager = vd.input("external command: ", type="pager") col.pageValue(row, pager)