GiorgosXou / TUIFIManager

A cross-platform terminal-based termux-oriented file manager (and component), meant to be used with a Uni-Curses project or as is.
GNU General Public License v3.0
688 stars 13 forks source link

Multiple instances when opening and closing a file #10

Closed GiorgosXou closed 2 years ago

GiorgosXou commented 2 years ago

https://github.com/GiorgosXou/TUIFIManager/blob/19487a82f9c42152b46c58e2a04a942585533eda/TUIFIManager/__init__.py#L255/

GiorgosXou commented 2 years ago

Silly of me running another subprocess on top of another thinking that the first one would had been terminated somehow (or whatever the fuck i was thinking that time).

Issue

It's more of a "how to restore the terminal after opening&closing a subprocess" issue rather than anything else. Somehow after such a sequence (of opening&closing) things can get messed up, especially keys and the mode of terminal [...]

A potential solution

A temporary but also potential solution could be os.exec*()

GiorgosXou commented 2 years ago

Yep, so far (the temp solution) works as expected Screenshot_2022-02-14-14-26-46-749_com termux (Image: termux)

GiorgosXou commented 2 years ago

Actually this line os.execl(sys.argv[0], sys.argv[0], self.directory) does the job (I'm not sure why sys.argv[0] needs to be pressent 2 times)

GiorgosXou commented 2 years ago

Also just a reminder to add

if unicurses.OPERATING_SYSTEM != 'Windows': 
    os.system('stty sane')

Or something like that anyways

GiorgosXou commented 2 years ago

I just found out that os.execl(sys.argv[0], sys.argv[0], self.directory) works only if pip3 install . the setup.py and not if I just run python3 ...\__main__.py with:

import sys                                               
from os.path import dirname, abspath    
sys.path.insert(0, dirname(dirname(abspath(__file__))))
GiorgosXou commented 2 years ago

Here another very intresting solution i found out:


    @staticmethod
    @contextmanager
    def suspend():
        """
        Suspend curses in order to open another subprocess in the terminal.
        """

        try:
            curses.endwin()
            yield
        finally:
            curses.doupdate()
def open(...):
   ...
            if open_with:
                with self.suspend():
                    os.system('clear')
                    proc = subprocess.Popen([open_with, directory])
                    proc.wait()

Althought I've no idea of how it works