Skip to content

Commit

Permalink
feat(cli): color commits for "gas log"
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyong.yu committed Aug 5, 2021
1 parent 6cb8032 commit 9fb2934
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions tensorbay/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import bisect
from collections import defaultdict
from datetime import datetime
from functools import partial
from itertools import islice, zip_longest
from textwrap import indent
from typing import DefaultDict, Dict, Iterator, List, Optional, Type, Union
Expand All @@ -19,11 +20,15 @@
from .tbrn import TBRN, TBRNType
from .utility import error, get_gas, shorten

_FULL_LOG = """commit {}
Author: {}
Date: {}
_COLORS = {"yellow": partial(click.style, fg="yellow"), "green": partial(click.style, fg="green")}
_LEFT_BRACKET = _COLORS["yellow"]("(", reset=False)
_RIGHT_BRACKET = _COLORS["yellow"](")", reset=True)

{}
_FULL_LOG = f"""{_COLORS['yellow']("commit", reset=False)} {{}}
Author: {{}}
Date: {{}}
{{}}
"""


Expand Down Expand Up @@ -58,21 +63,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:
commit_id = shorten(commit.commit_id)
if branch_name:
commit_id = f"{commit_id} ({branch_name})"
def _combine_commit_id(commit_id: str, branch_names: List[str]) -> str:
branch_name = _COLORS["yellow"](", ", reset=False).join(
_COLORS["green"](name, reset=False) for name in branch_names
)
return f"{commit_id} {_LEFT_BRACKET}{branch_name}{_RIGHT_BRACKET}"


def _get_oneline_log(commit: Commit, branch_names: Optional[List[str]]) -> str:

commit_id = _COLORS["yellow"](shorten(commit.commit_id), reset=True)
if branch_names:
commit_id = _combine_commit_id(commit_id, branch_names)
return f"{commit_id} {commit.title}\n"


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})"
commit_id = _COLORS["yellow"](commit.commit_id, reset=True)
if branch_names:
commit_id = _combine_commit_id(commit_id, branch_names)
return _FULL_LOG.format(
commit_id,
commit.committer.name,
Expand Down Expand Up @@ -174,8 +187,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, None))


class _CommitNode: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -281,9 +293,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 +308,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 +390,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, None),
original_pointer,
)
yield self._graph_printer(current_node.commit, print_branch_name, original_pointer)

if not parent:
break
Expand Down

0 comments on commit 9fb2934

Please sign in to comment.