mbxutils/bin/mbx
2021-10-21 22:59:27 +02:00

322 lines
10 KiB
Bash
Executable File

#!/bin/bash
# mbxutils: handy utilities for your X11 window manager.
# Copyright (C) 2021 Daniel Napora <napcok@gmail.com>
#
# 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"
}
###fgcolor=$(fg $bgcolor)
fg "$bgcolor"
#KOLOR FG
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
columns = ${columns:-1}
# 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_theme = ${icon_theme:-}
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
^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
^sep()
About <b>mbxutils</b>,mbx
^sep()
Logout dialog,mb-jgtools mblogout,system-shutdown
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
}
about() {
CONFIG_FILE=$(mktemp)
MENU_ITEMS=$(mktemp)
trap "rm -f ${CONFIG_FILE} ${MENU_ITEMS}" EXIT
eval $(xdotool getdisplaygeometry --shell)
twidth=$((WIDTH/4))
theight=$((HEIGHT/4))
THUMB="$(DISPLAY=:0 scrot -t ${twidth}x${theight} -o $HOME/.config/mbxutils/mbx.png -e 'echo $m')"
# TODO better menu height calculation
columns=2
icon_size=$((font_size*item_height_factor/100-2))
menu_halign=center
menu_padding=8
menu_width=$((twidth+menu_padding*2))
item_height=$((font_size*item_height_factor/100))
menu_padding_top=$((theight+menu_padding*2+40))
menu_height_max=$((menu_padding_top+menu_padding+item_height*7+sep_height*6))
menu_height_min=$((menu_padding_top+menu_padding+item_height*7+sep_height*6))
configure
cat <<EOF > ${MENU_ITEMS}
@icon,,${menu_padding},${menu_padding},${twidth},${theight},4,left,top,,,$THUMB
@text,,$((menu_padding+20)),$((menu_padding+theight)),${twidth},25,4,left,top,${fgcolor},#DDDDDD,<span font="20px"><b>mbxutils</b></span><span font="12px"> 0.2.0 (technical preview)</span>
@text,,$((menu_padding+40)),$((menu_padding+theight+20)),${twidth},25,4,left,top,${fgcolor} 60,${fgcolor} 12,<span font="12px"><i>a set of lightweight and handy utilities for your X11 window manager</i></span>
#^sep(mbxutils <span font-weight="normal"><sub><i>handy utils for your X11 window manager</i></sub></span>)
^sep(Utilities)
Main menu,mbx mmenu,menu
Left SidePanel,mbx lpanel,arrow-left
Right SidePanel,mbx rpanel,arrow-right
Quick Filebrowser,mbx files,folder
Preferences,^pipe(mbx preferences mmenu),firefox
^sep(Manual)
Configure keybindings,xdg-open https://mbxutils.maboxlinux.org,firefox
Roadmap
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(Appearance)")
out+=("<tt>$(printf "%-15s %24s" "Font:" "[<b>$font</b>]")</tt>,^pipe($ME fonts $1)")
out+=("<tt>$(printf "%-15s %24s" "Font size (px):" "[<b>$font_size</b>]")</tt>,^checkout(fontsize)")
out+=("<tt>$(printf "%-15s %24s" "Line height:" "[<b>$item_height_factor%</b>]")</tt>,^checkout(lineheight)")
out+=("<tt>$(printf "%-15s %24s" "BGcolor:" "[<b>$bgcolor</b>]")</tt>,^checkout(bgcolor)")
out+=("^sep(Other)")
out+=("<tt>$(printf "%-15s %24s" "Terminal:" "[<b>$terminal</b>]")</tt>,^checkout(terminal),terminal")
out+=("<tt>$(printf "%-15s %24s" "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