Use tmux and vim configs. Fixes #1. Fixes #2.

This commit is contained in:
Théophile Bastian 2020-05-07 13:16:44 +02:00
parent b154c79ece
commit 0a9c03d1e1
3 changed files with 63 additions and 21 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
virtualenv virtualenv
venv venv
*.pyc *.pyc
test

View file

@ -3,6 +3,7 @@ import pathlib
import os import os
import sys import sys
import time import time
import tempfile
import patch import patch
from tmux import TmuxSession from tmux import TmuxSession
@ -35,25 +36,23 @@ def get_patch(patch_path):
return patch.PatchSet(stream) return patch.PatchSet(stream)
def main(): def configure_home(home):
""" Entry-point function """ """ Configures the temporary home """
args = parse_args() tmux_conf_path = home / ".tmux.conf"
vim_conf_path = home / ".vimrc"
if args.directory: print("Configuring tmux in {}".format(tmux_conf_path))
chdir_to = pathlib.Path(args.directory) with tmux_conf_path.open("w") as tmux_conf:
if not chdir_to.is_dir(): tmux_conf.write("set -g status off\n")
print( with vim_conf_path.open("w") as vim_conf:
( vim_conf.write(
"The required working directory '{}' does not exist, or is not a " "syntax on\nset bg=dark\nset number\nset ts=4\nset sw=4\nset et\n"
"directory."
).format(str(chdir_to)),
file=sys.stderr,
) )
sys.exit(1)
os.chdir(chdir_to)
patchset = get_patch(args.patch) return tmux_conf_path, vim_conf_path
def apply_patchset(patchset):
tmux_session = TmuxSession.create_detached() tmux_session = TmuxSession.create_detached()
tmux_fg_process = tmux_session.process_attach(read_only=True) tmux_fg_process = tmux_session.process_attach(read_only=True)
time.sleep(0.2) time.sleep(0.2)
@ -74,5 +73,34 @@ def main():
tmux_session.session.kill_session() tmux_session.session.kill_session()
def main():
""" Entry-point function """
args = parse_args()
patchset = get_patch(args.patch)
if args.directory:
chdir_to = pathlib.Path(args.directory)
if not chdir_to.is_dir():
print(
(
"The required working directory '{}' does not exist, or is not a "
"directory."
).format(str(chdir_to)),
file=sys.stderr,
)
sys.exit(1)
os.chdir(chdir_to)
with tempfile.TemporaryDirectory() as tmp_home_path:
os.environ["HOME"] = tmp_home_path
tmp_home = pathlib.Path(tmp_home_path)
tmux_conf, _ = configure_home(tmp_home)
TmuxSession.initialize_server(
socket_name="patch2vimedit", config_file=tmux_conf.as_posix()
)
apply_patchset(patchset)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -9,16 +9,29 @@ import time
class TmuxSession: class TmuxSession:
""" A tmux session """ """ A tmux session """
tmux_server = libtmux.Server() tmux_server = None
tmux_socket = None
def __init__(self, session): def __init__(self, session):
if self.tmux_server is None:
raise Exception("Server not initialized")
self.session = session self.session = session
self.session_id = session.id self.session_id = session.id
self.keyboard_speed = 0.01 self.keyboard_speed = 0.00001
@classmethod
def initialize_server(cls, socket_name=None, config_file=None):
""" Initialize the tmux server """
cls.tmux_socket = socket_name
cls.tmux_server = libtmux.Server(
socket_name=socket_name, config_file=config_file
)
@classmethod @classmethod
def create_detached(cls, name=None): def create_detached(cls, name=None):
""" Create a new detached tmux session, returning the TmuxSession """ """ Create a new detached tmux session, returning the TmuxSession """
if cls.tmux_server is None:
raise Exception("Server not initialized")
if not name: if not name:
name = "patch2vimsession-{:04x}".format(random.randint(0, 1 << 16 - 1)) name = "patch2vimsession-{:04x}".format(random.randint(0, 1 << 16 - 1))
session = cls.tmux_server.new_session(session_name=name, attach=False) session = cls.tmux_server.new_session(session_name=name, attach=False)
@ -32,8 +45,7 @@ class TmuxSession:
def attach_ro(self): def attach_ro(self):
""" Same as `attach`, but attaches a tmux session in a read-only fashion (see """ Same as `attach`, but attaches a tmux session in a read-only fashion (see
`tmux attach -r`). """ `tmux attach -r`). """
# libtmux is not expressive enough to avoid `subprocess` here... self.tmux_server.cmd("attach", "-r", "-t", self.session.id)
subprocess.run(["tmux", "attach", "-r", "-t", self.session.id], check=True)
def session_exists(self): def session_exists(self):
""" Checks that the session exists """ """ Checks that the session exists """
@ -61,7 +73,8 @@ class TmuxSession:
def type_delay(self): def type_delay(self):
""" Introduce a small delay, to emulate a human typing """ """ Introduce a small delay, to emulate a human typing """
time.sleep(self.keyboard_speed + random.gauss(0, self.keyboard_speed * 0.1)) delay = self.keyboard_speed + random.gauss(0, self.keyboard_speed * 0.1)
time.sleep(delay)
def type_keys(self, *args): def type_keys(self, *args):
""" Same as `send_keys`, but emulates a human typing with pauses. This is """ Same as `send_keys`, but emulates a human typing with pauses. This is