Skip to content

Commit

Permalink
Add support for piping to stdout
Browse files Browse the repository at this point in the history
Not sure if this is actually the desired behavior, but this
will allow for things like:

  generate-json | jpterm | process-result-json

Once you find an expression you like you can just hit
"ctrl r" and once you exit it will pipe the result to the
next command.

Another option is just to support writing the output of the command
to a file.
  • Loading branch information
jamesls committed Jan 6, 2015
1 parent 840e573 commit 7e9682e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion jpterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(self, input_data):
self.lexer = pygments.lexers.get_lexer_by_name('json')
self.formatter = ConsoleJSONFormatter()
self.saved_expressions = []
self._latest_search_result = None
self._last_saved_search_result = None

def _create_colorized_json(self, json_string):
tokens = self.lexer.get_tokens(json_string)
Expand Down Expand Up @@ -119,6 +121,7 @@ def _on_edit(self, widget, text):
return
try:
result = jmespath.compile(text).search(self.parsed_json)
self._latest_search_result = result
self.footer.set_text("Status: success")
except Exception:
pass
Expand Down Expand Up @@ -149,10 +152,18 @@ def unhandled_input(self, key):
elif key == 'ctrl p':
self.saved_expressions.append(self.input_expr.edit_text)
self.footer.set_text("Status: expression saved")
elif key == 'ctrl r':
if self._latest_search_result is not None:
self._last_saved_search_result = self._latest_search_result
self.footer.set_text("Status: result saved")

def display_saved_expressions(self):
for expression in self.saved_expressions:
print(expression)
sys.__stdout__.write(expression)
sys.__stdout__.write('\n')
if self._last_saved_search_result is not None:
sys.__stdout__.write(json.dumps(self._last_saved_search_result, indent=2))
sys.__stdout__.write('\n')


def _load_input_json(filename):
Expand All @@ -173,6 +184,14 @@ def _load_input_json(filename):
return input_json


def switch_out_stdout():
if not os.isatty(sys.stdout.fileno()):
# If stdout is a pipe, then swap out the current stdout
# for the controlling tty. That way we can pipe
# the output of this command.
sys.stdout = open(os.ctermid(), 'w')


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input-json', nargs='?',
Expand All @@ -189,6 +208,7 @@ def main():
sys.stderr.write("Unable to load the input JSON: %s\n\n" % e)
return 1

switch_out_stdout()
screen = urwid.raw_display.Screen()
display = JMESPathDisplay(input_json)
display.main(screen=screen)
Expand Down

0 comments on commit 7e9682e

Please sign in to comment.