but soft, yonder princess

The other day I was playing with tmux and learned that I can drive stuff in remote windows. But for doing this programmatically, the right tool is Python, as usual. Specifically, the tmuxp library, which includes an interactive shell and a parser for readable config-files.

I've settled on this tmuxp configuration for starting the tmux session for working on stuff in this web tree:

auto/but-soft-yonder-princess/nikola.yaml (Source)

session_name: nikola
start_directory: ~/nikola/robtasm
windows:
  - window_name: git
    start_directory: ~/nikola/robtasm
    panes:
      - shell_command: git status
  - window_name: make
    panes:
      - shell_command: make tmux
  - window_name: edit
    panes:
      - shell_command: emacsclient -a= -c .

Here make tmux is a wrapper that calls this screen-splitting script:

auto/but-soft-yonder-princess/make.py (Source)

#!/usr/bin/env python

import argparse
import libtmux
import os
import tmuxp

def build_parser():
    parser = argparse.ArgumentParser(
        'make.py',
        description="Start two 'make' processes in adjacent termux panes.",
    )

    parser.add_argument(
        '--kill-only', action="store_true",
        help="Don't create new windows; just nix the ones we have.",
    )

    parser.add_argument(
        '--window-name', type=str, default='make',
        help="The name of the window to match (default: 'make').",
    )

    return parser

def main():
    args = build_parser().parse_args()

    server = libtmux.Server()
    this_pane = pane = tmuxp.util.get_current_pane(server)
    if not pane:
        raise RuntimeError('not within tmux')

    if pane.window.name != 'make':
        raise RuntimeError("Please rename this window to 'make' to proceed.")

    for p in pane.window.panes:
        if p != pane:
            p.kill()

    if args.kill_only:
        return this_pane

    for command in [
            "make continuous",
            "make serve",
    ]:
            pane = pane.split()
            pane.window.select_layout('even-vertical')
            pane.send_keys(command)

    return this_pane

if __name__ == "__main__":
    main()

I've also got my Makefile auto-generating code listing links, which I'll have to detail later. I am having trouble deciding whether my Makefile is a thing of beauty, a monstrosity, or both; it's grown considerably since I wrote watching for changes.