mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Move logic to handle terminal not supporting colors to use_color
This commit is contained in:
parent
a970d3b69b
commit
3d777bb386
1 changed files with 7 additions and 4 deletions
|
|
@ -3,13 +3,13 @@ from __future__ import unicode_literals
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
terminal_supports_colors = True
|
terminal_supports_color = True
|
||||||
if os.name == 'nt': # pragma: no cover (windows)
|
if os.name == 'nt': # pragma: no cover (windows)
|
||||||
from pre_commit.color_windows import enable_virtual_terminal_processing
|
from pre_commit.color_windows import enable_virtual_terminal_processing
|
||||||
try:
|
try:
|
||||||
enable_virtual_terminal_processing()
|
enable_virtual_terminal_processing()
|
||||||
except WindowsError:
|
except WindowsError:
|
||||||
terminal_supports_colors = False
|
terminal_supports_color = False
|
||||||
|
|
||||||
RED = '\033[41m'
|
RED = '\033[41m'
|
||||||
GREEN = '\033[42m'
|
GREEN = '\033[42m'
|
||||||
|
|
@ -30,7 +30,7 @@ def format_color(text, color, use_color_setting):
|
||||||
color - The color start string
|
color - The color start string
|
||||||
use_color_setting - Whether or not to color
|
use_color_setting - Whether or not to color
|
||||||
"""
|
"""
|
||||||
if not use_color_setting or not terminal_supports_colors:
|
if not use_color_setting:
|
||||||
return text
|
return text
|
||||||
else:
|
else:
|
||||||
return '{}{}{}'.format(color, text, NORMAL)
|
return '{}{}{}'.format(color, text, NORMAL)
|
||||||
|
|
@ -48,4 +48,7 @@ def use_color(setting):
|
||||||
if setting not in COLOR_CHOICES:
|
if setting not in COLOR_CHOICES:
|
||||||
raise InvalidColorSetting(setting)
|
raise InvalidColorSetting(setting)
|
||||||
|
|
||||||
return setting == 'always' or (setting == 'auto' and sys.stdout.isatty())
|
return (
|
||||||
|
setting == 'always' or
|
||||||
|
(setting == 'auto' and sys.stdout.isatty() and terminal_supports_color)
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue