Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): color commits for "gas log" #892

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions tensorbay/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
from .tbrn import TBRN, TBRNType
from .utility import error, get_gas, shorten

_FULL_LOG = """commit {}
Author: {}
Date: {}
_LEFT_BRACKET = click.style("(", fg="yellow", reset=False)
_COMMA = click.style(", ", fg="yellow", reset=False)
_RIGHT_BRACKET = click.style(")", fg="yellow", reset=True)

{}
_FULL_LOG = f"""{click.style("commit {}", fg="yellow")}
Author: {{}}
Date: {{}}

{{}}
yuyouyu32 marked this conversation as resolved.
Show resolved Hide resolved
"""

_ONELINE_LOG = f"""{click.style("{}", fg="yellow")} {{}}
"""


Expand Down Expand Up @@ -58,21 +65,29 @@ def _implement_log( # pylint: disable=too-many-arguments
click.echo_via_pager(islice(printer.generate_commits(), max_count))


def _get_oneline_log(commit: Commit, branch_name: Optional[str]) -> str:
def _join_branch_names(commit_id: str, branch_names: List[str]) -> str:
return (
f"{commit_id} {_LEFT_BRACKET}"
f"{_COMMA.join(click.style(name, fg='green', reset=False) for name in branch_names)}"
f"{_RIGHT_BRACKET}"
)


def _get_oneline_log(commit: Commit, branch_names: Optional[List[str]]) -> str:
commit_id = shorten(commit.commit_id)
if branch_name:
commit_id = f"{commit_id} ({branch_name})"
return f"{commit_id} {commit.title}\n"
if branch_names:
commit_id = _join_branch_names(commit_id, branch_names)
return _ONELINE_LOG.format(commit_id, commit.title)


def _get_full_log(commit: Commit, branch_name: Optional[str]) -> str:
def _get_full_log(commit: Commit, branch_names: Optional[List[str]]) -> str:
description = commit.description
if description:
description = f"\n\n{indent(description, INDENT)}"
commit_message = f"{commit.title}{description}\n"
commit_id = commit.commit_id
if branch_name:
commit_id = f"{commit_id} ({branch_name})"
if branch_names:
commit_id = _join_branch_names(commit_id, branch_names)
return _FULL_LOG.format(
commit_id,
commit.committer.name,
Expand Down Expand Up @@ -174,8 +189,7 @@ def generate_commits(self) -> Iterator[str]:
"""
for commit in self._generate_commits():
commit_id = commit.commit_id
print_branch_name = ", ".join(self._commit_id_to_branches.get(commit_id, ()))
yield self._printer(commit, print_branch_name)
yield self._printer(commit, self._commit_id_to_branches.get(commit_id))


class _CommitNode: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -281,9 +295,9 @@ def _set_next_node(self, node: _CommitNode) -> None:
self._sorted_leaves[self._pointer] = node

def _add_graph_oneline(
self, commit: Commit, branch_name: Optional[str], original_pointer: int
self, commit: Commit, branch_names: Optional[List[str]], original_pointer: int
) -> str:
log = _get_oneline_log(commit, branch_name)
log = _get_oneline_log(commit, branch_names)
prefixes = ["|"] * self._layer
# Don't merge branches.
if self._merge_pointer is None:
Expand All @@ -296,9 +310,9 @@ def _add_graph_oneline(
return "".join(lines)

def _add_graph_full(
self, commit: Commit, branch_name: Optional[str], original_pointer: int
self, commit: Commit, branch_names: Optional[List[str]], original_pointer: int
) -> str:
log = _get_full_log(commit, branch_name)
log = _get_full_log(commit, branch_names)
splitlines = iter(log.splitlines())
prefixes = ["|"] * self._layer
# Don't merge branches.
Expand Down Expand Up @@ -378,11 +392,11 @@ def generate_commits(self) -> Iterator[str]:
parent = current_node.parent
# Merge branch.
original_pointer = self._merge_branches(parent)

print_branch_name = ", ".join(
self._commit_id_to_branches.get(current_node.commit.commit_id, ())
yield self._graph_printer(
current_node.commit,
self._commit_id_to_branches.get(current_node.commit.commit_id),
original_pointer,
)
yield self._graph_printer(current_node.commit, print_branch_name, original_pointer)

if not parent:
break
Expand Down