20210103, Iterative Development
--

Some years back, I was impressed by Bret Victor's presentation, /Inventing on
Principle/ [2]. But it seemed like a lot of work to set up each scenario.

This note describes an approach that is similar but general-purpose.



Tools,

    A terminal multiplexor. I use tmux. [3]

    A text editor. I use vim.

    A shell. I use bash.

Explaining the layout,

    We use tmux to split our terminal window into two panes.

    We run a script-launcher in the left pane, vexec. (source below)

    We run a text editor in the right pane. We configure hotkeys in this
    editor (right) that cycle the script (left).

    In the right editor, we regularly edit the script that gets run on the
    left.

This allows high iteration akin to the javascript book in the Victor
presentation.


// Vexec

This is a script that launches another script each time you press enter.

This is a wrapper for the script that runs on the left,

    $ cat ~/bin/vexec
    #!/bin/bash

    if [[ "1" != "$#" ]]; then
        echo "Usage:"
        echo "  $0 app"
        exit 1
    fi
    TARGET=$1

    pause() {
        echo "[paused]"
        read _X
    }

    main() {
        while [[ "1" == "1" ]]; do
            clear
            $TARGET
            pause
        done
    }

    main

    $

If you are setting this up, make sure you use chmod to make it executable, and
put its directory into your PATH.

    e.g. chmod +x ~/bin/vexec; export PATH=$PATH:$HOME/bin

I can't remember why it is called vexec.


// Launch script

Make a launch script, app.

    $ touch app
    $ chmod +x app
    $ vexec ./app

Edit app in the right screen.

Put whatever you like in it. For example, if you are building a C application,
it might look like this,

    #!/bin/bash
    #
    #make extra_headers
    #make yacc_stuff
    make build
    ./exe

Comment and uncomment as you need to. That's it.


// Hotkeys

Significant hotkeys,

    ctrl_a, \       tmux splits the window into two, vertically.

    ctrl_a, s       tmux switches focus between its panes

    ctrl_a, o       tmux rotates its panes

    space, enter    vim sends /enter/ to the left tmux pane.

    space, \        vim sends /ctrl+c/ to the left tmux pane.

Vim is configured to treat space as its 'leader' key.

In .vimrc,

    set hidden              " edit multiple files without forced save
    set notimeout           " affects leader key timeout
    let mapleader=" "       " sets the leader key to space

    nmap <leader>\ :execute 'silent !tmux send-keys -t left C-c'<CR>
    nmap <leader><CR> :execute 'silent !tmux send-keys -t left C-m'<CR>

    nmap <leader>e :e       " convenience for opening another file for edit
    nmap <leader>e :w<CR>   " convenience for save
    nmap <leader>h :bp<CR>  " edit the previous file
    nmap <leader>l :bn<CR>  " edit the next file

In .tmux.conf,

    # Change the control character to be C-a.
    unbind C-b
    set -g prefix C-a

    # Allow for horizontal and vertical pane splits.
    bind - split-window -v -c "#{pane_current_path}"
    bind \ split-window -h -c "#{pane_current_path}"

The default ctrl character in tmux is ctrl_b, but the text above remaps it to
ctrl_a (like gnu-screen).

If you are using emacs, you probably want to set it to something else again,
as both ctrl_a and ctrl_b are default binds to useful feature in emacs.


// Wrapup

This approach allows me to rapidly iterate for such tasks as,

    Building systems in compiled languages, and working quickly through
    compile errors.

    Building systems in dynamic languages, and working quickly through test
    case failures.

    Writing and releasing documentation.

    Compiling, staging and publishing website content.

Building the workflow for a new project takes seconds.

A nice side-effect: common use cases accumulate as comments in the 'app' file.
This helps when returning to a project after time away.

If somebody worked out an emacs config equivalent to the vim config above, I
would be happy to add those notes to this page.



:1 Mikel Evins, /On repl-driven programming/,
    http://mikelevins.github.io/posts/2020-12-18-repl-driven/

:2 Bret Victor, /Inventing on Principle/, 
    https://www.youtube.com/watch?v=8QiPFmIMxFc&ab_channel=LunaProgrammingLanguageTutorials

:3 Tmux. Think of this as a window manager for your terminal.
    
    Here is an intro,
    https://www.youtube.com/watch?v=Lqehvpe_djs&ab_channel=IppSec

    The Pragmatic Programmer references on tmux is a thin and excellent book.
    You should be able to move from nothing to a nice setup in about an hour.

    Some other tools that serve a similar role: gnu-screen, dvtm/abduco.