mirror of
https://github.com/soimort/you-get.git
synced 2025-01-23 21:45:02 +03:00
update util.log
This commit is contained in:
parent
fb475bf0c2
commit
03749ecb19
@ -1,130 +1,97 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# This file is Python 2 compliant.
|
||||||
|
|
||||||
from ..version import __name__
|
from .. import __name__ as library_name
|
||||||
|
|
||||||
import os, sys, subprocess
|
import os, sys
|
||||||
|
|
||||||
# Is terminal ANSI/VT100 compatible
|
IS_ANSI_TERMINAL = os.getenv('TERM') in (
|
||||||
if os.getenv('TERM') in (
|
'eterm-color',
|
||||||
'xterm',
|
'linux',
|
||||||
'vt100',
|
'screen',
|
||||||
'linux',
|
'vt100',
|
||||||
'eterm-color',
|
'xterm')
|
||||||
'screen',
|
|
||||||
):
|
|
||||||
has_colors = True
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
# Eshell
|
|
||||||
ppid = os.getppid()
|
|
||||||
has_colors = (subprocess.getoutput('ps -p %d -ocomm=' % ppid)
|
|
||||||
== 'emacs')
|
|
||||||
except:
|
|
||||||
has_colors = False
|
|
||||||
|
|
||||||
# ANSI/VT100 escape code
|
# ANSI escape code
|
||||||
# http://en.wikipedia.org/wiki/ANSI_escape_code
|
# See <http://en.wikipedia.org/wiki/ANSI_escape_code>
|
||||||
colors = {
|
RESET = 0
|
||||||
'none': '',
|
BOLD = 1
|
||||||
'reset': '\033[0m',
|
UNDERLINE = 4
|
||||||
|
NEGATIVE = 7
|
||||||
|
NO_BOLD = 21
|
||||||
|
NO_UNDERLINE = 24
|
||||||
|
POSITIVE = 27
|
||||||
|
BLACK = 30
|
||||||
|
RED = 31
|
||||||
|
GREEN = 32
|
||||||
|
YELLOW = 33
|
||||||
|
BLUE = 34
|
||||||
|
MAGENTA = 35
|
||||||
|
CYAN = 36
|
||||||
|
LIGHT_GRAY = 37
|
||||||
|
DEFAULT = 39
|
||||||
|
BLACK_BACKGROUND = 40
|
||||||
|
RED_BACKGROUND = 41
|
||||||
|
GREEN_BACKGROUND = 42
|
||||||
|
YELLOW_BACKGROUND = 43
|
||||||
|
BLUE_BACKGROUND = 44
|
||||||
|
MAGENTA_BACKGROUND = 45
|
||||||
|
CYAN_BACKGROUND = 46
|
||||||
|
LIGHT_GRAY_BACKGROUND = 47
|
||||||
|
DEFAULT_BACKGROUND = 49
|
||||||
|
DARK_GRAY = 90 # xterm
|
||||||
|
LIGHT_RED = 91 # xterm
|
||||||
|
LIGHT_GREEN = 92 # xterm
|
||||||
|
LIGHT_YELLOW = 93 # xterm
|
||||||
|
LIGHT_BLUE = 94 # xterm
|
||||||
|
LIGHT_MAGENTA = 95 # xterm
|
||||||
|
LIGHT_CYAN = 96 # xterm
|
||||||
|
WHITE = 97 # xterm
|
||||||
|
DARK_GRAY_BACKGROUND = 100 # xterm
|
||||||
|
LIGHT_RED_BACKGROUND = 101 # xterm
|
||||||
|
LIGHT_GREEN_BACKGROUND = 102 # xterm
|
||||||
|
LIGHT_YELLOW_BACKGROUND = 103 # xterm
|
||||||
|
LIGHT_BLUE_BACKGROUND = 104 # xterm
|
||||||
|
LIGHT_MAGENTA_BACKGROUND = 105 # xterm
|
||||||
|
LIGHT_CYAN_BACKGROUND = 106 # xterm
|
||||||
|
WHITE_BACKGROUND = 107 # xterm
|
||||||
|
|
||||||
'black': '\033[30m',
|
def sprint(text, *colors):
|
||||||
'bold-black': '\033[30;1m',
|
"""Format text with color or other effects into ANSI escaped string."""
|
||||||
'dark-gray': '\033[90m',
|
return "\33[{}m{content}\33[{}m".format(";".join([str(color) for color in colors]), RESET, content=text) if IS_ANSI_TERMINAL and colors else text
|
||||||
'bold-dark-gray': '\033[90;1m',
|
|
||||||
|
|
||||||
'red': '\033[31m',
|
def println(text, *colors):
|
||||||
'bold-red': '\033[31;1m',
|
"""Print text to standard output."""
|
||||||
'light-red': '\033[91m',
|
sys.stdout.write(sprint(text, *colors) + "\n")
|
||||||
'bold-light-red': '\033[91;1m',
|
|
||||||
|
|
||||||
'green': '\033[32m',
|
def print_err(text, *colors):
|
||||||
'bold-green': '\033[32;1m',
|
"""Print text to standard error."""
|
||||||
'light-green': '\033[92m',
|
sys.stderr.write(sprint(text, *colors) + "\n")
|
||||||
'bold-light-green': '\033[92;1m',
|
|
||||||
|
|
||||||
'yellow': '\033[33m',
|
def print_log(text, *colors):
|
||||||
'bold-yellow': '\033[33;1m',
|
"""Print a log message to standard error."""
|
||||||
'light-yellow': '\033[93m',
|
sys.stderr.write(sprint("{}: {}".format(library_name, text), *colors) + "\n")
|
||||||
'bold-light-yellow': '\033[93;1m',
|
|
||||||
|
|
||||||
'blue': '\033[34m',
|
def i(message):
|
||||||
'bold-blue': '\033[34;1m',
|
"""Print a normal log message."""
|
||||||
'light-blue': '\033[94m',
|
print_log(message)
|
||||||
'bold-light-blue': '\033[94;1m',
|
|
||||||
|
|
||||||
'magenta': '\033[35m',
|
def d(message):
|
||||||
'bold-magenta': '\033[35;1m',
|
"""Print a debug log message."""
|
||||||
'light-magenta': '\033[95m',
|
print_log(message, BLUE)
|
||||||
'bold-light-magenta': '\033[95;1m',
|
|
||||||
|
|
||||||
'cyan': '\033[36m',
|
def w(message):
|
||||||
'bold-cyan': '\033[36;1m',
|
"""Print a warning log message."""
|
||||||
'light-cyan': '\033[96m',
|
print_log(message, YELLOW)
|
||||||
'bold-light-cyan': '\033[96;1m',
|
|
||||||
|
|
||||||
'light-gray': '\033[37m',
|
def e(message, exit_code=None):
|
||||||
'bold-light-gray': '\033[37;1m',
|
"""Print an error log message."""
|
||||||
'white': '\033[97m',
|
print_log(message, YELLOW, BOLD)
|
||||||
'bold-white': '\033[97;1m',
|
|
||||||
}
|
|
||||||
|
|
||||||
def underlined(text):
|
|
||||||
"""Returns an underlined text.
|
|
||||||
"""
|
|
||||||
return "\33[4m%s\33[24m" % text if has_colors else text
|
|
||||||
|
|
||||||
def println(text, color=None, ostream=sys.stdout):
|
|
||||||
"""Prints a text line to stream.
|
|
||||||
"""
|
|
||||||
if has_colors and color in colors:
|
|
||||||
ostream.write("{0}{1}{2}\n".format(colors[color], text, colors['reset']))
|
|
||||||
else:
|
|
||||||
ostream.write("{0}\n".format(text))
|
|
||||||
|
|
||||||
def printlog(message, color=None, ostream=sys.stderr):
|
|
||||||
"""Prints a log message to stream.
|
|
||||||
"""
|
|
||||||
if has_colors and color in colors:
|
|
||||||
ostream.write("{0}{1}: {2}{3}\n".format(colors[color], __name__, message, colors['reset']))
|
|
||||||
else:
|
|
||||||
ostream.write("{0}: {1}\n".format(__name__, message))
|
|
||||||
|
|
||||||
def i(message, ostream=sys.stderr):
|
|
||||||
"""Sends an info log message.
|
|
||||||
"""
|
|
||||||
printlog(message,
|
|
||||||
None,
|
|
||||||
ostream=ostream)
|
|
||||||
|
|
||||||
def d(message, ostream=sys.stderr):
|
|
||||||
"""Sends a debug log message.
|
|
||||||
"""
|
|
||||||
printlog(message,
|
|
||||||
'blue' if has_colors else None,
|
|
||||||
ostream=ostream)
|
|
||||||
|
|
||||||
def w(message, ostream=sys.stderr):
|
|
||||||
"""Sends a warning log message.
|
|
||||||
"""
|
|
||||||
printlog(message,
|
|
||||||
'yellow' if has_colors else None,
|
|
||||||
ostream=ostream)
|
|
||||||
|
|
||||||
def e(message, ostream=sys.stderr, exit_code=None):
|
|
||||||
"""Sends an error log message.
|
|
||||||
"""
|
|
||||||
printlog(message,
|
|
||||||
'bold-yellow' if has_colors else None,
|
|
||||||
ostream=ostream)
|
|
||||||
if exit_code is not None:
|
if exit_code is not None:
|
||||||
exit(exit_code)
|
exit(exit_code)
|
||||||
|
|
||||||
def wtf(message, ostream=sys.stderr, exit_code=-1):
|
def wtf(message, exit_code=-1):
|
||||||
"""What a Terrible Failure.
|
"""What a Terrible Failure!"""
|
||||||
"""
|
print_log(message, RED, BOLD)
|
||||||
printlog(message,
|
|
||||||
'bold-red' if has_colors else None,
|
|
||||||
ostream=ostream)
|
|
||||||
if exit_code is not None:
|
if exit_code is not None:
|
||||||
exit(exit_code)
|
exit(exit_code)
|
||||||
|
Loading…
Reference in New Issue
Block a user