Skip to content

Commit

Permalink
General improvements and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GiorgosXou committed Oct 18, 2024
1 parent 5c4c05b commit b1f0809
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 159 deletions.
10 changes: 5 additions & 5 deletions TUIFIManager/TUIFIProfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unicurses import COLOR_PAIR, COLOR_WHITE, OPERATING_SYSTEM, init_color, init_pair, pair_content
from .TUItilities import HOME_DIR, init_colorscheme
from .TUItilities import HOME_DIR, IS_MACOS, IS_WINDOWS, init_colorscheme
from shutil import which
from os.path import isfile
from os import getenv, sep
Expand All @@ -26,7 +26,7 @@ def __init__(self, text, color_map=1, open_with=None):



DEFAULT_OPENER = 'start' if 'Windows' == OPERATING_SYSTEM else 'open' if 'Darwin' == OPERATING_SYSTEM else 'xdg-open' # meh.. # TODO: make an enviromental variable insted of those 2 vars, for everything
DEFAULT_OPENER = 'start' if OPERATING_SYSTEM == IS_WINDOWS else 'open' if OPERATING_SYSTEM == IS_MACOS else 'xdg-open' # meh.. # TODO: make an enviromental variable insted of those 2 vars, for everything
DEFAULT_EDITOR = which(getenv('tuifi_default_editor', getenv('EDITOR', 'nvim'))) or which('emacs') or which('vim') or which('micro') or which('nano') or which('vi') or DEFAULT_OPENER
DEFAULT_WITH = DEFAULT_EDITOR

