8 changed files with 467 additions and 96 deletions
@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2025 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 |
||||
|
||||
sys.path.append(os.path.join(sys.path[0],'../..')) |
||||
import TermTk as ttk |
||||
|
||||
def testColor(prefix:str,c1:ttk.TTkColor, c2:ttk.TTkColor): |
||||
str1 = ttk.TTkString('Eugenio',c1) |
||||
str2 = ttk.TTkString('Parodi' ,c2) |
||||
rst = "\033]8;;\033\\" |
||||
print(prefix + (str1+str2).toAnsi() + rst) |
||||
|
||||
fg_r = ttk.TTkColor.FG_RED |
||||
fg_g = ttk.TTkColor.FG_GREEN |
||||
fg_b = ttk.TTkColor.FG_BLUE |
||||
bg_r = ttk.TTkColor.BG_RED |
||||
bg_g = ttk.TTkColor.BG_GREEN |
||||
bg_b = ttk.TTkColor.BG_BLUE |
||||
fg_r_l = ttk.TTkColor.fg('#FF0000',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
fg_g_l = ttk.TTkColor.fg('#00FF00',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
fg_b_l = ttk.TTkColor.fg('#0000FF',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
bg_r_l = ttk.TTkColor.bg('#FF0000',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
bg_g_l = ttk.TTkColor.bg('#00FF00',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
bg_b_l = ttk.TTkColor.bg('#0000FF',link='https://github.com/ceccopierangiolieugenio/pyTermTk') |
||||
testColor("r ",fg_r, bg_r) |
||||
testColor("g ",fg_g, bg_g) |
||||
testColor("b ",fg_b, bg_b) |
||||
testColor("rl ",fg_r_l, bg_r_l) |
||||
testColor("gl ",fg_g_l, bg_g_l) |
||||
testColor("bl ",fg_b_l, bg_b_l) |
||||
|
||||
c1 = fg_r + bg_g |
||||
c2 = fg_r | bg_g |
||||
testColor("T1 ",c1,c2) |
||||
|
||||
m1 = bg_b + fg_g |
||||
m2 = bg_b | fg_g |
||||
testColor("T2 ",m1,m2) |
||||
|
||||
m3 = ttk.TTkColor.FG_YELLOW + m1 |
||||
m4 = m1 + ttk.TTkColor.FG_YELLOW |
||||
m5 = ttk.TTkColor.FG_YELLOW | m1 |
||||
m6 = m1 | ttk.TTkColor.FG_YELLOW |
||||
testColor("M1 ",m3,m4) |
||||
testColor("M1 ",m5,m6) |
||||
|
||||
m3 = ttk.TTkColor.BG_YELLOW + m1 |
||||
m4 = m1 + ttk.TTkColor.BG_YELLOW |
||||
m5 = ttk.TTkColor.BG_YELLOW | m1 |
||||
m6 = m1 | ttk.TTkColor.BG_YELLOW |
||||
testColor("M2 ",m3,m4) |
||||
testColor("M2 ",m5,m6) |
||||
|
||||
m3 = ttk.TTkColor.RST + m1 |
||||
m4 = m1 + ttk.TTkColor.RST |
||||
m5 = ttk.TTkColor.RST | m1 |
||||
m6 = m1 | ttk.TTkColor.RST |
||||
testColor("M3 ",m3,m4) |
||||
testColor("M3 ",m5,m6) |
||||
@ -0,0 +1,173 @@
|
||||
#!/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 os |
||||
import sys |
||||
import random |
||||
import argparse |
||||
|
||||
sys.path.append(os.path.join(sys.path[0],'../..')) |
||||
import TermTk as ttk |
||||
|
||||
def demoTextEdit(root, filename): |
||||
frame = ttk.TTkFrame(parent=root, border=False, layout=ttk.TTkGridLayout()) |
||||
|
||||
te = ttk.TTkTextEdit() |
||||
te.setReadOnly(False) |
||||
|
||||
with open(filename, 'r') as f: |
||||
content = f.read() |
||||
doc = ttk.TextDocumentHighlight(text=content) |
||||
te.setDocument(doc) |
||||
|
||||
# use the widget size to wrap |
||||
# te.setLineWrapMode(ttk.TTkK.WidgetWidth) |
||||
# te.setWordWrapMode(ttk.TTkK.WordWrap) |
||||
|
||||
# Use a fixed wrap size |
||||
# te.setLineWrapMode(ttk.TTkK.FixedWidth) |
||||
# te.setWrapWidth(100) |
||||
|
||||
frame.layout().addWidget(te,1,0,1,10) |
||||
frame.layout().addWidget(ttk.TTkLabel(text="Wrap: ", maxWidth=6),0,0) |
||||
frame.layout().addWidget(lineWrap := ttk.TTkComboBox(list=['NoWrap','WidgetWidth','FixedWidth']),0,1) |
||||
frame.layout().addWidget(ttk.TTkLabel(text=" Type: ",maxWidth=7),0,2) |
||||
frame.layout().addWidget(wordWrap := ttk.TTkComboBox(list=['WordWrap','WrapAnywhere'], enabled=False),0,3) |
||||
frame.layout().addWidget(ttk.TTkLabel(text=" FixW: ",maxWidth=7),0,4) |
||||
frame.layout().addWidget(fixWidth := ttk.TTkSpinBox(value=te.wrapWidth(), maxWidth=6, maximum=500, minimum=10, enabled=False),0,5) |
||||
frame.layout().addWidget(ttk.TTkLabel(text=" Lexer: ",maxWidth=8),0,6) |
||||
frame.layout().addWidget(lexers := ttk.TTkComboBox(list=ttk.TextDocumentHighlight.getLexers()),0,7) |
||||
frame.layout().addWidget(ttk.TTkLabel(text=" Style: ",maxWidth=8),0,8) |
||||
frame.layout().addWidget(styles := ttk.TTkComboBox(list=ttk.TextDocumentHighlight.getStyles()),0,9) |
||||
|
||||
|
||||
lineWrap.setCurrentIndex(0) |
||||
wordWrap.setCurrentIndex(1) |
||||
|
||||
fixWidth.valueChanged.connect(te.setWrapWidth) |
||||
lexers.currentTextChanged.connect(doc.setLexer) |
||||
styles.currentTextChanged.connect(doc.setStyle) |
||||
|
||||
@ttk.pyTTkSlot(int) |
||||
def _lineWrapCallback(index): |
||||
if index == 0: |
||||
te.setLineWrapMode(ttk.TTkK.NoWrap) |
||||
wordWrap.setDisabled() |
||||
fixWidth.setDisabled() |
||||
elif index == 1: |
||||
te.setLineWrapMode(ttk.TTkK.WidgetWidth) |
||||
wordWrap.setEnabled() |
||||
fixWidth.setDisabled() |
||||
else: |
||||
te.setLineWrapMode(ttk.TTkK.FixedWidth) |
||||
wordWrap.setEnabled() |
||||
fixWidth.setEnabled() |
||||
|
||||
lineWrap.currentIndexChanged.connect(_lineWrapCallback) |
||||
|
||||
@ttk.pyTTkSlot(int) |
||||
def _wordWrapCallback(index): |
||||
if index == 0: |
||||
te.setWordWrapMode(ttk.TTkK.WordWrap) |
||||
else: |
||||
te.setWordWrapMode(ttk.TTkK.WrapAnywhere) |
||||
|
||||
@ttk.pyTTkSlot(ttk.TTkTextCursor) |
||||
def _positionChanged(cursor:ttk.TTkTextCursor): |
||||
extra_selections = [] |
||||
|
||||
# Hiighlight YELLOW all the selected lines |
||||
cursor = te.textCursor().copy() |
||||
lines = [] |
||||
for cur in cursor.cursors(): |
||||
selSt = cur.selectionStart().line |
||||
selEn = cur.selectionEnd().line |
||||
lines += [x for x in range(selSt,selEn+1)] |
||||
cursor.clearCursors() |
||||
cursor.clearSelection() |
||||
for x in set(lines): |
||||
cursor.addCursor(x,0) |
||||
selection = ttk.TTkTextEdit.ExtraSelection( |
||||
cursor=cursor, |
||||
color=ttk.TTkColor.BG_YELLOW, |
||||
format=ttk.TTkK.SelectionFormat.FullWidthSelection) |
||||
extra_selections.append(selection) |
||||
|
||||
# Highlight Red only the lines under the cursor positions |
||||
cursor = te.textCursor().copy() |
||||
cursor.clearSelection() |
||||
selection = ttk.TTkTextEdit.ExtraSelection( |
||||
cursor=cursor, |
||||
color=ttk.TTkColor.BG_RED, |
||||
format=ttk.TTkK.SelectionFormat.FullWidthSelection) |
||||
extra_selections.append(selection) |
||||
|
||||
# Highlight GREEN the words under the cursor positions |
||||
cursor = te.textCursor().copy() |
||||
cursor.select(ttk.TTkTextCursor.SelectionType.WordUnderCursor) |
||||
selection = ttk.TTkTextEdit.ExtraSelection( |
||||
cursor=cursor, |
||||
color=ttk.TTkColor.BG_GREEN) |
||||
extra_selections.append(selection) |
||||
|
||||
te.setExtraSelections(extra_selections) |
||||
|
||||
wordWrap.currentIndexChanged.connect(_wordWrapCallback) |
||||
te.cursorPositionChanged.connect(_positionChanged) |
||||
|
||||
return frame |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument('filename', type=str, nargs='+', |
||||
help='the filename/s') |
||||
args = parser.parse_args() |
||||
|
||||
ttk.TTkTheme.loadTheme(ttk.TTkTheme.NERD) |
||||
|
||||
root = ttk.TTk(layout=ttk.TTkGridLayout()) |
||||
appTemplate = ttk.TTkAppTemplate(parent=root) |
||||
appTemplate.setWidget(fileTree := ttk.TTkFileTree(), position=ttk.TTkK.LEFT, size=30) |
||||
appTemplate.setItem(layoutArea := ttk.TTkLayout(), position=appTemplate.Position.MAIN) |
||||
|
||||
def _openFile(fileName): |
||||
newPos = (0,0) |
||||
oldPos = [win.pos() for win in layoutArea.children()] |
||||
while newPos in oldPos: |
||||
newPos = (newPos[0]+1,newPos[1]+1,) |
||||
|
||||
win = ttk.TTkWindow(pos = newPos, size=(100,40), title=f"Test Text Edit ({fileName})", layout=ttk.TTkGridLayout(), border=True) |
||||
layoutArea.addWidget(win) |
||||
demoTextEdit(win, fileName) |
||||
|
||||
for file in args.filename: |
||||
_openFile(file) |
||||
|
||||
fileTree.fileActivated.connect(lambda x: _openFile(x.path())) |
||||
|
||||
root.mainloop() |
||||
|
||||
if __name__ == "__main__": |
||||
main() |
||||
Loading…
Reference in new issue