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

New option --no-color to disable colored output #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ TODOS and Possible Features
folder linters.
* Decide what linter to use based on the whole filename or even in the filetype,
as returned by the command file.
* Provide better options for colorizing the output, and maybe a way to disable
it. Also detect if colors are supported or if it is a tty.
* Provide better options for colorizing the output.
* Add support for more version control systems (svn, perforce). This should be
easy, it's just a matter of implementing the functions defined in
gitlint/git.py or gitlint/hg.py.
Expand Down
33 changes: 21 additions & 12 deletions gitlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
among others. See https://github.com/sk-/git-lint for the complete list.

Usage:
git-lint [-f | --force] [--json] [--last-commit] [FILENAME ...]
git-lint [-t | --tracked] [-f | --force] [--json] [--last-commit]
git-lint -h | --version
git-lint [-f|--force] [--no-color] [--json] [--last-commit] [FILENAME ...]
git-lint [-t|--tracked] [-f|--force] [--no-color] [--json] [--last-commit]
git-lint -h|--version

Options:
-h Show the usage patterns.
--version Prints the version number.
--no-color Disable colored output.
-f --force Shows all the lines with problems.
-t --tracked Lints only tracked files.
--json Prints the result as a json string. Useful to use it in
Expand Down Expand Up @@ -57,10 +58,6 @@
import gitlint.linters as linters
from gitlint.version import __VERSION__

ERROR = termcolor.colored('ERROR', 'red', attrs=('bold', ))
SKIPPED = termcolor.colored('SKIPPED', 'yellow', attrs=('bold', ))
OK = termcolor.colored('OK', 'green', attrs=('bold', ))


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

vcs, repository_root = get_vcs_root()

error = 'ERROR'
skipped = 'SKIPPED'
okay = 'OK'

color = not arguments['--no-color'] and sys.stdout.isatty()
if color:
error = termcolor.colored(error, 'red', attrs=('bold', ))
skipped = termcolor.colored(skipped, 'yellow', attrs=('bold', ))
okay = termcolor.colored(okay, 'green', attrs=('bold', ))

if vcs is None:
stderr.write('fatal: Not a git repository' + linesep)
return 128
Expand Down Expand Up @@ -250,20 +257,22 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
rel_filename = os.path.relpath(filename)

if not json_output:
stdout.write('Linting file: %s%s' % (termcolor.colored(
rel_filename, attrs=('bold', )), linesep))
filename = rel_filename
if color:
filename = termcolor.colored(filename, attrs=('bold', ))
stdout.write('Linting file: %s%s' % (filename, linesep))

output_lines = []
if result.get('error'):
output_lines.extend('%s: %s' % (ERROR, reason)
output_lines.extend('%s: %s' % (error, reason)
for reason in result.get('error'))
linter_not_found = True
if result.get('skipped'):
output_lines.extend('%s: %s' % (SKIPPED, reason)
output_lines.extend('%s: %s' % (skipped, reason)
for reason in result.get('skipped'))
if not result.get('comments', []):
if not output_lines:
output_lines.append(OK)
output_lines.append(okay)
else:
files_with_problems += 1
for data in result['comments']:
Expand Down
8 changes: 4 additions & 4 deletions test/unittest/test_gitlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ def test_main_errors_skipped_comments(self):
stdout=self.stdout,
stderr=None))
expected_output = os.linesep.join([
'\x1b[1m\x1b[31mERROR\x1b[0m: error1',
'\x1b[1m\x1b[31mERROR\x1b[0m: error2',
'\x1b[1m\x1b[33mSKIPPED\x1b[0m: skipped1',
'\x1b[1m\x1b[33mSKIPPED\x1b[0m: skipped2',
'ERROR: error1',
'ERROR: error2',
'SKIPPED: skipped1',
'SKIPPED: skipped2',
'msg1',
'msg2',
])
Expand Down