Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link modifier #208

Merged
merged 8 commits into from
Nov 21, 2024
55 changes: 33 additions & 22 deletions TermTk/TTkCore/TTkTerm/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,49 @@ class TTkTermColor():
29: ~STRIKETROUGH } # Ps = 2 9 ⇒ Not crossed-out, ECMA-48 3rd.

@staticmethod
def rgb2ansi(fg: tuple=None, bg:tuple=None, mod:int=0, link:str='', clean:bool=False):
def rgb2ansi(fg: tuple=None, bg:tuple=None, mod:int=0, clean:bool=False):
ret = []
ret_append = ret.append

if clean:
ret.append(0)
ret_append('0')

if fg:
ret.append(f'38;2;{fg[0]};{fg[1]};{fg[2]}')
ret_append(f'38;2;{fg[0]};{fg[1]};{fg[2]}')
if bg:
ret.append(f'48;2;{bg[0]};{bg[1]};{bg[2]}')

if mod & TTkTermColor.BOLD:
ret.append('1')
if mod & TTkTermColor.FAINT:
ret.append('2')
if mod & TTkTermColor.ITALIC:
ret.append('3')
if mod & TTkTermColor.UNDERLINE:
ret.append('4')
if mod & TTkTermColor.BLINKING:
ret.append('5')
if mod & TTkTermColor.REVERSED:
ret.append('7')
if mod & TTkTermColor.HIDDEN:
ret.append('8')
if mod & TTkTermColor.STRIKETROUGH:
ret.append('9')
ret_append(f'48;2;{bg[0]};{bg[1]};{bg[2]}')

if mod:
if mod & TTkTermColor.BOLD:
ret_append('1')
if mod & TTkTermColor.FAINT:
ret_append('2')
if mod & TTkTermColor.ITALIC:
ret_append('3')
if mod & TTkTermColor.UNDERLINE:
ret_append('4')
if mod & TTkTermColor.BLINKING:
ret_append('5')
if mod & TTkTermColor.REVERSED:
ret_append('7')
if mod & TTkTermColor.HIDDEN:
ret_append('8')
if mod & TTkTermColor.STRIKETROUGH:
ret_append('9')
if ret:
return f'\033[{";".join(str(x) for x in ret)}m'
return f'\033[{";".join(ret)}m'
else:
return '\033[0m'

@staticmethod
def rgb2ansi_link(fg: tuple=None, bg:tuple=None, mod:int=0, link:str='', clean:bool=False, cleanLink:bool=False):
linkAnsi = ""
if cleanLink:
linkAnsi = "\033]8;;\033\\"
if link:
linkAnsi = f"\033]8;id=1;{link}\033\\"
return TTkTermColor.rgb2ansi(fg=fg,bg=bg,mod=mod,clean=clean)+linkAnsi

def _256toRgb(val):
pass

Expand Down
2 changes: 2 additions & 0 deletions TermTk/TTkCore/TTkTerm/colors_ansi_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# https://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
# https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

__all__ = ['ansiMap16', 'ansiMap256']

ansiMap16 = {
# xterm Color Map
# fg
Expand Down
6 changes: 4 additions & 2 deletions TermTk/TTkCore/TTkTerm/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# from .input_mono import *
from .input_thread import *
__all__ = ['TTkInput']

# from .input_mono import TTkInput
from .input_thread import TTkInput
2 changes: 2 additions & 0 deletions TermTk/TTkCore/TTkTerm/term_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

__all__ = ['TTkTermBase']

import os

class TTkTermBase():
Expand Down
6 changes: 4 additions & 2 deletions TermTk/TTkCore/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,14 +743,14 @@ def pushToTerminalBuffered(self, x, y, w, h):
ansi = TTkTerm.Cursor.moveTo(y+1,x+1)
empty = False
if color != lastcolor:
ansi += str(color-lastcolor)
ansi += color-lastcolor
lastcolor = color
ansi+=ch
if not empty:
TTkTerm.push(ansi)
empty=True
# Reset the color at the end
TTkTerm.push(TTkColor.RST)
TTkTerm.push(TTkColor.RST-lastcolor)
# TTkTerm.flush()
# Switch the buffer
self._bufferedData, self._bufferedColors = data, colors
Expand Down Expand Up @@ -801,6 +801,8 @@ def pushToTerminalBufferedNew(self, x, y, w, h):
empty=True
# Reset the color at the end
TTkTerm.push(TTkColor.RST)
if lastcolor._link:
TTkTerm.push("\033]8;;\033\\")
# Switch the buffer
self._bufferedData, self._bufferedColors = data, colors
self._data, self._colors = oldData, oldColors
Loading
Loading