Skip to content

Commit

Permalink
Update colored from 1.3.5 to 1.4.3
Browse files Browse the repository at this point in the history
The repository has been moved from GitHub to GitLab:
https://gitlab.com/dslackw/colored/
  • Loading branch information
DimitriPapadopoulos committed Dec 16, 2021
1 parent aee6274 commit a5a00ad
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 428 deletions.
25 changes: 2 additions & 23 deletions rampwf/externals/colored/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__init__.py is a part of colored.
Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
All rights reserved.
Colored is very simple Python library for color and formatting in terminal.
https://github.com/dslackw/colored
colored is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from __future__ import print_function

Expand All @@ -30,5 +9,5 @@
from .back import *
from .style import *

__version_info__ = (1, 3, 5)
__version_info__ = (1, 4, 3)
__version__ = '{0}.{1}.{2}'.format(*__version_info__)
24 changes: 1 addition & 23 deletions rampwf/externals/colored/back.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
back.py is a part of colored.
Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
All rights reserved.
Colored is very simple Python library for color and formatting in terminal.
https://github.com/dslackw/colored
colored is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


from .colors import names

Expand Down
123 changes: 90 additions & 33 deletions rampwf/externals/colored/colored.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
colored.py is a part of colored.
import os
import platform

Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
All rights reserved.
Colored is very simple Python library for color and formatting in terminal.
https://github.com/dslackw/colored
colored is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
_ _
___ ___ | | ___ _ __ ___ __| |
/ __/ _ \| |/ _ \| "__/ _ \/ _` |
| (_| (_) | | (_) | | | __/ (_| |
\___\___/|_|\___/|_| \___|\__,_|
from .hex import HEX
import sys

Very simple Python library for color and formatting in terminal.
Collection of color codes and names for 256 color terminal setups.
The following is a list of 256 colors for Xterm, containing an example
of the displayed color, Xterm Name, Xterm Number.
"""

TTY_AWARE = True
IS_TTY = sys.stdout.isatty() and sys.stderr.isatty()

from .hex import HEX

_win_vterm_mode = None

class colored(object):

Expand All @@ -44,6 +20,7 @@ def __init__(self, color):
self.ESC = "\x1b["
self.END = "m"
self.color = color
self.enable_windows_terminal_mode()

if str(color).startswith("#"):
self.HEX = HEX(color.lower())
Expand Down Expand Up @@ -311,6 +288,8 @@ def __init__(self, color):

def attribute(self):
"""Set or reset attributes"""
if not self.enabled():
return ""

paint = {
"bold": self.ESC + "1" + self.END,
Expand Down Expand Up @@ -344,6 +323,8 @@ def attribute(self):

def foreground(self):
"""Print 256 foreground colors"""
if not self.enabled():
return ""
code = self.ESC + "38;5;"
if str(self.color).isdigit():
self.reverse_dict()
Expand All @@ -356,6 +337,8 @@ def foreground(self):

def background(self):
"""Print 256 background colors"""
if not self.enabled():
return ""
code = self.ESC + "48;5;"
if str(self.color).isdigit():
self.reverse_dict()
Expand All @@ -370,6 +353,73 @@ def reverse_dict(self):
"""reverse dictionary"""
self.reserve_paint = dict(zip(self.paint.values(), self.paint.keys()))

def enable_windows_terminal_mode(self):
'''Enable virtual terminal processing in windows terminal. Does
nothing if not on Windows. This is based on the rejected
enhancement <https://bugs.python.org/issue29059>.'''
global _win_vterm_mode
if _win_vterm_mode != None:
return _win_vterm_mode

# Note: Cygwin should return something like "CYGWIN_NT..."
_win_vterm_mode = platform.system().lower() == 'windows'
if _win_vterm_mode == False:
return

from ctypes import windll, c_int, byref, c_void_p
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
INVALID_HANDLE_VALUE = c_void_p(-1).value
STD_OUTPUT_HANDLE = c_int(-11)

hStdout = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
if hStdout == INVALID_HANDLE_VALUE:
_win_vterm_mode = False
return

mode = c_int(0)
ok = windll.kernel32.GetConsoleMode(c_int(hStdout), byref(mode))
if not ok:
_win_vterm_mode = False
return

mode = c_int(mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
ok = windll.kernel32.SetConsoleMode(c_int(hStdout), mode)
if not ok:
# Something went wrong, proably an too old version
# that doesn't support the VT100 mode.
# To be more certain we could check kernel32.GetLastError
# for STATUS_INVALID_PARAMETER, but since we only enable
# one flag we can be certain enough.
_win_vterm_mode = False
return

def enabled(self):

# https://github.com/chalk/supports-color#info
# Use the environment variable FORCE_COLOR=1 (level 1), FORCE_COLOR=2
# (level 2), or FORCE_COLOR=3 (level 3) to forcefully enable color, or
# FORCE_COLOR=0 to forcefully disable. The use of FORCE_COLOR overrides
# all other color support checks.
if "FORCE_COLOR" in os.environ:
if int(os.environ["FORCE_COLOR"]) == 0:
return False
else:
return True

# https://no-color.org/
# Check for the presence of a NO_COLOR environment variable that, when
# present (regardless of its value), prevents the addition of ANSI
# color.
if "NO_COLOR" in os.environ:
return False

# Also disable coloring when not printing to a TTY.
if TTY_AWARE and not IS_TTY:
return False

# In all other cases, enable coloring.
return True


def attr(color):
"""alias for colored().attribute()"""
Expand Down Expand Up @@ -404,6 +454,13 @@ def stylize_interactive(text, styles, reset=True):
safety."""
# problem: readline includes bare ANSI codes in width calculations.
# solution: wrap nonprinting codes in SOH/STX when necessary.
# see: https://github.com/dslackw/colored/issues/5
# see: https://gitlab.com/dslackw/colored/issues/5
terminator = _c0wrap(attr("reset")) if reset else ""
return "{}{}{}".format(_c0wrap(styles), text, terminator)

def set_tty_aware(awareness=True):
"""Makes all interactions here tty aware. This means that if either
stdout or stderr are directed to something other than a tty,
colorization will not be added."""
global TTY_AWARE
TTY_AWARE = awareness
24 changes: 1 addition & 23 deletions rampwf/externals/colored/colors.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
colors.py is a part of colored.
Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
All rights reserved.
Colored is very simple Python library for color and formatting in terminal.
https://github.com/dslackw/colored
colored is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


names = [
'BLACK',
Expand Down
24 changes: 1 addition & 23 deletions rampwf/externals/colored/fore.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
#!/usr/bin/python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
fore.py is a part of colored.
Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
All rights reserved.
Colored is very simple Python library for color and formatting in terminal.
https://github.com/dslackw/colored
colored is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


from .colors import names

Expand Down
Loading

0 comments on commit a5a00ad

Please sign in to comment.