#!/bin/env fish
function chooseDisk -a outFile message selectScript
    if [ -z "$selectScript" ]
        set selectScript "$(status dirname)/select.fish"
    end

    source "$selectScript"

    while true
        set -l header
        set -l disks

        lsblk -do NAME,SIZE,TYPE | begin
            read header

            cat | while read disk
                set -l diskInfo (string split -n " " $disk)

                if contains "$diskInfo[3]" disk
                    set -a disks "$disk"
                end
            end
        end

        select "$header" "$outFile" "$message" "No valid disk found!" "$(string collect $disks)" false
        and begin
            set -l disk (string split -n " " (cat "$outFile"))
            echo "/dev/$disk[1]" >$outFile
            return
        end
    end
end

chooseDisk $argv