mbxutils/bin/mbx
2021-10-20 20:36:53 +02:00

329 lines
9.9 KiB
Bash
Executable File

#!/bin/bash
# mbxutils: handy utilities for your X11 window manager.
#
# Copyright (C) 2021 Daniel Napora <napcok@gmail.com>
# mbx
# TODO Preferences standalone
# TODO Logout Dialog postponed
# TODO Filebrowser standalone postponed
ME=$(basename "$0")
CNF_DIR="$HOME/.config/mbxutils"
CNF_FILE="$CNF_DIR/$ME.conf"
[ ! -d "$HOME/.config/jgmenu" ] && mkdir -p "$HOME/.config/jgmenu"
[ ! -d "$CNF_DIR" ] && mkdir -p "$CNF_DIR" && cp -r /usr/share/mbxutils/* "$CNF_DIR"/
if [ ! -f $CNF_FILE ]; then
cat <<EOF > ${CNF_FILE}
menu_padding=5
item_height_factor=200
font="Noto Sans"
font_size=12
EOF
fi
source <(grep = $CNF_FILE)
fg(){
r=$(printf "%d" 0x${1:1:2}) g=$(printf "%d" 0x${1:3:2}) b=$(printf "%d" 0x${1:5:2})
rgb="$((r*299+g*587+b*114))"
[ "$rgb" -gt 138000 ] && fgcolor="#222222" color_title_fg="#111111" color_sel_fg="#111111" color_sep_fg="#222222 20"|| fgcolor="#EEEEEE" color_title_fg="#FFFFFF" color_sel_fg="#FFFFFF"
[ "$rgb" -gt 200000 ] && color_sel_bg="#222222 8" color_sel_border="#222222 16" color_title_bg="#222222 8"
### [ "$((r*299+g*587+b*114))" -gt 149000 ] && echo "#111111" || echo "#EEEEEE"
}
###fgcolor=$(fg $bgcolor)
fg "$bgcolor"
#KOLOR FG
### DDEV ONLY
rgb() {
r=$(printf "%d" 0x${1:1:2}) g=$(printf "%d" 0x${1:3:2}) b=$(printf "%d" 0x${1:5:2})
echo "rgb($r, $g, $b)"
}
color=$(rgb $fgcolor)
background=$(rgb $bgcolor)
function luminance()
{
R=$( echo $1 | awk -F, '{print substr($1,5)}' )
G=$( echo $1 | awk -F, '{print $2}' )
B=$( echo $1 | awk -F, '{n=length($3); print substr($3,1,n-1)}' )
echo "scale=4
rsrgb=$R/255
gsrgb=$G/255
bsrgb=$B/255
if ( rsrgb <= 0.03928 ) r = rsrgb/12.92 else r = e( 2.4 * l((rsrgb+0.055)/1.055) )
if ( gsrgb <= 0.03928 ) g = gsrgb/12.92 else g = e( 2.4 * l((gsrgb+0.055)/1.055) )
if ( bsrgb <= 0.03928 ) b = bsrgb/12.92 else b = e( 2.4 * l((bsrgb+0.055)/1.055) )
0.2126 * r + 0.7152 * g + 0.0722 * b" | bc -l
}
lum1=$( luminance $color )
lum2=$( luminance $background )
# compute contrast
function contrast()
{
echo "scale=2
if ( $1 > $2 ) { l1=$1; l2=$2 } else { l1=$2; l2=$1 }
(l1 + 0.05) / (l2 + 0.05)" | bc
}
rel=$( contrast $lum1 $lum2 )
configure() {
if [ -f /usr/share/mbxutils/mbxutils.inc ];then
. /usr/share/mbxutils/mbxutils.inc
else
cat <<EOF > ${CONFIG_FILE}
position_mode = ${position_mode:-fixed}
csv_cmd = apps
stay_alive = 0
terminal_exec = ${terminal:-terminator}
terminal_args = -e
hover_delay = 80
# MENU
menu_halign = ${menu_halign:-left}
menu_valign = ${menu_valign:-center}
menu_margin_x = ${menu_margin_x:-4}
menu_margin_y = ${menu_margin_y:-4}
sub_spacing = ${sub_spacing:-2}
menu_width = ${menu_width:-20}
menu_border = 1
menu_height_min = ${menu_height_min:-0}
menu_height_max = ${menu_height_max:-0}
menu_padding_top = ${menu_padding_top:-5}
menu_padding_right = ${menu_padding:-5}
menu_padding_bottom = ${menu_padding:-5}
menu_padding_left = ${menu_padding:-5}
menu_radius = ${menu_radius:-1}
# ITEMS
item_height = $((font_size*item_height_factor/100))
item_halign = ${item_halign:-left}
item_padding_x = 2
item_margin_x = ${item_margin_x:-3}
item_margin_y = ${item_margin_y:-3}
item_radius = ${item_radius:-2}
item_border = ${item_border:-1}
arrow_string = ${arrow_string:-▸}
hide_back_items = ${hide_back_items:-0}
# COLORS
color_sel_bg = ${color_sel_bg:-}
color_sel_fg = ${color_sel_fg:-}
color_sel_border = ${color_sel_border:-#eeeeee 40}
color_norm_fg = ${fgcolor:-}
color_title_fg = ${color_title_fg:-}
color_menu_bg = ${bgcolor:-#0F2437}
color_menu_border = #eeeeee 8
color_title_bg = ${color_title_bg:-"$fgcolor 12"}
color_title_border = ${color_title_border:-}
color_sep_fg = ${color_sep_fg:-}
icon_size = ${icon_size:-0}
icon_norm_alpha = 70
icon_sel_alpha = 100
icon_text_spacing = $((icon_size/4))
font_size = ${font_size:-12}
font = ${font} ${font_size}px
sep_markup = weight="bold"
sep_halign = ${sep_halign:-center}
sep_height = ${sep_height:-3}
EOF
fi
}
mmenu() {
CONFIG_FILE=$(mktemp)
trap "rm -f ${CONFIG_FILE}" EXIT
font_size=${font_size:-12}
position_mode=${1:-pointer}
hide_back_items=1
## poniższe defaultowo do configu
menu_padding=${menu_padding:-5}
menu_padding_top=$((font_size*item_height_factor/100+menu_padding))
icon_size=$((font_size*item_height_factor/100-2))
#CHujowy HAK
#apps_in_submenu=true
rm -f ~/.config/jgmenu/append.csv
if [[ $apps_in_submenu == true ]]
then
cat <<EOF > $HOME/.config/jgmenu/prepend.csv
@text,,$((menu_padding + 24)),$((menu_padding_top-font_size*item_height_factor/100)),150,20,0,left,top,auto,#000000,<span size="large">🔍</span>
@search,,$((menu_padding + 38)),$((menu_padding + 4)),150,20,2,left,top,auto,#000000 0,<i>type to search</i>
. ~/.config/mbxutils/inc/favorites.csv
$APPS,^checkout(apps),applications-other
. ~/.config/mbxutils/inc/m_after_apps.csv
^sep()
Logout dialog,mb-jgtools mblogout,system-shutdown
Preferences,^pipe($ME preferences mmenu),firefox
^sep(rgb: $rgb)
^sep(rel: $rel)
^tag(apps)
EOF
else
cat <<EOF > $HOME/.config/jgmenu/prepend.csv
@text,,$((menu_padding+20)),$((menu_padding_top-font_size*item_height_factor/100)),150,$((font_size*item_height_factor/100)),0,left,top,auto,#000000,<span size="large">🔍</span>
@search,,$((menu_padding + 38)),$((menu_padding_top-font_size*item_height_factor/100)),150,$((font_size*item_height_factor/100)),2,left,top,auto,#000000,<i><u> type to search </u></i>
. ~/.config/mbxutils/inc/favorites.csv
EOF
cat <<EOF > $HOME/.config/jgmenu/append.csv
. ~/.config/mbxutils/inc/m_after_apps.csv
^sep()
Preferences,^pipe($ME preferences mmenu),firefox
Logout dialog,mb-jgtools mblogout,system-shutdown
^sep(rgb: $rgb)
^sep(rel: $rel)
EOF
fi
configure
jgmenu --config-file=${CONFIG_FILE} --no-append 2>/dev/null
}
lpanel() {
CONFIG_FILE=$(mktemp)
MENU_ITEMS=$(mktemp)
trap "rm -f ${CONFIG_FILE} ${MENU_ITEMS}" EXIT
menu_halign="left"
configure
cat <<EOF > ${MENU_ITEMS}
. ~/.config/mbxutils/inc/ltop.csv
^sep(<b>File browser</b>)
/etc,^pipe(mbxfiles /etc)
~/.config,^pipe(mbxfiles ~/.config)
^sep(<b>mbxutils</b>)
Left panel,,firefox
. ~/.config/mbxutils/inc/lbottom.csv
EOF
jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
}
rpanel() {
CONFIG_FILE=$(mktemp)
MENU_ITEMS=$(mktemp)
trap "rm -f ${CONFIG_FILE} ${MENU_ITEMS}" EXIT
menu_halign="right"
icon_size=$((font_size*item_height_factor/100-2))
configure
cat <<EOF > ${MENU_ITEMS}
. ~/.config/mbxutils/inc/rtop.csv
^sep(Right Panel)
Right Panel,,terminal
. ~/.config/mbxutils/inc/rbottom.csv
EOF
jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
}
setvar() {
search="$(echo $1|cut -d= -f1)="
if grep -Rq $search $CNF_FILE
then #found
sed -i s/^"$search".*$/"$1"/ $CNF_FILE
else #not found
echo $1 >> $CNF_FILE
fi
}
preferences() {
# if $1 brak then standalone
PAR="${ME} $1"
out+=("^sep(mbxutils Global Settings)")
out+=("^sep(rgb: $rgb)")
out+=("^sep(rel: $rel)")
out+=("^sep(Appearance)")
out+=("<tt>$(printf "%-15s %20s" "Font:" "[<b>$font</b>]")</tt>,^pipe($ME fonts $1)")
out+=("<tt>$(printf "%-15s %20s" "Font size (px):" "[<b>$font_size</b>]")</tt>,^checkout(fontsize)")
out+=("<tt>$(printf "%-15s %20s" "Line height:" "[<b>$item_height_factor%</b>]")</tt>,^checkout(lineheight)")
out+=("<tt>$(printf "%-15s %20s" "BGcolor:" "[<b>$bgcolor</b>]")</tt>,^checkout(bgcolor)")
out+=("^sep(Other)")
out+=("<tt>$(printf "%-15s %20s" "Terminal:" "[<b>$terminal</b>]")</tt>,^checkout(terminal),terminal")
out+=("<tt>$(printf "%-15s %20s" "File Manager:" "[<b>$filemanager</b>]")</tt>,^checkout(filemanager)")
#out+=("<tt>$(printf "%-15s %20s" "Margin (px):" "[<b>$margin_xy</b>]")</tt>,^checkout(margin)")
out+=("^sep()")
out+=("Edit config file,xdg-open $CNF_FILE")
out+=("Open config dir,xdg-open $CNF_DIR")
#tags
out2+=("^tag(fontsize)")
out2+=("^sep(Font size)")
for i in 10 11 12 13 14 15 16 18 20; do out2+=("$(printf "<tt>%4s</tt>,%s" "$i" "$ME setvar font_size=${i};$PAR fixed")");done
out2+=("^tag(lineheight)")
for i in 120 140 160 180 200 240 280; do out2+=("$(printf "<tt>%4s%%</tt>,%s" "$i" "$ME setvar item_height_factor=${i};$PAR fixed")");done
out2+=("^tag(bgcolor)")
out2+=("^sep(Select BGcolor)")
out2+=("Pick color from screen")
colors="$CNF_DIR/inc/colors.clr"
while read -r color
do
fg $color
out2+=("<tt><span bgcolor='$color' fgcolor='$fgcolor'><sub> </sub>$color<sup> $rgb</sup></span></tt>,mbx setvar bgcolor='$color';$PAR fixed")
done < "$colors"
out2+=("^tag(terminal)")
out2+=("^sep(Select Terminal)")
terminals="$CNF_DIR/inc/terminals.inc"
while read -r term
do
out2+=("$term,mbx setvar terminal=$term;$PAR fixed")
done < "$terminals"
out2+=("^tag(filemanager)")
out2+=("^sep(Select File Manager)")
filemanagers="$CNF_DIR/inc/file_managers.inc"
while read -r fm
do
out2+=("$fm,mbx setvar filemanager=$fm;$PAR fixed")
done < "$filemanagers"
if [[ -z $1 ]]; then
echo "standalone"
exit 0
else
printf '%s\n' "${out[@]}"
printf '%s\n' "${out2[@]}"
fi
}
fonts(){
PAR="${ME} $1"
echo "^sep(Font)"
while read -r x
do
echo "<span size=\"x-large\" font_family=\"$x\">$x</span>,$ME setvar \"font=\'${x}\'\";$PAR fixed"
done < "$CNF_DIR"/inc/fonts.inc
}
case "$1" in
mmenu) mmenu "$2";;
lpanel) lpanel;;
rpanel) rpanel;;
preferences) preferences "$2";;
fonts) fonts "$2";;
setvar) setvar "$2";;
*) about;;
esac
exit 0