Skip to content

Commit

Permalink
add bang operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tjfontaine committed Sep 22, 2015
1 parent 7c88ba1 commit d04a1ae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Number formatting:
* paged output
* repeat expressions
* pipelined commands
* bang operator (redirect output to shell pipeline)
- `::nm ! grep foobar | less`

## Commands

Expand All @@ -62,7 +64,6 @@ Number formatting:

## TODO

* bang operator
* semi-colon parsing
* dot expressions
* format expressions (i.e. `0xffffff/nap`)
Expand Down
54 changes: 45 additions & 9 deletions src/llmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
import cmd, shlex
import pipes
import re

from subprocess import Popen, PIPE

Expand Down Expand Up @@ -30,16 +32,36 @@ def precmd(self, line):

arguments = []
cur = []

for a in raw_args:
if a == '|':
arguments.append(cur)
cur = []
continue
cur.append(a)
shell_arguments = None

for arg in raw_args:
## shlex.split doesn't handle unspaced pipes, but will preserve spaces in
## args, so assume that foo!bar|bash -c "grep foo|sed|awk"
## TODO XXX bash -c "noarg|noarg2" wouldn't be handled either.
if re.search('\s', arg) is not None:
dargs = [arg]
else:
dargs = re.split('([|!])', arg)

for a in dargs:
if not a:
continue
if shell_arguments is None and a == '|':
arguments.append(cur)
cur = []
elif shell_arguments is None and a == '!':
if len(cur) > 0:
arguments.append(cur)
shell_arguments = []
cur = []
else:
cur.append(a)

if len(cur) > 0:
arguments.append(cur)
if shell_arguments is not None:
shell_arguments.extend(cur)
else:
arguments.append(cur)

steps = []
stepsCount = len(arguments)
Expand Down Expand Up @@ -104,7 +126,21 @@ def precmd(self, line):

## TODO XXX only page on ttys
## TODO XXX respect $PAGER
pager = Popen(['less', '-F', '-R', '-S', '-X', '-K',],
if shell_arguments is None:
shell_arguments = ['less', '-F', '-R', '-S', '-X', '-K']
use_shell = False
else:
use_shell = True
sargs = []
for s in shell_arguments:
if s is not '|':
sargs.append(pipes.quote(s))
else:
sargs.append(s)
shell_arguments = ' '.join(sargs)

pager = Popen(shell_arguments,
shell=use_shell,
stdin=PIPE,
stdout=sys.stdout,
stderr=sys.stderr)
Expand Down

0 comments on commit d04a1ae

Please sign in to comment.