parent
bae95d943c
commit
69740f6843
@ -0,0 +1,52 @@
|
||||
#-*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2014 Calculate Ltd. http://www.calculate-linux.org
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from printing import Print
|
||||
from palette import TextState
|
||||
from info import Terminal
|
||||
Colors = TextState.Colors
|
||||
|
||||
from converter import ConsoleCodes256Converter, XmlConverter
|
||||
from output import XmlOutput, ColorTerminal16Output, TerminalPositionOutput, \
|
||||
ColorTerminal256Output
|
||||
|
||||
|
||||
def convert_console_to_xml(s):
|
||||
"""Преобразовать вывод консоли в xml для внутреннего использования"""
|
||||
return ConsoleCodes256Converter(output=XmlOutput()).transform(s)
|
||||
|
||||
def get_color_print():
|
||||
"""
|
||||
Получить объект для вывода текста в цвете
|
||||
"""
|
||||
return Print(output=XmlOutput())
|
||||
|
||||
def get_terminal_output():
|
||||
return (ColorTerminal256Output()
|
||||
if Terminal().colors > 16
|
||||
else ColorTerminal16Output())
|
||||
|
||||
def get_terminal_print(printfunc=lambda x: x):
|
||||
"""
|
||||
Получить объект для вывода в терминал
|
||||
"""
|
||||
# TODO: возвращать объект 256 или 16 терминал в зависимости от параметров
|
||||
return Print(output=get_terminal_output(),
|
||||
position_controller=TerminalPositionOutput(),
|
||||
printfunc=printfunc)
|
||||
|
||||
|
||||
def convert_xml_to_terminal(s):
|
||||
return XmlConverter(output=get_terminal_output()).transform(s)
|
@ -0,0 +1,43 @@
|
||||
#-*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2014 Calculate Ltd. http://www.calculate-linux.org
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import curses
|
||||
from os import environ
|
||||
import sys
|
||||
import struct
|
||||
import fcntl
|
||||
import termios
|
||||
|
||||
|
||||
class Terminal(object):
|
||||
def __init__(self):
|
||||
curses.setupterm(environ.get('TERM', 'linux'))
|
||||
|
||||
@property
|
||||
def colors(self):
|
||||
return curses.tigetnum('colors')
|
||||
|
||||
@property
|
||||
def width(self):
|
||||
s = struct.pack("HHHH", 0, 0, 0, 0)
|
||||
fd_stdout = sys.stdout.fileno()
|
||||
try:
|
||||
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
|
||||
except IOError:
|
||||
# если ошибка то ширина 80 символов
|
||||
return 80
|
||||
#(rows, cols, x pixels, y pixels)
|
||||
return struct.unpack("HHHH", x)[1]
|
@ -0,0 +1,81 @@
|
||||
#-*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2014 Calculate Ltd. http://www.calculate-linux.org
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from output import BaseOutput, BasePositionOutput
|
||||
|
||||
|
||||
class Print(object):
|
||||
"""
|
||||
Упрощенное получение строки при помощи Output объектов """
|
||||
def __init__(self, output=BaseOutput(),
|
||||
position_controller=BasePositionOutput(),
|
||||
printfunc=lambda x:x):
|
||||
self.output = output
|
||||
self.position_controller = position_controller
|
||||
self.buffer = []
|
||||
self.printfunc = printfunc
|
||||
|
||||
@property
|
||||
def bold(self):
|
||||
self.buffer.append(self.output.setBold())
|
||||
return self
|
||||
|
||||
def foreground(self, color):
|
||||
self.buffer.append(self.output.setForeground(color))
|
||||
return self
|
||||
|
||||
def background(self, color):
|
||||
self.buffer.append(self.output.setBackground(color))
|
||||
return self
|
||||
|
||||
@property
|
||||
def invert(self):
|
||||
self.buffer.append(self.output.setInvert())
|
||||
return self
|
||||
|
||||
@property
|
||||
def halflight(self):
|
||||
self.buffer.append(self.output.setHalfbright())
|
||||
return self
|
||||
|
||||
def up(self, count):
|
||||
self.buffer.append(self.position_controller.moveCursorUp(count))
|
||||
return self
|
||||
|
||||
def down(self, count):
|
||||
self.buffer.append(self.position_controller.moveCursorDown(count))
|
||||
return self
|
||||
|
||||
def right(self, count):
|
||||
self.buffer.append(self.position_controller.moveCursorRight(count))
|
||||
return self
|
||||
|
||||
def left(self, count):
|
||||
self.buffer.append(self.position_controller.moveCursorLeft(count))
|
||||
return self
|
||||
|
||||
@property
|
||||
def clear_line(self):
|
||||
self.buffer.append(self.position_controller.clearLine(whole_line=True))
|
||||
return self
|
||||
|
||||
def __call__(self, s, *args, **kwargs):
|
||||
s = s.format(*args, **kwargs)
|
||||
self.buffer.append(self.output.outputText(s))
|
||||
self.buffer.append(self.output.endText())
|
||||
try:
|
||||
return self.printfunc("".join(filter(None, self.buffer)))
|
||||
finally:
|
||||
self.buffer = []
|
Loading…
Reference in new issue