#!/usr/bin/env fish

# wrapper around bemenu
# bmenu *       - use as dmenu, -p for custom prompt (man bemenu)
# bmenu run     - select from .desktop files and run it
# bmenu start   - internal option

if test "$argv[1]" = "run"
    test -n "$argv[2]" && set t "$argv[2]" || set t "terminal"

    test -n "$i3SOCK" && set wrapper 'i3-msg exec --'
    test -n "$SWAYSOCK" && set wrapper 'swaymsg exec --'

    exec j4-dmenu-desktop \
        --dmenu="bmenu start -p Iniciar:" \
        --term "$t" \
        --wrapper="$wrapper" \
        --no-generic
end

if test -n "$SWAYSOCK"
    swaymsg -t get_tree |
        jq -je '..|select(.focused? and .fullscreen_mode? == 1)|""' &&
        swaymsg -q fullscreen off &&
        set fullscreen

    swaymsg -t get_outputs |
        jq -r 'map(.focused)|reverse|index(true)' |
        read focused_output

    test -n "$focused_output"
    and set focused_output "-m $focused_output"
end

function clean_exit
    set -q fullscreen
    and swaymsg -q fullscreen on &
end

trap clean_exit EXIT

set -l config "$HOME/.config/bmenu.conf"
if test -f $config
  source $config
end

dhist wrap -- bemenu \
    $focused_output\
    --ignorecase\
    --bottom\
    --no-overlap\
    --list 20\
    --prefix '>'\
    --fn "$fn"\
    --tb "$tb" --tf "$tf" \
    --fb "$fb" --ff "$ff" \
    --nb "$nb" --nf "$nf" \
    --ab "$ab" --af "$af" \
    --hb "$hb" --hf "$hf" \
    $argv

# vim: ft=fish