Skip to content

Commit

Permalink
Merge c871124 into 6fdaf3d
Browse files Browse the repository at this point in the history
  • Loading branch information
tomv564 authored Nov 23, 2017
2 parents 6fdaf3d + c871124 commit d3b1bfc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
11 changes: 11 additions & 0 deletions GitSavvy.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
*/
"close_panel_for": [],

/*
Whether git and githook output is logged live
If false, results are shown after execution.
*/
"live_panel_output": true,

/*
Use the Sublime configured syntax for COMMIT_EDITMSG rather than
the custom bundled syntax that comes with GitSavvy.
Expand All @@ -114,6 +120,11 @@
*/
"show_input_in_output": true,

/*
Change this to `true` to print the stdin in the panel output.
*/
"show_stdin_in_output": false,

/*
Change this to `false` to suppress Git status in ST3 status bar.
*/
Expand Down
19 changes: 19 additions & 0 deletions common/commands/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ def run(self, edit, msg=""):
panel_view.set_read_only(True)
panel_view.show(0)
self.view.window().run_command("show_panel", {"panel": "output.{}".format(PANEL_NAME)})


class GsAppendPanelCommand(TextCommand):

"""
Given a `msg` string, find a transient text panel at the bottom of the
active window, and append the `msg` contents there.
"""

def run(self, edit, msg=""):
panel_view = self.view.window().find_output_panel(PANEL_NAME)
if panel_view:
panel_view.set_read_only(False)
panel_view.run_command(
'append',
{'characters': msg, 'force': True, 'scroll_to_end': True})
panel_view.set_read_only(True)
else:
print("panel not found")
5 changes: 5 additions & 0 deletions common/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
def panel(*msgs):
msg = "\n".join(str(msg) for msg in msgs)
sublime.active_window().active_view().run_command("gs_display_panel", {"msg": msg})


def panel_append(*msgs):
msg = "\n".join(str(msg) for msg in msgs)
sublime.active_window().active_view().run_command("gs_append_panel", {"msg": msg})
79 changes: 72 additions & 7 deletions core/git_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import subprocess
import shutil
from contextlib import contextmanager
import threading

import sublime

Expand Down Expand Up @@ -45,6 +46,54 @@
)


class LoggingProcessWrapper(object):

"""
Wraps a Popen object with support for logging stdin/stderr
"""
def __init__(self, process):
self.process = process
self.stdout = b''
self.stderr = b''

def read_stdout(self):
try:
for line in self.process.stdout:
self.stdout = self.stdout + line
util.log.panel_append(line.decode())
except IOError as err:
util.log.panel_append(err)

def read_stderr(self):
try:
for line in self.process.stderr:
self.stderr = self.stderr + line
util.log.panel_append(line.decode())
except IOError as err:
util.log.panel_append(err)

def communicate(self, stdin):
"""
Emulates Popen.communicate
Writes stdin (if provided)
Logs output from both stdout and stderr
Returns stdout, stderr
"""
if stdin is not None:
self.process.stdin.write(stdin)
self.process.stdin.flush()
self.process.stdin.close()

stdout_thread = threading.Thread(target=self.read_stdout)
stdout_thread.start()
stderr_thread = threading.Thread(target=self.read_stderr)
stderr_thread.start()

self.process.wait()

return self.stdout, self.stderr


class GitCommand(StatusMixin,
ActiveBranchMixin,
BranchesMixin,
Expand Down Expand Up @@ -96,6 +145,8 @@ def git(self, *args,
if args[0] in close_panel_for:
sublime.active_window().run_command("hide_panel", {"cancel": True})

live_panel_output = savvy_settings.get("live_panel_output") or False

stdout, stderr = None, None

try:
Expand Down Expand Up @@ -127,8 +178,27 @@ def git(self, *args,
cwd=working_dir,
env=environ,
startupinfo=startupinfo)
stdout, stderr = p.communicate(
(stdin.encode(encoding=stdin_encoding) if encode else stdin) if stdin else None)

original_stdin = stdin
if stdin is not None and encode:
stdin = stdin.encode(encoding=stdin_encoding)

if show_panel and live_panel_output:
util.log.panel("")
if savvy_settings.get("show_stdin_in_output") and stdin is not None:
util.log.panel_append("STDIN\n{}\n".format(original_stdin))
if savvy_settings.get("show_input_in_output"):
util.log.panel_append("> {}\n\n".format(command_str))
wrapper = LoggingProcessWrapper(p)
stdout, stderr = wrapper.communicate(stdin)
else:
stdout, stderr = p.communicate(stdin)
if show_panel:
if savvy_settings.get("show_input_in_output"):
util.log.panel("> {}\n{}\n{}".format(command_str, stdout, stderr))
else:
util.log.panel("{}\n{}".format(stdout, stderr))

if decode:
stdout, stderr = self.decode_stdout(stdout, savvy_settings), stderr.decode()

Expand Down Expand Up @@ -165,11 +235,6 @@ def git(self, *args,
else:
raise GitSavvyError("`{}` failed.".format(command_str))

if show_panel:
if savvy_settings.get("show_input_in_output"):
util.log.panel("> {}\n{}\n{}".format(command_str, stdout, stderr))
else:
util.log.panel("{}\n{}".format(stdout, stderr))

return stdout

Expand Down

0 comments on commit d3b1bfc

Please sign in to comment.