Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 71532ce

Browse files
committed
New option --no-color to disable colored output
Also disable color if stdout isn't connected to a tty.
1 parent 79c9f5c commit 71532ce

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ TODOS and Possible Features
255255
folder linters.
256256
* Decide what linter to use based on the whole filename or even in the filetype,
257257
as returned by the command file.
258-
* Provide better options for colorizing the output, and maybe a way to disable
259-
it. Also detect if colors are supported or if it is a tty.
258+
* Provide better options for colorizing the output.
260259
* Add support for more version control systems (svn, perforce). This should be
261260
easy, it's just a matter of implementing the functions defined in
262261
gitlint/git.py or gitlint/hg.py.

gitlint/__init__.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
among others. See https://github.com/sk-/git-lint for the complete list.
2323
2424
Usage:
25-
git-lint [-f | --force] [--json] [--last-commit] [FILENAME ...]
26-
git-lint [-t | --tracked] [-f | --force] [--json] [--last-commit]
27-
git-lint -h | --version
25+
git-lint [-f|--force] [--no-color] [--json] [--last-commit] [FILENAME ...]
26+
git-lint [-t|--tracked] [-f|--force] [--no-color] [--json] [--last-commit]
27+
git-lint -h|--version
2828
2929
Options:
3030
-h Show the usage patterns.
3131
--version Prints the version number.
32+
--no-color Disable colored output.
3233
-f --force Shows all the lines with problems.
3334
-t --tracked Lints only tracked files.
3435
--json Prints the result as a json string. Useful to use it in
@@ -57,10 +58,6 @@
5758
import gitlint.linters as linters
5859
from gitlint.version import __VERSION__
5960

60-
ERROR = termcolor.colored('ERROR', 'red', attrs=('bold', ))
61-
SKIPPED = termcolor.colored('SKIPPED', 'yellow', attrs=('bold', ))
62-
OK = termcolor.colored('OK', 'green', attrs=('bold', ))
63-
6461

6562
def find_invalid_filenames(filenames, repository_root):
6663
"""Find files that does not exist, are not in the repo or are directories.
@@ -202,6 +199,16 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
202199

203200
vcs, repository_root = get_vcs_root()
204201

202+
error = 'ERROR'
203+
skipped = 'SKIPPED'
204+
okay = 'OK'
205+
206+
color = not arguments['--no-color'] and sys.stdout.isatty()
207+
if color:
208+
error = termcolor.colored(error, 'red', attrs=('bold', ))
209+
skipped = termcolor.colored(skipped, 'yellow', attrs=('bold', ))
210+
okay = termcolor.colored(okay, 'green', attrs=('bold', ))
211+
205212
if vcs is None:
206213
stderr.write('fatal: Not a git repository' + linesep)
207214
return 128
@@ -250,20 +257,22 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
250257
rel_filename = os.path.relpath(filename)
251258

252259
if not json_output:
253-
stdout.write('Linting file: %s%s' % (termcolor.colored(
254-
rel_filename, attrs=('bold', )), linesep))
260+
filename = rel_filename
261+
if color:
262+
filename = termcolor.colored(filename, attrs=('bold', ))
263+
stdout.write('Linting file: %s%s' % (filename, linesep))
255264

256265
output_lines = []
257266
if result.get('error'):
258-
output_lines.extend('%s: %s' % (ERROR, reason)
267+
output_lines.extend('%s: %s' % (error, reason)
259268
for reason in result.get('error'))
260269
linter_not_found = True
261270
if result.get('skipped'):
262-
output_lines.extend('%s: %s' % (SKIPPED, reason)
271+
output_lines.extend('%s: %s' % (skipped, reason)
263272
for reason in result.get('skipped'))
264273
if not result.get('comments', []):
265274
if not output_lines:
266-
output_lines.append(OK)
275+
output_lines.append(okay)
267276
else:
268277
files_with_problems += 1
269278
for data in result['comments']:

0 commit comments

Comments
 (0)