From a23bf5951d2802b06f18ce4d468649c8d3598a48 Mon Sep 17 00:00:00 2001 From: Daniel McArdle Date: Tue, 12 Feb 2019 00:25:12 +0000 Subject: [PATCH] Colorize the current branch in 'git cl status' Change-Id: I5cdb484ae4b6829aeeb83924fb464939cdb4298b Reviewed-on: https://chromium-review.googlesource.com/c/1464776 Reviewed-by: Edward Lesmes Commit-Queue: Daniel McArdle --- git_cl.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/git_cl.py b/git_cl.py index 549aae25c..c49d6581a 100755 --- a/git_cl.py +++ b/git_cl.py @@ -3624,7 +3624,6 @@ def CMDbaseurl(parser, args): return RunGit(['config', 'branch.%s.base-url' % branch, args[0]], error_ok=False).strip() - def color_for_status(status): """Maps a Changelist status to color, for CMDstatus and other tools.""" return { @@ -3944,8 +3943,27 @@ def CMDstatus(parser, args): fine_grained=not options.fast, max_processes=options.maxjobs) + current_branch = GetCurrentBranch() + + def FormatBranchName(branch, colorize=False): + """Simulates 'git branch' behavior. Colorizes and prefixes branch name with + an asterisk when it is the current branch.""" + + asterisk = "" + color = Fore.RESET + if branch == current_branch: + asterisk = "* " + color = Fore.GREEN + branch_name = ShortBranchName(branch) + + if colorize: + return asterisk + color + branch_name + Fore.RESET + else: + return branch_name + branch_statuses = {} - alignment = max(5, max(len(ShortBranchName(c.GetBranch())) for c in changes)) + + alignment = max(5, max(len(FormatBranchName(c.GetBranch())) for c in changes)) for cl in sorted(changes, key=lambda c: c.GetBranch()): branch = cl.GetBranch() while branch not in branch_statuses: @@ -3964,15 +3982,14 @@ def CMDstatus(parser, args): reset = '' status_str = '(%s)' % status if status else '' print(' %*s : %s%s %s%s' % ( - alignment, ShortBranchName(branch), color, url, + alignment, FormatBranchName(branch, colorize=True), color, url, status_str, reset)) - branch = GetCurrentBranch() print() - print('Current branch: %s' % branch) + print('Current branch: %s' % current_branch) for cl in changes: - if cl.GetBranch() == branch: + if cl.GetBranch() == current_branch: break if not cl.GetIssue(): print('No issue assigned.')