7 changed files with 227 additions and 38 deletions
@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
# of this software and associated documentation files (the "Software"), to deal |
||||
# in the Software without restriction, including without limitation the rights |
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
# copies of the Software, and to permit persons to whom the Software is |
||||
# furnished to do so, subject to the following conditions: |
||||
# |
||||
# The above copyright notice and this permission notice shall be included in all |
||||
# copies or substantial portions of the Software. |
||||
# |
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
# SOFTWARE. |
||||
|
||||
from TermTk.TTkCore.constant import TTkK |
||||
from TermTk.TTkCore.log import TTkLog |
||||
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal |
||||
from TermTk.TTkCore.color import TTkColor |
||||
|
||||
class TTkString(): |
||||
__slots__ = ('_text','_colors','_baseColor') |
||||
def __init__(self): |
||||
self._baseColor = TTkColor.RST |
||||
self._text = "" |
||||
self._colors = [] |
||||
|
||||
def __len__(self): |
||||
return len(self._text) |
||||
|
||||
def __str__(self): |
||||
return self._text |
||||
|
||||
def __add__(self, other): |
||||
ret = TTkString() |
||||
ret._baseColor = self._baseColor |
||||
if isinstance(other, TTkString): |
||||
ret._text = self._text + other._text |
||||
ret._colors = self._colors + other._colors |
||||
elif isinstance(other, str): |
||||
ret._text = self._text + other |
||||
ret._colors = self._colors + [self._baseColor]*len(other) |
||||
elif isinstance(other, TTkColor): |
||||
ret._text = self._text |
||||
ret._colors = self._colors |
||||
ret._baseColor = other |
||||
return ret |
||||
|
||||
def __radd__(self, other): |
||||
ret = TTkString() |
||||
ret._baseColor = self._baseColor |
||||
if isinstance(other, TTkString): |
||||
ret._text = other._text + self._text |
||||
ret._colors = other._colors + self._colors |
||||
elif isinstance(other, str): |
||||
ret._text = other + self._text |
||||
ret._colors = [self._baseColor]*len(other) + self._colors |
||||
return ret |
||||
|
||||
def __setitem__(self, index, value): |
||||
raise NotImplementedError() |
||||
|
||||
def __getitem__(self, index): |
||||
raise NotImplementedError() |
||||
|
||||
def toAansi(self): |
||||
out = "" |
||||
color = None |
||||
for ch, col in zip(self._text, self._colors): |
||||
if col != color: |
||||
color = col |
||||
out += str(TTkColor.RST) + str(color) |
||||
out += ch |
||||
return out |
||||
|
||||
def align(self, width=None, color=TTkColor.RST, alignment=TTkK.NONE): |
||||
lentxt = len(self._text) |
||||
if not width or width == lentxt: return self |
||||
|
||||
ret = TTkString() |
||||
|
||||
if lentxt < width: |
||||
pad = width-lentxt |
||||
if alignment in [TTkK.NONE, TTkK.LEFT_ALIGN]: |
||||
ret._text = self._text + " " *pad |
||||
ret._colors = self._colors + [color]*pad |
||||
elif alignment == TTkK.RIGHT_ALIGN: |
||||
ret._text = " " *pad + self._text |
||||
ret._colors = [color]*pad + self._colors |
||||
elif alignment == TTkK.CENTER_ALIGN: |
||||
p1 = pad//2 |
||||
p2 = pad-p1 |
||||
ret._text = " " *p1 + self._text + " " *p2 |
||||
ret._colors = [color]*p1 + self._colors + [color]*p2 |
||||
elif alignment == TTkK.JUSTIFY: |
||||
# TODO: Text Justification |
||||
ret._text = self._text + " " *pad |
||||
ret._colors = self._colors + [color]*pad |
||||
else: |
||||
ret._text = self._text[:width] |
||||
ret._colors = self._colors[:width] |
||||
|
||||
return ret |
||||
|
||||
def getData(self): |
||||
return (self._text,self._colors) |
||||
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
# of this software and associated documentation files (the "Software"), to deal |
||||
# in the Software without restriction, including without limitation the rights |
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
# copies of the Software, and to permit persons to whom the Software is |
||||
# furnished to do so, subject to the following conditions: |
||||
# |
||||
# The above copyright notice and this permission notice shall be included in all |
||||
# copies or substantial portions of the Software. |
||||
# |
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
# SOFTWARE. |
||||
|
||||
import sys, os |
||||
import logging |
||||
import time |
||||
|
||||
sys.path.append(os.path.join(sys.path[0],'..')) |
||||
from TermTk.libbpytop import Term, Mv, Color |
||||
from TermTk import TTkLog |
||||
from TermTk.TTkCore import TTkColor |
||||
from TermTk.TTkCore import TTkHelper |
||||
from TermTk.TTkCore import TTkString |
||||
|
||||
TTkLog.use_default_file_logging() |
||||
|
||||
Term.init(mouse=False) |
||||
TTkLog.info("Starting") |
||||
|
||||
s1 = TTkString() + "Text " + "Text 1" |
||||
s2 = TTkString() + TTkColor.fg("#ff0000") + "Test " + "Text 2" |
||||
s3 = TTkString() + TTkColor.bg("#550088") + "Test " + "Text 3" |
||||
s4 = TTkString() + ( TTkColor.fg("#00ff00") + |
||||
TTkColor.bg("#555500") ) + "Test " + TTkColor.bg("#880055") + "Text 4" |
||||
|
||||
Term.push( |
||||
TTkHelper.Mv.t(2,4) + # Cursor x:2, y:4 |
||||
s1.toAansi() ) |
||||
time.sleep(1) |
||||
TTkLog.info("next : 3") |
||||
|
||||
Term.push( |
||||
TTkHelper.Mv.d(1) + Mv.l(3) + # Cursor 1 Down, 3 Left |
||||
s2.toAansi() ) |
||||
time.sleep(1) |
||||
TTkLog.info("next : 2") |
||||
|
||||
Term.push( |
||||
TTkHelper.Mv.d(1) + Mv.l(3) + # Cursor 1 Down, 3 Left |
||||
s3.toAansi() ) |
||||
time.sleep(1) |
||||
TTkLog.info("next : 1") |
||||
|
||||
Term.push( |
||||
TTkHelper.Mv.d(1) + Mv.l(3) + # Cursor 1 Down, 3 Left |
||||
s4.toAansi() ) |
||||
time.sleep(3) |
||||
TTkLog.info("Ending") |
||||
|
||||
Term.exit() |
||||
Loading…
Reference in new issue