Basic patch typing in Vim

This commit is contained in:
Théophile Bastian 2020-05-04 00:08:47 +02:00
parent 3b6d0e0fc6
commit 7f8a53e05c
2 changed files with 68 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import time
import patch
from tmux import TmuxSession
from vim_session import VimSession
def parse_args():
@ -55,13 +56,16 @@ def main():
tmux_session = TmuxSession.create_detached()
tmux_fg_process = tmux_session.process_attach(read_only=True)
time.sleep(0.2)
tmux_session.send_keys('echo "Hello, world!"', "enter")
vim_session = VimSession(tmux_session)
time.sleep(0.2)
vim_session.apply_patchset(patchset)
time.sleep(1)
tmux_session.send_keys('echo "Bye, world!"', "enter")
time.sleep(1)
tmux_session.send_keys("exit", "enter")
vim_session.quit()
time.sleep(0.5)
tmux_session.type_keys("exit", "enter")
tmux_fg_process.join(1)
if tmux_fg_process.exitcode is None: # Did not terminate -- kill it

View File

@ -0,0 +1,59 @@
import sys
class VimSession:
""" A Vim session instrumented through tmux """
def __init__(self, tmux_session, file_path=None):
self.tmux_session = tmux_session
self.file_path = file_path
self.tmux_session.type_keys("vim")
if self.file_path:
self.tmux_session.type_keys(" {}".format(self.file_path))
self.tmux_session.type_keys("enter")
self.tmux_session.send_keys(":set paste", "enter")
def edit_file(self, file_path):
self.tmux_session.type_keys("escape", ":e ", file_path, "enter")
self.file_path = file_path
def apply_patchset(self, patchset):
print("Applying patchset…", file=sys.stderr)
for patch in patchset:
print("Asking to apply patch…", file=sys.stderr)
self.apply_patch(patch)
def apply_patch(self, patch):
source = patch.source.decode("utf8")
target = patch.target.decode("utf8")
print(
"Applying patch on {} -> {}.".format(source, target), file=sys.stderr,
)
if source != target:
self.tmux_session.type_keys(
"escape", ":!mv ", source, " ", target, "enter",
)
if self.file_path != target:
self.edit_file(target)
for hunk in patch:
self.apply_hunk(hunk)
self.tmux_session.type_keys("escape", ":w", "enter")
def apply_hunk(self, hunk):
# So far, very naive.
self.tmux_session.type_keys("escape", "{}G0".format(hunk.starttgt))
for b_line in hunk.text:
line = b_line.decode("utf8")
if line[0] == " ":
self.tmux_session.type_keys("j")
elif line[0] == "-":
self.tmux_session.type_keys("dd")
elif line[0] == "+":
self.tmux_session.type_keys("O", line.strip()[1:], "escape", "j")
def quit(self):
self.tmux_session.type_keys("escape", ":q", "enter")