Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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)
    )
Ejemplo n.º 7
0
def inputExpr(self, prompt, *args, **kwargs):
    return vd.input(prompt,
                    "expr",
                    *args,
                    completer=CompleteExpr(self),
                    **kwargs)
Ejemplo n.º 8
0
def pageValueWith(col, row):
    pager = vd.input("external command: ", type="pager")
    col.pageValue(row, pager)