Skip to content

Commit 62bd406

Browse files
committed
Colorize help output
1 parent 9afbcb4 commit 62bd406

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

git_delete_merged_branches/__main__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under GPL v3 or later
33

44
import argparse
5+
import os
56
import re
67
import sys
78
import traceback
@@ -13,6 +14,9 @@
1314
from textwrap import dedent
1415
from typing import List, Set
1516

17+
import colorama
18+
19+
from ._argparse_color import add_color_to_formatter_class
1620
from ._confirm import Confirmation
1721
from ._git import Git
1822
from ._metadata import APP, DESCRIPTION, VERSION
@@ -246,9 +250,15 @@ def _parse_command_line():
246250
Please report bugs at https://github.com/hartwork/{APP}. Thank you!
247251
""")
248252

253+
colorize = 'NO_COLOR' not in os.environ
254+
formatter_class = RawDescriptionHelpFormatter
255+
if colorize:
256+
colorama.init()
257+
formatter_class = add_color_to_formatter_class(formatter_class)
258+
249259
parser = argparse.ArgumentParser(prog='git-delete-merged-branches', add_help=False,
250260
description=DESCRIPTION, epilog=_EPILOG,
251-
formatter_class=RawDescriptionHelpFormatter)
261+
formatter_class=formatter_class)
252262

253263
modes = parser.add_argument_group('modes').add_mutually_exclusive_group()
254264
modes.add_argument('--configure', dest='force_reconfiguration', action='store_true',
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (C) 2020 Sebastian Pipping <sebastian@pipping.org>
2+
# Licensed under GPL v3 or later
3+
4+
import re
5+
6+
import colorama
7+
8+
_SECTION_COLOR = colorama.Fore.WHITE + colorama.Style.BRIGHT
9+
_ARGUMENT_COLOR = colorama.Fore.GREEN + colorama.Style.BRIGHT
10+
_PARAMETER_COLOR = colorama.Fore.GREEN
11+
_PROG_COLOR = colorama.Fore.CYAN + colorama.Style.BRIGHT
12+
_URL_COLOR = colorama.Fore.MAGENTA + colorama.Style.BRIGHT
13+
_RESET_COLOR = colorama.Style.RESET_ALL
14+
15+
_SUBSTITUTIONS = (
16+
('^(.+):$', f'{_SECTION_COLOR}\\1{_RESET_COLOR}:'),
17+
('(?<!\\w)(--?[a-z-]+) ([A-Z_]+)',
18+
f'{_ARGUMENT_COLOR}\\1{_RESET_COLOR} {_PARAMETER_COLOR}\\2{_RESET_COLOR}'),
19+
('(?<!\\w)(--?[a-z-]+)', f'{_ARGUMENT_COLOR}\\1{_RESET_COLOR}'),
20+
('^(usage): ([^ ]+)\\b',
21+
f'{_SECTION_COLOR}\\1{_RESET_COLOR}: {_PROG_COLOR}\\2{_RESET_COLOR}'),
22+
('(https://[^ ]+[^ .])', f'{_URL_COLOR}\\1{_RESET_COLOR}'),
23+
)
24+
25+
26+
def add_color_to_formatter_class(formatter_class):
27+
class_name = formatter_class.__name__.replace('Formatter', 'ColorFormatter')
28+
29+
class Class(formatter_class):
30+
def format_help(self):
31+
text = super().format_help()
32+
for pattern, replacement in _SUBSTITUTIONS:
33+
matcher = re.compile(pattern, flags=re.MULTILINE)
34+
text = matcher.sub(replacement, text)
35+
return text
36+
37+
Class.__name__ = class_name
38+
39+
return Class

0 commit comments

Comments
 (0)