#!/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

# t  title
# f  filter
# n  normal
# h  highlighted
# s  selected
# sc scrollbar

    set fn "{{@@ font.mono      @@}} {{@@ font.size.small           @@}}"

    set tb "{{@@ color.bg       @@}}{{@@ opacity | clamp_to_hex @@}}"
    set tf "{{@@ accent_color   @@}}"

    set fb "{{@@ color.bg       @@}}{{@@ opacity | clamp_to_hex @@}}"
    set ff "{{@@ color.txt      @@}}"

    set nb "{{@@ color.bg       @@}}{{@@ opacity | clamp_to_hex @@}}"
    set nf "{{@@ color.txt      @@}}"

    set hb "{{@@ accent_color   @@}}"
    set hf "{{@@ accent_fg      @@}}"

#{%@@ if False @@%}#
# Hacks to allow runnning even if not templated
    set fn "Fira Code 12"

    set tf "#D9534F"
    set tb "#202020"

    set ff "#ffffff"
    set fb "#202020"

    set nf "#ffffff"
    set nb "#202020"

    set hf "#D9534F"
    set hb "#404040"
#{%@@ endif @@%}#

command -qs dhist &&
    set wraper dhist wrap

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

# vim: ft=fish
