1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-22 02:29:07 +00:00
thefuck/thefuck/logs.py

96 lines
3.0 KiB
Python
Raw Normal View History

2015-07-29 15:22:24 +03:00
# -*- encoding: utf-8 -*-
2015-07-23 06:09:57 +03:00
from contextlib import contextmanager
from datetime import datetime
2015-04-22 06:03:06 +02:00
import sys
from traceback import format_exception
import colorama
2015-09-06 21:47:12 +03:00
from .conf import settings
2015-04-22 06:03:06 +02:00
2015-09-06 21:47:12 +03:00
def color(color_):
2015-04-22 06:03:06 +02:00
"""Utility for ability to disabling colored output."""
if settings.no_colors:
return ''
else:
return color_
2015-09-06 21:47:12 +03:00
def exception(title, exc_info):
2015-04-22 06:03:06 +02:00
sys.stderr.write(
u'{warn}[WARN] {title}:{reset}\n{trace}'
2015-04-22 06:03:06 +02:00
u'{warn}----------------------------{reset}\n\n'.format(
warn=color(colorama.Back.RED + colorama.Fore.WHITE
2015-09-06 21:47:12 +03:00
+ colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL),
title=title,
2015-04-22 06:03:06 +02:00
trace=''.join(format_exception(*exc_info))))
2015-09-06 21:47:12 +03:00
def rule_failed(rule, exc_info):
2015-11-01 14:42:48 +08:00
exception(u'Rule {}'.format(rule.name), exc_info)
2015-09-06 21:47:12 +03:00
def failed(msg):
2015-11-01 13:16:58 +08:00
sys.stderr.write(u'{red}{msg}{reset}\n'.format(
2015-07-28 22:04:27 +03:00
msg=msg,
2015-09-06 21:47:12 +03:00
red=color(colorama.Fore.RED),
reset=color(colorama.Style.RESET_ALL)))
2015-07-28 22:04:27 +03:00
2015-09-06 21:47:12 +03:00
def show_corrected_command(corrected_command):
2015-11-01 13:16:58 +08:00
sys.stderr.write(u'{bold}{script}{reset}{side_effect}\n'.format(
2015-07-28 22:04:27 +03:00
script=corrected_command.script,
2015-11-01 14:42:48 +08:00
side_effect=u' (+side effect)' if corrected_command.side_effect else u'',
2015-09-06 21:47:12 +03:00
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL)))
2015-04-22 06:03:06 +02:00
2015-09-06 21:47:12 +03:00
def confirm_text(corrected_command):
2015-04-22 06:03:06 +02:00
sys.stderr.write(
2015-11-01 13:16:58 +08:00
(u'{clear}{bold}{script}{reset}{side_effect} '
2015-11-01 14:42:48 +08:00
u'[{green}enter{reset}/{blue}{reset}/{blue}{reset}'
u'/{red}ctrl+c{reset}]').format(
2015-07-28 22:04:27 +03:00
script=corrected_command.script,
side_effect=' (+side effect)' if corrected_command.side_effect else '',
clear='\033[1K\r',
2015-09-06 21:47:12 +03:00
bold=color(colorama.Style.BRIGHT),
green=color(colorama.Fore.GREEN),
red=color(colorama.Fore.RED),
reset=color(colorama.Style.RESET_ALL),
blue=color(colorama.Fore.BLUE)))
2015-07-15 07:47:54 +03:00
2015-09-06 21:47:12 +03:00
def debug(msg):
2015-07-15 07:47:54 +03:00
if settings.debug:
sys.stderr.write(u'{blue}{bold}DEBUG:{reset} {msg}\n'.format(
msg=msg,
2015-09-06 21:47:12 +03:00
reset=color(colorama.Style.RESET_ALL),
blue=color(colorama.Fore.BLUE),
bold=color(colorama.Style.BRIGHT)))
2015-07-23 06:09:57 +03:00
@contextmanager
2015-09-06 21:47:12 +03:00
def debug_time(msg):
2015-07-23 06:09:57 +03:00
started = datetime.now()
try:
yield
finally:
2015-09-06 21:47:12 +03:00
debug(u'{} took: {}'.format(msg, datetime.now() - started))
2015-09-06 21:47:12 +03:00
def how_to_configure_alias(configuration_details):
print("Seems like {bold}fuck{reset} alias isn't configured!".format(
2015-09-06 21:47:12 +03:00
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL)))
if configuration_details:
content, path = configuration_details
print(
"Please put {bold}{content}{reset} in your "
"{bold}{path}{reset}.".format(
2015-09-06 21:47:12 +03:00
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL),
path=path,
content=content))
print('More details - https://github.com/nvbn/thefuck#manual-installation')