Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbonn committed Feb 5, 2024
1 parent 1089066 commit 5a42ce1
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 58 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

123 changes: 69 additions & 54 deletions i3_quickterm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,52 @@ def command(self, payload: str, *kargs, **kwargs):


class Quickterm:
def __init__(self, conf: Conf, shell: str):
def __init__(self, conf: Conf, shell: str, conn: Optional[i3ipc.Connection] = None):
self.conf = conf
self.shell = shell
self._conn = None
self._conn = conn
self._ws = None
self._ws_fetched = False
self._con = None
self._con_fetched = False
self._verbose = self.conf.get("_verbose", False)

@property
def conn(self) -> i3ipc.Connection:
if self._conn is None:
if self.conf["_verbose"]:
if self._verbose:
self._conn = VerboseConnection()
else:
self._conn = i3ipc.Connection()
return self._conn

@property
def ws(self) -> Optional[i3ipc.Con]:
if self._ws is None:
if not self._ws_fetched and self._ws is None:
self._ws = get_current_workspace(self.conn)
self._ws_fetched = True
return self._ws

@property
def select_mark(self) -> str:
def mark(self) -> str:
if self.shell is None:
raise RuntimeError("No shell defined")
return MARK_QT.format(self.shell)

def con_on_workspace(self, mark: str) -> Optional[i3ipc.Con]:
@property
def con(self) -> Optional[i3ipc.Con]:
"""Find container in complete tree"""
if not self._con_fetched and self._con is None:
node = self.conn.get_tree().find_marked(self.mark)
if len(node) == 0:
self._con = None
else:
self._con = node[0]
self._con_fetched = True
return self._con

def con_in_workspace(self, mark: str) -> Optional[i3ipc.Con]:
"""Find container in workspace"""
if self.ws is None:
return None
c = self.ws.find_marked(mark)
Expand All @@ -249,44 +265,31 @@ def con_on_workspace(self, mark: str) -> Optional[i3ipc.Con]:
return c[0]

def execvp(self, cmd):
if self.conf["_verbose"]:
if self._verbose:
print(f"execvp: {cmd}")
os.execvp(cmd[0], cmd)

"""Operations"""

def launch_inplace(self):
"""Quickterm is called by itself
Mark current window, move back and focus again, then run shell in current
process
"""

self.conn.command(f"mark {self.select_mark}")
self.conn.command(f"mark {self.mark}")

self.focus_on_current_ws()

prog_cmd = expand_command(self.conf["shells"][self.shell])
self.execvp(prog_cmd)

def toggle(self):
"""Toggle quickterm
def toggle_on_current_ws(self):
"""If on another workspace: hide, otherwise show on current"""
move_to_scratchpad(self.conn, f"[con_id={self.con.id}]")

If it does not exist: create()
Else:
hide();
if workspace was not current:
focus_on_current()
"""
qt_node = self.conn.get_tree().find_marked(self.select_mark)

if len(qt_node) == 0:
self.execute_term()
return

qt_node = qt_node[0]

move_to_scratchpad(self.conn, f"[con_id={qt_node.id}]")

if self.ws is not None and qt_node.workspace().name != self.ws.name:
if self.ws is not None and self.con.workspace().name != self.ws.name:
self.focus_on_current_ws()

def focus_on_current_ws(self):
Expand All @@ -309,17 +312,18 @@ def focus_on_current_ws(self):
posy = wy

self.conn.command(
f"[con_mark={self.select_mark}] "
f"[con_mark={self.mark}] "
f"move scratchpad, "
f"scratchpad show, "
f"resize set {width} px {height} px, "
f"move absolute position {posx}px {posy}px"
)

def execute_term(self):
"""Launch i3-quickterm in a new terminal"""
term = TERMS.get(self.conf["term"], self.conf["term"])
qt_cmd = f"{sys.argv[0]} -i {self.shell}"
if self.conf["_verbose"]:
if self._verbose:
qt_cmd += " -v"
if "_config" in self.conf:
qt_cmd += f" -c {self.conf['_config']}"
Expand All @@ -333,6 +337,41 @@ def execute_term(self):
self.execvp(term_cmd)


def run_qt(qt: Quickterm, in_place: bool = False):
"""Main logic"""
shell = qt.shell

if in_place:
if shell is None:
raise RuntimeError("shell should be provided when running in place")

# we are launched by ourselves: start a shell
qt.launch_inplace()
return

if shell is None:
c = qt.con_in_workspace(MARK_QT_PATTERN)
if c is not None:
# undefined shell and visible on workspace: hide
move_to_scratchpad(qt.conn, f"[con_id={c.id}]")
return

# undefined shell and nothing on workspace: ask for shell selection
shell = select_shell(qt.conf)
if shell is None:
return
qt.shell = shell

# show logic
# if it does not exist: create
# else: toggle on current workspace
if qt.con is None:
qt.execute_term()
return

qt.toggle_on_current_ws()


def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--in-place", dest="in_place", action="store_true")
Expand Down Expand Up @@ -365,31 +404,7 @@ def main():

qt = Quickterm(conf, args.shell)

shell = args.shell

if args.in_place:
if shell is None:
raise RuntimeError("shell should be provided when running in place")

# we are launched by ourselves: start a shell
qt.launch_inplace()
return 0

if shell is None:
c = qt.con_on_workspace(MARK_QT_PATTERN)
if c is not None:
# undefined shell and visible on workspace: hide
move_to_scratchpad(qt.conn, f"[con_id={c.id}]")
return 0

# undefined shell and nothing on workspace: ask for shell selection
shell = select_shell(conf)
if shell is None:
return 0
qt.shell = shell

# main toggle logic
qt.toggle()
run_qt(qt)

return 0

Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
black==24.1.1
click==8.1.7
flake8==7.0.0
iniconfig==2.0.0
mccabe==0.7.0
mypy-extensions==1.0.0
packaging==23.2
pathspec==0.12.1
platformdirs==4.2.0
pluggy==1.4.0
pycodestyle==2.11.1
pyflakes==3.2.0
pytest==8.0.0
tomli==2.0.1
typing_extensions==4.9.0
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ license_files = LICENSE.txt

[bdist_wheel]
universal=0

[tool:pytest]
testpaths = tests

[flake8]
max-line-length = 88
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def find_version(*file_paths):
long_description_content_type="text/markdown",
url="https://github.com/lbonn/i3-quickterm",
author="lbonn",
author_email="bonnans.l@gmail.com",
author_email="github@lbonnans.net",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: End Users/Desktop",
Expand All @@ -52,7 +52,7 @@ def find_version(*file_paths):
python_requires=">=3.6",
install_requires=["i3ipc>=2.0.1"],
extras_require={
"dev": ["black", "flake8"],
"dev": ["black", "flake8", "pytest"],
},
entry_points={
"console_scripts": [
Expand Down
Loading

0 comments on commit 5a42ce1

Please sign in to comment.