Skip to content

Commit

Permalink
Other: show_branch_panel
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Jul 20, 2017
1 parent d7e3d15 commit ee3760c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 79 deletions.
33 changes: 10 additions & 23 deletions core/commands/commit_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ...common import util
from ..git_command import GitCommand
from ..ui_mixins.quick_panel import PanelActionMixin
from ..ui_mixins.quick_panel import PanelActionMixin, show_branch_panel


COMMIT_NODE_CHAR = "●"
Expand Down Expand Up @@ -146,35 +146,22 @@ def run(self, base_commit=None, target_commit=None, file_path=None):
self._target_commit = target_commit
sublime.set_timeout_async(self.run_async)

def run_async(self):
self.all_branches = [b.name_with_remote for b in self.get_branches()]
def run_async(self, **kwargs):
show_branch_panel(self.on_branch_selection)

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
def on_branch_selection(self, branch):
if branch:
self.window.run_command("gs_compare_commit", {
"file_path": self._file_path,
"base_commit": self._base_commit if self._base_commit else branch,
"target_commit": self._target_commit if self._target_commit else branch
})
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index,
)

def on_branch_selection(self, index):
if index == -1:
self.window.run_command("gs_compare_against", {
"base_commit": self._base_commit,
"target_commit": self._target_commit,
"file_path": self._file_path
})
return
selected_branch = self.all_branches[index]
self.window.run_command("gs_compare_commit", {
"file_path": self._file_path,
"base_commit": self._base_commit if self._base_commit else selected_branch,
"target_commit": self._target_commit if self._target_commit else selected_branch
})


class GsCompareAgainstCommand(PanelActionMixin, WindowCommand, GitCommand):
Expand Down
24 changes: 6 additions & 18 deletions core/commands/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from ...common import util
from ..git_command import GitCommand
from ..ui_mixins.quick_panel import PanelActionMixin, PanelCommandMixin, show_log_panel
from ..ui_mixins.quick_panel import PanelActionMixin, PanelCommandMixin
from ..ui_mixins.quick_panel import show_log_panel, show_branch_panel


class LogMixin(object):
Expand Down Expand Up @@ -99,24 +100,11 @@ def log(self, **kwargs):
class GsLogByBranchCommand(LogMixin, WindowCommand, GitCommand):

def run_async(self, **kwargs):
self.all_branches = [b.name_with_remote for b in self.get_branches()]
show_branch_panel(self.on_branch_selection)

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)

def on_branch_selection(self, index):
if index == -1:
return
super().run_async(branch=self.all_branches[index])
def on_branch_selection(self, branch):
if branch:
super().run_async(branch=branch)


class GsLogCommand(PanelCommandMixin, WindowCommand, GitCommand):
Expand Down
29 changes: 9 additions & 20 deletions core/commands/log_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from .log import GsLogActionCommand, GsLogCommand
from .navigate import GsNavigate
from ...common import util
from ..ui_mixins.quick_panel import show_branch_panel


COMMIT_NODE_CHAR = "●"
COMMIT_NODE_CHAR_OPTIONS = "●*"
Expand Down Expand Up @@ -95,7 +97,7 @@ class GsLogGraphByAuthorCommand(LogGraphMixin, WindowCommand, GitCommand):
by the specified author.
"""

def run_async(self):
def run_async(self, **kwargs):
email = self.git("config", "user.email").strip()
self._entries = []

Expand Down Expand Up @@ -129,26 +131,13 @@ def get_graph_args(self):

class GsLogGraphByBranchCommand(LogGraphMixin, WindowCommand, GitCommand):

def run_async(self):
self.all_branches = [b.name_with_remote for b in self.get_branches()]

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)
def run_async(self, **kwargs):
show_branch_panel(self.on_branch_selection)

def on_branch_selection(self, index):
if index == -1:
return
self._selected_branch = self.all_branches[index]
super().run_async()
def on_branch_selection(self, branch):
if branch:
self._selected_branch = branch
super().run_async()

def get_graph_args(self):
args = super().get_graph_args()
Expand Down
23 changes: 5 additions & 18 deletions core/commands/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .log import LogMixin
from .reflog import RefLogMixin
from ...common import util
from ..ui_mixins.quick_panel import show_branch_panel


PADDING = " "
Expand Down Expand Up @@ -63,25 +64,11 @@ class GsResetCommand(ResetMixin, LogMixin, WindowCommand, GitCommand):
class GsResetBranch(ResetMixin, LogMixin, WindowCommand, GitCommand):

def run_async(self, **kwargs):
self.all_branches = [b.name_with_remote for b in self.get_branches()]
show_branch_panel(self.on_branch_selection)

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)

def on_branch_selection(self, index):
if index == -1:
return
self._selected_branch = self.all_branches[index]
self.do_action(self._selected_branch)
def on_branch_selection(self, branch):
if branch:
self.do_action(branch)


class GsResetReflogCommand(ResetMixin, RefLogMixin, WindowCommand, GitCommand):
Expand Down
48 changes: 48 additions & 0 deletions core/ui_mixins/quick_panel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import itertools
import sublime
from ...common import util
from ..git_command import GitCommand


class PanelActionMixin(object):
Expand Down Expand Up @@ -296,3 +297,50 @@ def on_selection(self, commit):
def on_selection_async(self, commit):
sublime.active_window().run_command("hide_panel", {"panel": "output.show_commit_info"})
self.on_done(commit)


def show_branch_panel(on_done):
"""
Display log entries in quick panel with pagination, and execute on_done(commit)
when item is selected. `entries` can be either a list or a generator of LogEnty.
"""
bp = BranchPanel(on_done)
bp.show()
return bp


class BranchPanel(GitCommand):

def __init__(
self, on_done, remote=None, local_banches_only=True, remote_banches_only=False,
selected_branch=None):
self.window = sublime.active_window()
self.on_done = on_done
self.remote = remote
self.local_banches_only = local_banches_only
self.remote_banches_only = remote_banches_only
self.selected_branch = selected_branch

def show(self):
self.all_branches = [b.name_with_remote for b in self.get_branches()]

if self.selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self.branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)

def on_branch_selection(self, index):
if index == -1:
self.branch = None
else:
self.branch = self.all_branches[index]

self.on_done(self.branch)

0 comments on commit ee3760c

Please sign in to comment.