From 742c479dcdab14b0f97cec0c0fc0dd1b6345472a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sun, 10 May 2020 16:32:23 +0200 Subject: [PATCH] Add logging facilities --- patch2vimedit/main.py | 35 ++++++++++++++++++++++++++++++++++- patch2vimedit/tmux.py | 19 ++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/patch2vimedit/main.py b/patch2vimedit/main.py index 5a3b4e4..8e0c93f 100644 --- a/patch2vimedit/main.py +++ b/patch2vimedit/main.py @@ -8,11 +8,21 @@ import tempfile import patch from tmux import TmuxSession from vim_session import VimSession +import logging + + +logger = logging.getLogger(__name__) def parse_args(): """ Parse command-line arguments """ parser = argparse.ArgumentParser(prog="patch2vimedit") + parser.add_argument( + "-g", + "--debug", + action="store_true", + help="Enable debug logging to /tmp/patch2log", + ) parser.add_argument( "-C", "--directory", @@ -46,7 +56,19 @@ def configure_home(home): tmux_conf.write("set -g status off\n") with vim_conf_path.open("w") as vim_conf: vim_conf.write( - "syntax on\nset bg=dark\nset number\nset ts=4\nset sw=4\nset et\nset so=5\n" + """ +syntax on +set bg=dark +set number +set ts=4 +set sw=4 +set et +set so=5 +set noautoindent +set nosmartindent +set nocindent +set indentexpr& +""" ) return tmux_conf_path, vim_conf_path @@ -73,10 +95,21 @@ def apply_patchset(patchset): tmux_session.session.kill_session() +def configure_log(args): + if args.debug: + logging.basicConfig(filename="/tmp/patch2log", level=logging.DEBUG) + else: + logging.basicConfig(level=logging.CRITICAL) + + logging.getLogger("libtmux").setLevel(logging.INFO) + + def main(): """ Entry-point function """ args = parse_args() + configure_log(args) + patchset = get_patch(args.patch) if args.directory: diff --git a/patch2vimedit/tmux.py b/patch2vimedit/tmux.py index a6ba356..c75dee7 100644 --- a/patch2vimedit/tmux.py +++ b/patch2vimedit/tmux.py @@ -1,9 +1,11 @@ -import subprocess import multiprocessing -import sys import random -import libtmux import time +import logging +import libtmux + + +logger = logging.getLogger(__name__) class TmuxSession: @@ -15,10 +17,13 @@ class TmuxSession: def __init__(self, session): if self.tmux_server is None: raise Exception("Server not initialized") + self.session = session self.session_id = session.id self.keyboard_speed = 0.00001 + self.sendkey_log_buffer = [] + @classmethod def initialize_server(cls, socket_name=None, config_file=None): """ Initialize the tmux server """ @@ -67,6 +72,14 @@ class TmuxSession: # Weirdly, `tmux send-keys 'blah;'` doesn't send the semicolon; and so # does `tmux send-keys ';'`. We must escape it with a backslash. arg = arg[:-1] + r"\;" + + if arg in ["escape", "enter"]: + logline = "".join(self.sendkey_log_buffer) + " " + arg + logger.debug(logline) + self.sendkey_log_buffer = [] + else: + self.sendkey_log_buffer.append(arg) + self.session.attached_pane.send_keys( arg, suppress_history=False, enter=False )