#!/bin/bash # License GPLv3+: GNU GPL version 3 or later # This is free software; you are free to change and redistribute it. # There is NO WARRANTY, to the extent permitted by law. args=($@) if [[ -n "${args[@]}" ]]; then argsl=$(printf '%s\n' "${args[@]}") while read line; do case "$line" in -*) plugin_path="${line#-}" if [[ -f "$plugin_path" ]] && [[ -x "$plugin_path" ]]; then insert_tmp+=("$(sed -n "3 {s/# //p}" "$plugin_path")") else exit 1 fi ;; *) if [[ -d "$line" ]] && [[ -x "$line" ]]; then workdir="$line" export workdir else workdir="$HOME" fi ;; esac done <<< "$argsl" fi insert_cmd=("$(printf '%s\n' "${insert_tmp[@]}" | sort -rg -ti -k1)") if [[ -n "${insert_cmd[@]}" ]]; then fm_file=$(< /opt/scripts/fm_slmenu.sh) for x in "${!insert_cmd[@]}"; do fm_file="$(sed -e "${insert_cmd[x]}" <<< "$fm_file")" done bash <(echo "$fm_file") exit 0 fi apps_path="$HOME/.local/share/applications/" [[ -z "$workdir" ]] && workdir="$HOME" cd "$workdir" || exit slmenu_cmd () { slmenu "$1" -t -i -l 61 } check_root () { case "$(pwd)" in '/') pardir="" ;; *) pardir="../\n" ;; esac } input_short () { check_root short_ls () { ls -1 --group-directories-first -F; } printf "%s\n$pardir%s\n" "$(pwd)" "$(short_ls)" | slmenu_cmd } input_verbose () { check_root long_ls () { LC_ALL="C" ls -lAh --group-directories-first -F; } printf "%s\n$pardir%s\n" "$(pwd)" "$(long_ls)" | slmenu_cmd -d } type_cmd () { file -b --mime-type -- "$list" } tool_cmd () { sed -n "/^${type//\//\\/}/ {s@^.*=@$apps_path@;s/\;.*//p;q}" -- \ "$HOME/.config/mimeapps.list" } comppre_cmd () { sed -n '/\[Desktop Entry\]/,/^Exec=/!d;s/^Exec=//p' -- "$tool" } testterm_cmd () { grep -q '^Terminal=true' -- "$tool" } error_cmd () { echo "$list" >> tmp printf '%s' 'An error occurred. Please press Enter.' | slmenu_cmd > /dev/null } perm_cmd () { printf '%s' "$list: Permission denied" | slmenu_cmd > /dev/null } noapp_cmd () { printf '%s' "There's no app specified for $type in mimeapps.list" | slmenu_cmd > /dev/null } exec_terminal () { urxvt -e bash -c -- "$(comppre_cmd) -- $list" > /dev/null 2>&1 } exec_gui () { exec $(comppre_cmd) "$list" > /dev/null 2>&1 } sed_cmd () { sed -E 's/([,.:a-zA-Z0-9-]*[[:space:]]*){8}//' } while true; do list="$(input_short)"; [[ -z $list ]] && { clear && exit; } case "$list" in *"*") list="${list%?}";; esac; [[ -z $list ]] && exit if [[ -d "$list" ]]; then if [[ -x "$list" ]]; then cd -- "$list" || { error_cmd && continue; } else perm_cmd && continue fi elif [[ -f "$list" ]]; then [[ ! -r "$list" ]] && { perm_cmd && continue; } type="$(type_cmd)"; [[ -z $type ]] && { error_cmd && continue; } tool="$(tool_cmd)"; [[ ! -r $tool ]] && { noapp_cmd; continue; } if testterm_cmd; then exec_terminal & else exec_gui & fi clear && exit elif [[ $list == '//+' ]]; then while true; do input="$(input_verbose)"; [[ -z $input ]] && { clear && exit; } case "$input" in "../") cd ../ || { error_cmd && continue; } ;; "//q") continue 2 ;; "//"*) eval "${input#\/\/}" || { error_cmd && continue; } continue ;; *) list="$(sed_cmd <<< "$input")"; [[ -z $list ]] && { error_cmd && continue; } case "$input" in *"*") list="${list%?}"; [[ -z $list ]] && { error_cmd && continue; } ;; esac if [[ -d "$list" ]]; then if [[ -x "$list" ]]; then cd -- "$list" || { error_cmd && continue; } else perm_cmd && continue fi elif [[ -f "$list" ]]; then if [[ -r "$list" ]]; then type="$(type_cmd)"; [[ -z $type ]] && { error_cmd && continue; } tool="$(tool_cmd)"; [[ ! -r $tool ]] && { noapp_cmd; continue; } if testterm_cmd; then exec_terminal & else exec_gui & fi else perm_cmd && continue fi else error_cmd && continue fi ;; esac done else case "$list" in "//"*) eval "${list#\/\/}" || { error_cmd && continue; } continue ;; *) error_cmd && continue ;; esac fi done