updater: Add some colors and a frame to the update message (#1273)

Close #1032
This commit is contained in:
Mr. Outis
2018-10-31 17:52:50 -06:00
committed by GitHub
parent acae1b996f
commit bdf4095297
2 changed files with 77 additions and 3 deletions
+60
View File
@@ -2,11 +2,28 @@ import sys
import logging
import colorama
import traceback
import re
colorama.init()
def visual_width(line):
""" Get the the number of columns required to display a string """
return len(re.sub(colorama.ansitowin32.AnsiToWin32.ANSI_CSI_RE, '', line))
def visual_center(line, width):
""" Center align string according to it's visual width """
spaces = max(width - visual_width(line), 0)
left_padding = int(spaces / 2)
right_padding = (spaces - left_padding)
return (left_padding * ' ') + line + (right_padding * ' ')
class Logger(object):
FMT = '%(message)s'
DEFAULT_LEVEL = logging.INFO
@@ -155,3 +172,46 @@ class Logger(object):
@staticmethod
def info(msg):
Logger._with_progress(Logger.logger().info, msg)
@classmethod
def box(cls, msg, border_color=''):
lines = msg.split('\n')
max_width = max(visual_width(line) for line in lines)
padding_horizontal = 5
padding_vertical = 1
box_size_horizontal = (max_width + (padding_horizontal * 2))
chars = {
'corner': '+',
'horizontal': '-',
'vertical': '|',
'empty': ' ',
}
margin = "{corner}{line}{corner}\n".format(
corner=chars['corner'],
line=chars['horizontal'] * box_size_horizontal,
)
padding_lines = [
"{border}{space}{border}\n".format(
border=cls.colorize(chars['vertical'], border_color),
space=chars['empty'] * box_size_horizontal,
) * padding_vertical
]
content_lines = [
"{border}{space}{content}{space}{border}\n".format(
border=cls.colorize(chars['vertical'], border_color),
space=chars['empty'] * padding_horizontal,
content=visual_center(line, max_width),
) for line in lines
]
return "{margin}{padding}{content}{padding}{margin}".format(
margin=cls.colorize(margin, border_color),
padding=''.join(padding_lines),
content=''.join(content_lines),
)
+17 -3
View File
@@ -1,6 +1,7 @@
import os
import time
import requests
import colorama
from dvc import VERSION_BASE
from dvc.logger import Logger
@@ -56,6 +57,19 @@ class Updater(object): # pragma: no cover
l_patch <= c_patch:
return
msg = 'You are using dvc version {}, however version {} is ' \
'available. Consider upgrading.'
Logger.warn(msg.format(current, latest))
msg = (
'Update available {red}{current}{reset} -> {green}{latest}{reset}'
'\n'
'Run {yellow}pip{reset} install dvc {blue}--upgrade{reset}'
.format(
red=colorama.Fore.RED,
reset=colorama.Fore.RESET,
green=colorama.Fore.GREEN,
yellow=colorama.Fore.YELLOW,
blue=colorama.Fore.BLUE,
current=current,
latest=latest,
)
)
Logger.box(msg, border_color='yellow')