Expand Down Expand Up @@ -819,7 +819,7 @@ def __init__(self, text, color_map=1, open_with=None):
' C j \n'
' L ] \n'
' '
),4),
),4, DEFAULT_EDITOR),
'/tuifi3':TUIFIProfile((
' .^ \n'
' ~GB^ \n'
Expand All @@ -841,7 +841,7 @@ def __init__(self, text, color_map=1, open_with=None):
' JBY^ \n'
' .^. \n'
' '
),5),
),5, DEFAULT_EDITOR),
'/tuifi4':TUIFIProfile((
'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n'
'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n'
Expand Down Expand Up @@ -874,7 +874,7 @@ def __init__(self, text, color_map=1, open_with=None):
'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⠀⠀⠀⠀\n'
'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⠀⠀⠀⠀\n'
'⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠁⠀⠀⠀'
),8)
),8, DEFAULT_EDITOR)
}
DEFAULT_PROFILE = TUIFIProfiles[':file']
LINK_SYMBOL = '↩' # Potential:⤶ ⤾ ↲ ⎌ ☍ ⧉
Expand Down
96 changes: 25 additions & 71 deletions TUIFIManager/TUItilities.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""
TUItilities is a set of TUI components and terminal utilities in ALPHA version (They will be exported to a new package).
"""
from time import sleep
import unicurses as uc
import threading
import subprocess
import threading
from dataclasses import dataclass
from os.path import isfile
from shutil import which
from pipes import quote
from time import sleep
from os import getenv, getcwd


Expand All @@ -20,6 +21,7 @@
BEGIN_MOUSE = "\033[?1003h"
END_MOUSE = "\033[?1003l"

IS_MACOS = uc.OPERATING_SYSTEM == 'Darwin'
IS_WINDOWS = uc.OPERATING_SYSTEM == 'Windows'
HOME_DIR = getenv('UserProfile') if IS_WINDOWS else getenv('HOME')
SHELL = getenv('SHELL') # https://stackoverflow.com/a/35662469/11465149 | https://superuser.com/questions/1515578/
Expand Down Expand Up @@ -369,11 +371,14 @@ def get_mouse(self):
def handle_events(self, event, redraw_parent=True):
if event == uc.KEY_MOUSE:
in_range, id, x, y, z, bstate = self.get_mouse() # aah... here because calling getmouse more than once, returns 0,0...
if not in_range:return
if (bstate & uc.BUTTON1_RELEASED or bstate & uc.BUTTON3_RELEASED or bstate & uc.BUTTON2_RELEASED):
if not in_range: return False # If not in_range, there's no consumption of the event
if ( bstate & uc.BUTTON1_RELEASED or bstate & uc.BUTTON3_RELEASED or bstate & uc.BUTTON2_RELEASED) or \
(bstate & uc.BUTTON1_CLICKED or bstate & uc.BUTTON3_CLICKED or bstate & uc.BUTTON2_CLICKED): # because Termux doesn't care about uc.mouseinterval(0), see also /TUIFIManager/issues/114
self.on_click(self, id, x, y, z, bstate)
return
return True
self.on_hover(self, id, x, y, z, bstate)
return True
return False


def centerX(self):
Expand Down Expand Up @@ -509,11 +514,13 @@ def _in_range(self):
)

def handle_events(self, event, redraw_parent=True): # prevent multiple if conditions for drawable components too
if event == uc.KEY_RESIZE: self.handle_resize(redraw_parent)
if event == uc.KEY_RESIZE: return self.handle_resize(redraw_parent) # meaning no event consumption, we let KEY_RESIZE pass through
elif self.visibility:
super().handle_events(event,redraw_parent)
event_consumed = super().handle_events(event,redraw_parent)
for drawable in self.components:
if drawable.visibility: drawable.handle_events(event, redraw_parent)
if drawable.visibility: event_consumed |= drawable.handle_events(event, redraw_parent)
return event_consumed
return False # meaning no event consumption, we found no event matching


def _resize(self):
Expand All @@ -524,6 +531,7 @@ def handle_resize(self, redraw_parent=True, redraw_border=True): # TODO: max min
super().handle_resize(redraw_parent=redraw_parent, redraw_border=redraw_border)
for drawable in self.components:
drawable.handle_resize()
return False # meaning no event consumption, we let KEY_RESIZE pass through



Expand Down Expand Up @@ -563,17 +571,17 @@ def __add_component_to(self, winpad):


class Label(Drawable): # TODO: make components a type of Drawables\components rather than a WindowPad
def __init__(self,winpad:WindowPad, y=0, x=0, text='', height=1, width=None, anchor=(False, False, False, False), wrap_text=False, color=4 ) -> None:
def __init__(self,winpad:WindowPad, y=0, x=0, text='', height=1, width=None, anchor=(False, False, False, False), wrap_text=False, color=4, style=uc.A_NORMAL ) -> None:
super().__init__(winpad, y, x, height, width if width else len(text), anchor)
self.style = uc.A_NORMAL
self.style = style
self.color_pair = color
self.__text = text
self._text = text
self.wrap_text = wrap_text
self.maxheight = 1 # WARN: this is temporary maybe?
self.minwidth = len(text) # WARN: this is temporary maybe?
self.maxheight = 1 # WARN : this is temporary maybe?
self.minwidth = len(text) # WARN : this is temporary maybe?

@property
def text(self): return self.__text
def text(self): return self._text


def draw(self):
Expand All @@ -599,7 +607,7 @@ def draw(self):

@text.setter
def text(self, text):
self.__text = text
self._text = text
self.refresh()


Expand Down Expand Up @@ -641,60 +649,6 @@ def draw(self):



class PictureBoxMono(Drawable): # Monochrome
def __init__(self,winpad:WindowPad, y=0, x=0, text='', height=1, width=None, anchor=(False, False, False, False), wrap_text=False, color=4 ) -> None:
super().__init__(winpad, y, x, height, width if width else len(text), anchor)
self.style = uc.A_NORMAL
self._text = text
self.color_pair = color


@property
def text(self): return self._text


@text.setter
def text(self, text):
self._text = text
self.refresh()


def draw(self):
if not self.text: return
for i, ln in enumerate(self.text.split('\n')):
uc.mvwaddstr(self.parent.win,self.y+i,self.x, ' '*len(ln), color_pair_with_effect(self.color_pair , self.style ))
uc.mvwaddstr(self.parent.win,self.y+i,self.x, ln , color_pair_with_effect(self.color_pair , self.style ))




class PictureBoxMono(Drawable): # Monochrome
def __init__(self,winpad:WindowPad, y=0, x=0, text='', height=1, width=None, anchor=(False, False, False, False), wrap_text=False, color=4 ) -> None:
super().__init__(winpad, y, x, height, width if width else len(text), anchor)
self.style = uc.A_NORMAL
self._text = text
self.color_pair = color


@property
def text(self): return self._text


@text.setter
def text(self, text):
self._text = text
self.refresh()


def draw(self):
if not self.text: return
for i, ln in enumerate(self.text.split('\n')):
uc.mvwaddstr(self.parent.win,self.y+i,self.x, ' '*len(ln), color_pair_with_effect(self.color_pair , self.style ))
uc.mvwaddstr(self.parent.win,self.y+i,self.x, ln , color_pair_with_effect(self.color_pair , self.style ))





# THIS IS ONLY FOR TESTING PURPOSES
def main():
Expand Down Expand Up @@ -731,13 +685,13 @@ def label_hoverd(label, id, x, y, z, bstate):
winform.minheight = 10
winform.maxheight = 20

uc.wbkgd(winform.pad, uc.COLOR_PAIR(7))
uc.wbkgd(winform.pad, uc.COLOR_PAIR(1))

winform2 = WindowPad(stdscr,12,4,10,60, anchor=(True, False, True, False))
# # winform2.minwidth = 30
# winform2.minwidth = 40
# winform2.maxwidth = 60
uc.wbkgd(winform2.pad, uc.COLOR_PAIR(9))
uc.wbkgd(winform2.pad, uc.COLOR_PAIR(1))

tl = Label(winform ,1,2 ,anchor=(True, False, True, False),text='top-left')
tr = Label(winform ,1,winform.width-11 ,anchor=(True, False, False, True),text='top-right')
Expand Down
Loading

0 comments on commit b1f0809

Please sign in to comment.