Skip to content

Instantly share code, notes, and snippets.

@romgrk
Last active June 4, 2022 00:53
Show Gist options
  • Save romgrk/0ba7a864a4e6c76eefd3 to your computer and use it in GitHub Desktop.
Save romgrk/0ba7a864a4e6c76eefd3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Make sure to 'chmod +x' this, and mv it in your path.
# Quick dl command:
#$ wget https://gist.githubusercontent.com/romgrk/0ba7a864a4e6c76eefd3/raw/92015b98899d77ab908d4b90e67b4fcfd39ad1d6/dropdownterminal && chmod +x ./dropdownterminal
# Recommended invocation: (interactive mode)
#$ ./dropdownterminal -i
func_dropdownterminal () {
local USAGE="
Usage:
$0 help
$0 [options] [key] key range (datatype)
$0 [options] [key] [value] set value of key
Options:
-h help
-l list keys
-i interactive mode
Shortkeys: Keys:
h terminal-height
height terminal-height
fg foreground-color
bg background-color
a transparency-level
alpha transparency-level
fx opening-animation-time
fxe enable-animation
scroll scrollbar-visible
custom custom-command
Env:
DROPDOWN_DIR (default ~/.local/share/gnome-shell/extensions)
DROPDOWN_SCHEMA (default org.zzrough.gs-extensions.drop-down-terminal)
"
if [[ $# -eq 0 ]]; then
printf "%s\n" "$USAGE"
exit 0
fi
local DIR SCHEMA;
DIR=${DROPDOWN_DIR:="$HOME/.local/share/gnome-shell/extensions"}
DIR="$DIR/drop-down-terminal@gs-extensions.zzrough.org"
SCHEMA=${DROPDOWN_SCHEMA:="org.zzrough.gs-extensions.drop-down-terminal"}
print_usage () { printf "%s\n" "$USAGE"; }
list_keys () { gsettings --schemadir $DIR list-keys $SCHEMA $1 $2; }
set_key () {
local v=`gsettings --schemadir $DIR set $SCHEMA $1 $2`
if [[ $? -eq 0 ]]; then
exit 0
else
exit 1
fi; }
get_key () {
local v=`gsettings --schemadir $DIR get $SCHEMA $1`
if [[ $? -eq 0 ]]; then
printf "%s" $v
exit 0
else
printf ""
exit 1
fi; }
get_type () {
local v=`gsettings --schemadir $DIR range $SCHEMA $1`
if [[ $? -eq 0 ]]; then
printf "%s" $v
exit 0
else
printf ""
exit 1
fi; }
expand_key () {
local k=$(printf "%s\n" "$USAGE" | sed -r -n -e 's/^\s+'$1'\s+//p')
[[ -z $k ]] && k="$1"; printf "$k"; }
esc() { [[ -n $2 ]] && printf "\e[${1}m$2\e[0m" || printf "\e[${1}m"; }
xfg() { esc "38;5;$1"; }
xbg() { esc "48;5;$1"; }
rst() { esc '0' "$@"; }
bold() { esc '1:97' "$@"; }
comment() { esc '38;5;242' "$@"; }
bval() { esc '0;47;34' "$@"; }
inputfd() { esc '38;5;160;48;5;250' "$@"; }
inputhd() { esc '38;5;124;48;5;236' "$@"; }
hl() { esc '1;91' "$1"; printf ' '; printf "%s " "${@:2}"; }
rep() { [[ $# -eq 1 || ${#2} -lt 1 ]] && printf "%${1}s" || (printf "%${1}s" |tr " " "$2"); }
# key being modified
local key
# value of the key
local val
# datatype of the value
local valType
# unit of the value
local unit
# onscreen textBuffer
local textBuffer
local -A PROMPT
PROMPT[title]='enter key'
PROMPT[lead]='=> '
PROMPT[size]=25
PROMPT[msglead]=' ['
PROMPT[msg]='esc exits, tab list keys'
PROMPT[msgend]=']'
prompt_reset () {
PROMPT[title]='enter key'
PROMPT[lead]='=> '
PROMPT[size]=25
PROMPT[msglead]=' ['
PROMPT[msg]='esc exits, tab list keys'
PROMPT[msgend]=']'
key=''
val=''
unit=''
textBuffer=''
}
prompt_display () {
local len fill txt1 txt2;
tput cr
tput el
printf "$(rst)"
printf "$(esc '0;38;5;254' [)"
printf "$(esc '1;31')%s$(esc '0;38;5;254' ]) " ${PROMPT[title]}
if [[ -n $textBuffer ]]; then
len=PROMPT[size]
if [[ -z $unit ]]; then
fill=$(( len - ${#textBuffer} ))
printf "$(inputfd)%s" $textBuffer
printf $(rep $fill)
else
fill=$(( len - ${#textBuffer} - ${#unit} - 1 ))
printf "$(inputfd)%s" $textBuffer
printf $(rep $fill)
printf "%s " $unit
fi
else
len=$(( PROMPT[size] ))
printf "$(inputfd)"
printf "%${len}s"
fi
printf "$(xbg '238')"
printf "$(xfg '247')"
printf "${PROMPT[msglead]}"
printf "%s" ${PROMPT[msg]}
printf "$(xbg '238')"
printf "$(xfg '247')"
printf "%s " ${PROMPT[msgend]}
printf "$(rst)"
tput cr
tput cuf $(( ${#PROMPT[title]} + 3 + ${#textBuffer} ))
}
prompt_confirm () {
# Key
if [[ -z $key ]]; then
key=`expand_key $textBuffer`
val=`get_key $key`
if [[ -n $val ]]; then
valType=`get_type $key`
parse_value $val
textBuffer=$val
PROMPT[title]=$key
PROMPT[msg]='<esc> go back'
else
PROMPT[title]='enter key'
key=''
val=''
unit=''
textBuffer=''
PROMPT[msg]='No such key'
fi
# Value
else
local newval="$textBuffer"
[[ $textBuffer =~ ^[0-9]+$ ]] && newval="${textBuffer}$unit"
local res=`set_key $key $newval`
if [[ $? -ne 0 ]]; then
PROMPT[msg]=$(esc '91')'error'$(xfg '247')
else
PROMPT[msg]=$(esc '92')'ok'$(xfg '247')
parse_value $newval
textBuffer=$val
fi
fi
}
prompt_loop () {
SELECT=""
IFS=''
if [[ -n $key ]]; then
textBuffer=$key
key=''
prompt_confirm
fi
prompt_display
while [[ "$SELECT" != $'BBB' ]]; do
read -s -r -N 1 SELECT
if [[ "$SELECT" == $'\x0a' ]]; then
prompt_confirm
elif [[ "$SELECT" == $'\t' ]]; then
echo ""
local shorts=`echo $USAGE | sed -r -n -e '/^Short/,/^\s*$/ p'`
while read -r k; do
if [[ $shorts =~ $k ]]; then
sed -r -n -e "/$k/ p" <<< $shorts
else
printf " (none) %s\n" $k
fi
done <<< "`list_keys`"
elif [[ "$SELECT" == $'\e' ]]; then
if [[ -n $key ]]; then
prompt_reset
else
exit 0
fi
elif [[ "$SELECT" == $'\x7f' ]]; then
local len=${#textBuffer}
[[ $len -ne 0 ]] && textBuffer="${textBuffer:0:len-1}"
else
if [[ -n $key && "$SELECT" == $'j' ]]; then
[[ $textBuffer =~ ^[0-9]+$ ]] && let "val -= 1"
textBuffer="$val"
prompt_confirm
elif [[ -n $key && "$SELECT" == $'k' ]]; then
[[ $textBuffer =~ ^[0-9]+$ ]] && let "val += 1"
textBuffer="$val"
prompt_confirm
elif [[ "$SELECT" =~ [a-zA-Z0-9%\'] ]]; then
textBuffer+="$SELECT"
[[ -n $key ]] && PROMPT[msg]='<esc> go back'
fi
fi
[[ -n $key && -n $unit ]] && PROMPT[msg]+='; j/k -/+'
prompt_display
done
}
parse_value () {
local newval=$1
case $newval in # $( arch ) returns machine architecture.
*[0-9]%*)
newval=$(echo "$newval" | sed -e 's/[^0-9]//g')
unit="%"
val=$newval
;;
*[0-9]px* )
newval=$(echo "$newval" | sed -e 's/[^0-9]//g')
unit='px'
val=$newval
;;
*\ [0-9]* )
newval=$(echo "$newval" | sed -r -e 's/^.+ //g')
unit=''
val=$newval
;;
+[0-9] )
unit=''
val=$newval
;;
* )
newval=${newval##\'}
newval=${newval/%\'}
unit=''
val=$newval
esac
}
local -A ARGV;
ARGV[key]=0
ARGV[value]=0
ARGV[interactive]=0
while getopts "ihl" opt; do
case $opt in
i)
ARGV[interactive]=1
;;
l)
echo ""
local shorts=`printf $USAGE | \sed -r -n -e '/^Short/,/^\s*$/ p'`
while read -r k; do
if [[ $shorts =~ $k ]]; then
sed -r -n -e "/$k/ p" <<< $shorts
else
printf " (none) %s\n" $k
fi
done <<< "`list_keys`"
exit 0;
;;
h)
printf "$(print_usage)\n"; exit 0;
;;
\?)
printf "Invalid option: $OPTARG; $OPTERR"
printf "$(print_usage)\n"; exit 1;
;;
esac
done
shift $(($OPTIND-1))
if [[ $# -gt 0 ]]; then
key=$1
key=`expand_key $key`
fi
if [[ $# -gt 1 ]]; then
val=$2
fi
if [[ ${ARGV[interactive]} -eq 1 ]]; then
prompt_loop
exit 0
fi
if [[ $# -eq 1 ]]; then
val="`get_key $key`"
[[ -z $val ]] && exit 1
valType="`get_type $key`"
echo -en " $(comment '=>') $(esc '38;5;202')$val"
echo -e " $(comment)$valType"
else
set_key $key "$val"
exit $?
fi
}
func_dropdownterminal $@
unset -f func_dropdownterminal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment