#!/bin/bash
# interactively remove docker images using fzf

if command -v docker &>/dev/null
then
    docker=docker
elif command -v podman &>/dev/null
then
    docker=podman
else
    echo >&2 "Neither docker nor podman found"
    exit 127
fi
command -v fzf >/dev/null    || { echo fzf not found;    exit 127; }

IFS=$'\t\n'
while imglines=$($docker image ls --noheading | fzf -m --prompt="Select image(s) to delete (use tab to select multiple)> " --nth=1,2,3); do
    while read -r imgline; do
        img=$(awk '/[<]none[>] *[<]none[>]/ { print $3; exit }
                                            { print $1":"$2  }' <<<$imgline)
        $docker rmi "$img" || read -r -p"exited $?; hit enter to continue..."
    done <<< "$imglines"
done
