File: ptui.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # ptui [options...]
  27 #
  28 # Podman Text User-Interface lets you run `podman` commands, picking options
  29 # and images/containers interactively using `fzf`.
  30 #
  31 # It's always possible to cancel any command or item-picking, and do nothing
  32 # as a result.
  33 #
  34 # The extra options are, available both in single and double-dash versions
  35 #
  36 #   -h, -help    show this help message
  37 
  38 
  39 case "$1" in
  40     -h|--h|-help|--help)
  41         awk '/^# +ptui /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  42         exit 0
  43     ;;
  44 
  45     --) shift ;;
  46 
  47     -*)
  48         printf "unsupported option '%s'\n" "$1" >&2
  49         exit 1
  50     ;;
  51 esac
  52 
  53 if ! command -v fzf > /dev/null; then
  54     printf "fzf (interactive picker) not found\n" >&2
  55     exit 2
  56 fi
  57 
  58 keys='ctrl-a:select-all,ctrl-space:toggle,F10:abort,F12:abort'
  59 picker="fzf --reverse --header-first --header-lines=1 --bind ${keys}"
  60 
  61 # choose top-level command
  62 
  63 msg='Podman Text User-Interface (ptui): pick a podman command to run'
  64 data="$(podman 2> /dev/null)"
  65 [ -z "${data}" ] && exit 1
  66 
  67 choice="$(echo "${data}" | grep '^  *[a-z]' | ${picker} --header="${msg}")"
  68 [ -z "${choice}" ] && exit 1
  69 
  70 choice1="$(echo "${choice}" | awk '{ print $1 }')"
  71 
  72 case "${choice1}" in
  73     list|ls|ps)
  74         cmd="podman ${choice1}"
  75         printf "\033[7m%s\033[0m\n" "${cmd}"
  76         ${cmd}
  77         exit $?
  78     ;;
  79 esac
  80 
  81 # choose 2nd-level command
  82 
  83 msg="Podman Text User-Interface (ptui): podman ${choice1}"
  84 data="$(podman ${choice1} 2> /dev/null)"
  85 [ -z "${data}" ] && exit 1
  86 
  87 choice="$(echo "${data}" | grep '^  *[a-z]' | ${picker} --header="${msg}")"
  88 [ -z "${choice}" ] && exit 1
  89 
  90 choice2="$(echo "${choice}" | awk '{ print $1 }')"
  91 
  92 case "${choice2}" in
  93     list|ls|ps)
  94         cmd="podman ${choice1} ${choice2}"
  95         printf "\033[7m%s\033[0m\n" "${cmd}"
  96         ${cmd}
  97         exit $?
  98     ;;
  99 esac
 100 
 101 # choose items to act on
 102 
 103 msg="Podman Text User-Interface (ptui): podman ${choice1} ${choice2}"
 104 data="$(podman ${choice1} ls 2> /dev/null)"
 105 [ -z "${data}" ] && exit 1
 106 
 107 targets="$(echo "${data}" | ${picker} -m --header="${msg}")"
 108 [ -z "${targets}" ] && exit 1
 109 
 110 # find which field/position is the ID, since these are shown in different
 111 # places by default, depending on the subcommand
 112 pos="$(
 113     echo "${data}" | awk '
 114         NR == 1 { for (i = 1; i <= NR; i++) if ($i == "ID") break }
 115         NR == 1 { print i - 1; exit }
 116     '
 117 )"
 118 
 119 ids="$(echo "${targets}" | awk -v pos="${pos}" '{ printf " %s", $pos }')"
 120 [ -z "${ids}" ] && exit 1
 121 
 122 cmd="podman ${choice1} ${choice2}${ids}"
 123 printf "\033[7m%s\033[0m\n" "${cmd}"
 124 ${cmd}