Skip to content

Instantly share code, notes, and snippets.

@wbob
Created November 26, 2024 13:38

Revisions

  1. wbob created this gist Nov 26, 2024.
    36 changes: 36 additions & 0 deletions wsqlite
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/env bash
    #
    # simple sqlite-wrapper to have PAGER support
    #
    # install and chmod+x to ~/bin/wsqlite and load in ~/.profile PATH

    set -eE
    command -v sqlite3 >/dev/null || echo "sqlite3 not found, please install" || exit 1
    command -v tmux >/dev/null || echo "tmux not found, please install" || exit 1

    usage() {
    echo "usage: wsqlite <sqlite.db>"
    }
    test -z $1 && usage && exit 0

    target="$1"
    fullpath=$(readlink -f .)
    fifo="$fullpath/sqlitepager.tmp"
    rc="$fullpath/sqlitepager.rc"
    test -p "$fifo" && rm -v "$fifo"
    mkfifo "$fifo"

    test -f "$rc" && rm -v "$rc"
    echo ".output \"$fifo\"" > "$rc"
    echo ".mode column" >> "$rc"
    cat "$rc"

    tmux kill-session -t "sqlite" >/dev/null || continue
    tmux new-session -s "sqlite" -d
    tmux split-window -d -v -t "sqlite"
    tmux send -t "sqlite.0" "sqlite3 -init \"$rc\" \"$target\"" ENTER
    tmux send -t "sqlite.1" "less -f -S \"$fifo\"" ENTER
    tmux attach-session -d -t "sqlite.0" -c "ls"

    rm "$fifo"
    rm "$rc"