|
|
|
@ -14,18 +14,19 @@
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
from calculate.lib.utils.tools import SavableIterator
|
|
|
|
|
from palette import (TextState, BaseColorMapping,
|
|
|
|
|
ConsoleCodeMapping, LightColorMapping, ConsoleColor256,
|
|
|
|
|
ConsoleCodesInfo, SpanPalette)
|
|
|
|
|
|
|
|
|
|
class BaseOutput:
|
|
|
|
|
|
|
|
|
|
class BaseOutput(object):
|
|
|
|
|
"""
|
|
|
|
|
Базовый вывод текста.
|
|
|
|
|
|
|
|
|
|
Вывод просто текста без изменения шрифта
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,state=None):
|
|
|
|
|
|
|
|
|
|
def __init__(self, state=None):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def setBold(self):
|
|
|
|
@ -76,31 +77,31 @@ class BaseOutput:
|
|
|
|
|
"""
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def outputText(self,text):
|
|
|
|
|
def outputText(self, text):
|
|
|
|
|
"""
|
|
|
|
|
Вывести текст с установленными настройками
|
|
|
|
|
"""
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
def setForeground(self,color):
|
|
|
|
|
def setForeground(self, color):
|
|
|
|
|
"""
|
|
|
|
|
Установить цвет шрифта
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def setBackground(self,color):
|
|
|
|
|
def setBackground(self, color):
|
|
|
|
|
"""
|
|
|
|
|
Установить цвет фона
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def resetForeground(self,color):
|
|
|
|
|
def resetForeground(self, color):
|
|
|
|
|
"""
|
|
|
|
|
Использовать цвет шрифта по умолчанию
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def resetBackground(self,color):
|
|
|
|
|
def resetBackground(self, color):
|
|
|
|
|
"""
|
|
|
|
|
Использовать цвет фона по умолчанию
|
|
|
|
|
"""
|
|
|
|
@ -116,11 +117,13 @@ class BaseOutput:
|
|
|
|
|
Выключить инверсию
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SaveAttrOutput(BaseOutput):
|
|
|
|
|
"""
|
|
|
|
|
Базовый класс с сохранением атрибутов
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,state=None):
|
|
|
|
|
|
|
|
|
|
def __init__(self, state=None):
|
|
|
|
|
self.prev_state = state.clone() if state else TextState()
|
|
|
|
|
self.current_state = self.prev_state.clone()
|
|
|
|
|
|
|
|
|
@ -150,13 +153,13 @@ class SaveAttrOutput(BaseOutput):
|
|
|
|
|
self.resetBackground()
|
|
|
|
|
self.resetInvert()
|
|
|
|
|
|
|
|
|
|
def setForeground(self,color):
|
|
|
|
|
def setForeground(self, color):
|
|
|
|
|
self.current_state.foreground = color
|
|
|
|
|
|
|
|
|
|
def resetForeground(self):
|
|
|
|
|
self.current_state.foreground = TextState.Colors.DEFAULT
|
|
|
|
|
|
|
|
|
|
def setBackground(self,color):
|
|
|
|
|
def setBackground(self, color):
|
|
|
|
|
self.current_state.background = color
|
|
|
|
|
|
|
|
|
|
def resetBackground(self):
|
|
|
|
@ -168,13 +171,14 @@ class SaveAttrOutput(BaseOutput):
|
|
|
|
|
def setInvert(self):
|
|
|
|
|
self.current_state.invert = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ColorTerminalOutput(SaveAttrOutput):
|
|
|
|
|
"""
|
|
|
|
|
Форматирует текст для вывода в консоль
|
|
|
|
|
"""
|
|
|
|
|
mapColors = ConsoleCodeMapping(ConsoleCodesInfo.FOREGROUND,
|
|
|
|
|
BaseColorMapping).mapTS_Console
|
|
|
|
|
mapLightColors = ConsoleCodeMapping(ConsoleCodesInfo.FOREGROUND-
|
|
|
|
|
mapLightColors = ConsoleCodeMapping(ConsoleCodesInfo.FOREGROUND -
|
|
|
|
|
LightColorMapping.offset,
|
|
|
|
|
LightColorMapping).mapTS_Console
|
|
|
|
|
mapBackgroundColors = ConsoleCodeMapping(ConsoleCodesInfo.BACKGROUND,
|
|
|
|
@ -188,27 +192,28 @@ class ColorTerminalOutput(SaveAttrOutput):
|
|
|
|
|
self.resetBold()
|
|
|
|
|
SaveAttrOutput.setHalfbright(self)
|
|
|
|
|
|
|
|
|
|
def handleForeground(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleForeground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
cci = ConsoleCodesInfo
|
|
|
|
|
color = curstate.foreground
|
|
|
|
|
if color in self.mapColors:
|
|
|
|
|
attrs.append(self.mapColors[color])
|
|
|
|
|
elif color in self.mapLightColors:
|
|
|
|
|
if prevstate.bold == curstate.bold and curstate.bold == False:
|
|
|
|
|
if prevstate.bold == curstate.bold and not curstate.bold:
|
|
|
|
|
tail_attrs.append(cci.NORMAL)
|
|
|
|
|
attrs.append(cci.BOLD)
|
|
|
|
|
attrs.append(self.mapLightColors[color])
|
|
|
|
|
else:
|
|
|
|
|
attrs.append(cci.FOREGROUND_DEFAULT)
|
|
|
|
|
self.resetForeground()
|
|
|
|
|
|
|
|
|
|
def handleBackground(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleBackground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
color = curstate.background
|
|
|
|
|
if color in self.mapBackgroundColors:
|
|
|
|
|
attrs.append(self.mapBackgroundColors[color])
|
|
|
|
|
else:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.BACKGROUND_DEFAULT)
|
|
|
|
|
|
|
|
|
|
def handleIntensity(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleIntensity(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
if curstate.bold and curstate.bold != prevstate.bold:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.BOLD)
|
|
|
|
|
elif (curstate.halfbright and
|
|
|
|
@ -217,25 +222,25 @@ class ColorTerminalOutput(SaveAttrOutput):
|
|
|
|
|
else:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.NORMAL)
|
|
|
|
|
|
|
|
|
|
def handleUnderline(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleUnderline(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
if curstate.underline:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.UNDERLINE)
|
|
|
|
|
else:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.NOUNDERLINE)
|
|
|
|
|
|
|
|
|
|
def handleInvert(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleInvert(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
if curstate.invert:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.INVERT)
|
|
|
|
|
else:
|
|
|
|
|
attrs.append(ConsoleCodesInfo.NOINVERT)
|
|
|
|
|
|
|
|
|
|
def _createAttrs(self,prevstate,curstate):
|
|
|
|
|
def _createAttrs(self, prevstate, curstate):
|
|
|
|
|
"""
|
|
|
|
|
Создать ESC последовательность для установки параметров текста
|
|
|
|
|
"""
|
|
|
|
|
attrs = []
|
|
|
|
|
attrs, tail_attrs = [],[]
|
|
|
|
|
# получить интенсивность (полутон и жирность относятся к интенсивности)
|
|
|
|
|
intensity = lambda x:x & (TextState.Attributes.HALFBRIGHT |
|
|
|
|
|
intensity = lambda x: x & (TextState.Attributes.HALFBRIGHT |
|
|
|
|
|
TextState.Attributes.BOLD)
|
|
|
|
|
|
|
|
|
|
if (prevstate.attr != curstate.attr and
|
|
|
|
@ -245,44 +250,34 @@ class ColorTerminalOutput(SaveAttrOutput):
|
|
|
|
|
attrs.append(ConsoleCodesInfo.RESET)
|
|
|
|
|
else:
|
|
|
|
|
if intensity(prevstate.attr) != intensity(curstate.attr):
|
|
|
|
|
self.handleIntensity(prevstate,curstate,attrs)
|
|
|
|
|
self.handleIntensity(prevstate, curstate, attrs, tail_attrs)
|
|
|
|
|
if prevstate.underline != curstate.underline:
|
|
|
|
|
self.handleUnderline(prevstate,curstate,attrs)
|
|
|
|
|
self.handleUnderline(prevstate, curstate, attrs, tail_attrs)
|
|
|
|
|
if prevstate.invert != curstate.invert:
|
|
|
|
|
self.handleInvert(prevstate,curstate,attrs)
|
|
|
|
|
self.handleInvert(prevstate, curstate, attrs, tail_attrs)
|
|
|
|
|
if prevstate.foreground != curstate.foreground:
|
|
|
|
|
self.handleForeground(prevstate,curstate,attrs)
|
|
|
|
|
self.handleForeground(prevstate, curstate, attrs, tail_attrs)
|
|
|
|
|
if prevstate.background != curstate.background:
|
|
|
|
|
self.handleBackground(prevstate,curstate,attrs)
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
def handlePostAttr(self,prevstate,curstate):
|
|
|
|
|
"""
|
|
|
|
|
Добавление аттрибутов после выведенного текста
|
|
|
|
|
"""
|
|
|
|
|
if prevstate.foreground != curstate.foreground:
|
|
|
|
|
color = curstate.foreground
|
|
|
|
|
if (color in self.mapLightColors and
|
|
|
|
|
prevstate.bold == curstate.bold and curstate.bold == False):
|
|
|
|
|
return [ConsoleCodesInfo.NORMAL]
|
|
|
|
|
self.handleBackground(prevstate, curstate, attrs, tail_attrs)
|
|
|
|
|
return attrs, tail_attrs
|
|
|
|
|
|
|
|
|
|
def _createEscCode(self,attrs):
|
|
|
|
|
def _createEscCode(self, attrs):
|
|
|
|
|
"""
|
|
|
|
|
Создать ESC строку
|
|
|
|
|
"""
|
|
|
|
|
attrs = map(str,['\033['] + attrs + ['m'])
|
|
|
|
|
return "%s%s%s"%(attrs[0],";".join(attrs[1:-1]),attrs[-1])
|
|
|
|
|
attrs = map(str, ['\033['] + attrs + ['m'])
|
|
|
|
|
return "%s%s%s" % (attrs[0], ";".join(attrs[1:-1]), attrs[-1])
|
|
|
|
|
|
|
|
|
|
def outputText(self,s):
|
|
|
|
|
def outputText(self, s):
|
|
|
|
|
"""
|
|
|
|
|
Задание параметров текста и вывод его
|
|
|
|
|
"""
|
|
|
|
|
if self.prev_state != self.current_state:
|
|
|
|
|
attr = self._createAttrs(self.prev_state,self.current_state)
|
|
|
|
|
attr, tail_attrs = \
|
|
|
|
|
self._createAttrs(self.prev_state, self.current_state)
|
|
|
|
|
attr = self._createEscCode(attr)
|
|
|
|
|
postattr = self.handlePostAttr(self.prev_state,self.current_state)
|
|
|
|
|
if postattr:
|
|
|
|
|
postattr = self._createEscCode(postattr)
|
|
|
|
|
if tail_attrs:
|
|
|
|
|
postattr = self._createEscCode(tail_attrs)
|
|
|
|
|
else:
|
|
|
|
|
postattr = ""
|
|
|
|
|
self.prev_state = self.current_state.clone()
|
|
|
|
@ -295,12 +290,14 @@ class ColorTerminalOutput(SaveAttrOutput):
|
|
|
|
|
self.reset()
|
|
|
|
|
return self.outputText("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ColorTerminal256Output(ColorTerminalOutput):
|
|
|
|
|
"""
|
|
|
|
|
Вывод на 256 цветный терминал
|
|
|
|
|
"""
|
|
|
|
|
mapLightColors = LightColorMapping.mapTS_Console
|
|
|
|
|
def handleForeground(self,prevstate,curstate,attrs):
|
|
|
|
|
|
|
|
|
|
def handleForeground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
color = curstate.foreground
|
|
|
|
|
color256 = ConsoleColor256.rgbToConsole(color)
|
|
|
|
|
if not color256 and color in self.mapLightColors:
|
|
|
|
@ -310,9 +307,10 @@ class ColorTerminal256Output(ColorTerminalOutput):
|
|
|
|
|
ConsoleCodesInfo.COLOR256,
|
|
|
|
|
color256])
|
|
|
|
|
else:
|
|
|
|
|
ColorTerminalOutput.handleForeground(self,prevstate,curstate,attrs)
|
|
|
|
|
ColorTerminalOutput.handleForeground(self, prevstate, curstate,
|
|
|
|
|
attrs, tail_attrs)
|
|
|
|
|
|
|
|
|
|
def handleBackground(self,prevstate,curstate,attrs):
|
|
|
|
|
def handleBackground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
color = curstate.background
|
|
|
|
|
color256 = ConsoleColor256.rgbToConsole(color)
|
|
|
|
|
if not color256 and color in self.mapLightColors:
|
|
|
|
@ -322,20 +320,65 @@ class ColorTerminal256Output(ColorTerminalOutput):
|
|
|
|
|
ConsoleCodesInfo.COLOR256,
|
|
|
|
|
color256])
|
|
|
|
|
else:
|
|
|
|
|
ColorTerminalOutput.handleBackground(self,prevstate,curstate,attrs)
|
|
|
|
|
ColorTerminalOutput.handleBackground(self, prevstate, curstate,
|
|
|
|
|
attrs, tail_attrs)
|
|
|
|
|
|
|
|
|
|
def handlePostAttr(self,prevstate,curstate):
|
|
|
|
|
pass
|
|
|
|
|
class ColorTerminal16Output(ColorTerminalOutput):
|
|
|
|
|
"""
|
|
|
|
|
Вывод на 16 цветный терминал с преобразованием RGB к ближайшему базовому
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, state=None, palette=None):
|
|
|
|
|
SaveAttrOutput.__init__(self, state=state)
|
|
|
|
|
self.palette = palette
|
|
|
|
|
|
|
|
|
|
def _handleNearestColors(self, color):
|
|
|
|
|
"""
|
|
|
|
|
Обработка преобразования к ближайшему цвету
|
|
|
|
|
"""
|
|
|
|
|
standardColors = TextState.normalColors + TextState.lightColors
|
|
|
|
|
if self.palette and color not in standardColors:
|
|
|
|
|
return self.palette.getBaseColorByRGB(color)
|
|
|
|
|
return color
|
|
|
|
|
|
|
|
|
|
def handleForeground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
"""
|
|
|
|
|
Добавить преобразование RGB к ближайшему базовому
|
|
|
|
|
"""
|
|
|
|
|
_curstate = curstate.clone()
|
|
|
|
|
_curstate.foreground = \
|
|
|
|
|
self._handleNearestColors(curstate.foreground)
|
|
|
|
|
super(ColorTerminal16Output, self).handleForeground(
|
|
|
|
|
prevstate, _curstate,
|
|
|
|
|
attrs, tail_attrs)
|
|
|
|
|
|
|
|
|
|
def handleBackground(self, prevstate, curstate, attrs, tail_attrs):
|
|
|
|
|
"""
|
|
|
|
|
Добавить преобразование RGB к ближайшему базовому
|
|
|
|
|
"""
|
|
|
|
|
mapHighNormal = dict(zip(TextState.lightColors,
|
|
|
|
|
TextState.normalColors))
|
|
|
|
|
_curstate = curstate.clone()
|
|
|
|
|
_curstate.background = \
|
|
|
|
|
self._handleNearestColors(curstate.background)
|
|
|
|
|
# преобразовать яркий цвет к обычному
|
|
|
|
|
_curstate.background = \
|
|
|
|
|
mapHighNormal.get(_curstate.background, _curstate.background)
|
|
|
|
|
super(ColorTerminal16Output, self).handleBackground(
|
|
|
|
|
prevstate, _curstate,
|
|
|
|
|
attrs, tail_attrs)
|
|
|
|
|
|
|
|
|
|
class SpanCssOutput(SaveAttrOutput):
|
|
|
|
|
"""
|
|
|
|
|
Форматирует текст для вывода в консоль
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,state=None,palette=SpanPalette()):
|
|
|
|
|
SaveAttrOutput.__init__(self,state=state)
|
|
|
|
|
|
|
|
|
|
def __init__(self, state=None, palette=SpanPalette()):
|
|
|
|
|
SaveAttrOutput.__init__(self, state=state)
|
|
|
|
|
self.palette = palette
|
|
|
|
|
|
|
|
|
|
def getStringColor(self,color,bold=False,halfbright=False,background=False):
|
|
|
|
|
def getStringColor(self, color, bold=False, halfbright=False,
|
|
|
|
|
background=False):
|
|
|
|
|
"""
|
|
|
|
|
Получить название цвета по номеру и состоянию текста
|
|
|
|
|
"""
|
|
|
|
@ -349,17 +392,17 @@ class SpanCssOutput(SaveAttrOutput):
|
|
|
|
|
if background:
|
|
|
|
|
return self.palette.getBackgroundColor(color)
|
|
|
|
|
else:
|
|
|
|
|
return self.palette.getTextColor(color,bright)
|
|
|
|
|
return self.palette.getTextColor(color, bright)
|
|
|
|
|
|
|
|
|
|
def getTags(self,prevstate,curstate):
|
|
|
|
|
def getTags(self, prevstate, curstate):
|
|
|
|
|
"""
|
|
|
|
|
Создать ESC последовательность для установки параметров текста
|
|
|
|
|
"""
|
|
|
|
|
style = []
|
|
|
|
|
|
|
|
|
|
colorAttr = ["color","background"]
|
|
|
|
|
colorAttr = ["colortext", "background"]
|
|
|
|
|
if curstate.invert:
|
|
|
|
|
colorAttr = colorAttr[1],colorAttr[0]
|
|
|
|
|
colorAttr = colorAttr[1], colorAttr[0]
|
|
|
|
|
if (prevstate.foreground != curstate.foreground or
|
|
|
|
|
prevstate.bold != curstate.bold or
|
|
|
|
|
curstate.invert or
|
|
|
|
@ -368,10 +411,11 @@ class SpanCssOutput(SaveAttrOutput):
|
|
|
|
|
curstate.bold,
|
|
|
|
|
curstate.halfbright,
|
|
|
|
|
background=False)
|
|
|
|
|
style.append("%s:%s;"%(colorAttr[0],sColor))
|
|
|
|
|
style.append("%s:%s;" % (colorAttr[0], sColor))
|
|
|
|
|
if prevstate.background != curstate.background or curstate.invert:
|
|
|
|
|
sColor = self.getStringColor(curstate.background,background=True)
|
|
|
|
|
style.append("%s:%s;"%(colorAttr[1],sColor))
|
|
|
|
|
sColor = self.getStringColor(curstate.background,
|
|
|
|
|
background=True)
|
|
|
|
|
style.append("%s:%s;" % (colorAttr[1], sColor))
|
|
|
|
|
if prevstate.underline != curstate.underline:
|
|
|
|
|
if curstate.underline:
|
|
|
|
|
style.append("text-decoration:underline;")
|
|
|
|
@ -382,11 +426,11 @@ class SpanCssOutput(SaveAttrOutput):
|
|
|
|
|
style.append("font-weight:bold;")
|
|
|
|
|
else:
|
|
|
|
|
style.append("font-weight:normal;")
|
|
|
|
|
return '<span style="%s">'%"".join(style),'</span>'
|
|
|
|
|
return '<span style="%s">' % "".join(style), '</span>'
|
|
|
|
|
|
|
|
|
|
def outputText(self,s):
|
|
|
|
|
def outputText(self, s):
|
|
|
|
|
if self.prev_state != self.current_state:
|
|
|
|
|
lattr, rattr = self.getTags(self.prev_state,self.current_state)
|
|
|
|
|
lattr, rattr = self.getTags(self.prev_state, self.current_state)
|
|
|
|
|
else:
|
|
|
|
|
lattr = rattr = ""
|
|
|
|
|
return lattr + s + rattr
|