Skip to content

Commit

Permalink
Add asciifetch, wait_for_pid, install figlet and gum
Browse files Browse the repository at this point in the history
  • Loading branch information
ron.record committed Jun 10, 2024
1 parent a961b76 commit f799f56
Show file tree
Hide file tree
Showing 12 changed files with 1,304 additions and 73 deletions.
312 changes: 312 additions & 0 deletions bin/asciifetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
#!/bin/bash
#
# asciifetch - display, animate, zoom RoonCommandLine splash screen using Kitty
#
# Written by Ronald Joe Record <[email protected]>
#
# Based on https://github.com/info-mono/diyfetch/blob/main/examples/brandfetch
# from the diyfetch project by Nguyen Nhadt Anh <[email protected]>

have_gum=$(type -p gum)
[ "${have_gum}" ] || exit 1
have_wmctrl=`type -p wmctrl`
have_xprop=`type -p xprop`

pathadd() {
if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
PATH="$1${PATH:+":$PATH"}"
fi
}

pathadd "/usr/local/bin"
pathadd "/usr/local/Roon/bin"
pathadd "${HOME}/.local/bin"
export PATH

platform=`uname -s`
if [ "${platform}" == "Darwin" ]
then
KITTY_QUIT_OPT="--override macos_quit_when_last_window_closed=yes"
else
KITTY_QUIT_OPT=
fi

usage() {
printf "\nUsage: asciifetch [-a] [-f] [-k] [-q] [-z] [-u]"
printf "\nWhere:"
printf "\n\t-a indicates animate display"
printf "\n\t-f indicates fullscreen mode"
printf "\n\t-k indicates do not exit"
printf "\n\t-q indicates quick display, do not sleep"
printf "\n\t-z indicates zoom display"
printf "\n\t-u displays this usage message and exits\n"
exit 1
}

add_fullscreen() {
[ "${have_wmctrl}" ] && {
[ "${have_xprop}" ] && {
# The things a man must do
win_id=`wmctrl -lp | grep $(xprop -root | grep _NET_ACTIVE_WINDOW | head -1 | awk '{print $5}' | sed 's/,//' | sed 's/^0x/0x0/') | awk ' { print $1 } '`
[ "${win_id}" ] && xprop -id ${win_id} | grep _NET_WM_STATE | grep FULLSCREEN > /dev/null && isfull=1
}
[ "${isfull}" ] || {
wmctrl -r ':ACTIVE:' -b add,fullscreen
sleep 1
set_term_sizes
}
}
}

remove_fullscreen() {
[ "${have_wmctrl}" ] && {
[ "${isfull}" ] || {
wmctrl -r ':ACTIVE:' -b remove,fullscreen
clear
sleep 1
set_term_sizes
display_ascii
}
}
}

cleanup() {
[ "${fullscreen}" == "fullscreen" ] && [ "${animate}" ] && remove_fullscreen
tput cnorm
tput cup ${terminal_height} 0
parent_name=$(ps -p ${PPID} -o comm=)
if [ "${parent_name}" == "asciifetch" ]
then
exit 2
else
exit 0
fi
}

animate() {
tput civis -- invisible 2> /dev/null
interval="0.1"
lastinterval="0.2"
liftlines=$((terminal_height / 4))
while true
do
line=1
tput cup ${terminal_height} 0
while [ ${line} -lt ${liftlines} ]
do
sleep ${interval}
printf "\n"
line=$((line+1))
done

line=1
tput cup 0 0
while [ ${line} -lt ${liftlines} ]
do
sleep ${interval}
tput ri
line=$((line+1))
done

line=1
tput cup ${terminal_height} 0
while [ ${line} -lt ${liftlines} ]
do
sleep ${interval}
printf "\n"
line=$((line+1))
done

line=1
tput cup 0 0
while [ ${line} -lt ${liftlines} ]
do
sleep ${lastinterval}
tput ri
line=$((line+1))
done

[ "${quick}" ] || sleep 2
[ "${keepalive}" ] || break
done
[ "${fullscreen}" == "fullscreen" ] && remove_fullscreen
tput cup ${terminal_height} 0
tput cnorm
}

zoom() {
sock_name="/tmp/fetch_kitty$$"
init_size=32
interval="0.1"
kitty --title="Asciiville Zoom" \
--start-as ${fullscreen} \
--override initial_window_width=98c \
--override initial_window_height=26c \
--override allow_remote_control=yes \
--override dynamic_background_opacity=yes \
--override background=black \
--override cursor=#000000 \
--override font_size=${size} \
--override draw_minimal_borders=yes \
--override tab_bar_min_tabs=2 \
--override startup_session=none \
${KITTY_QUIT_OPT} \
--listen-on unix:${sock_name} \
asciifetch -k 2> /dev/null &
kitty_pid=$!
trap 'cleanup' USR1
wait_for_pid ${kitty_pid} > /dev/null 2>&1 &
sleep 1

if [ "${fullscreen}" == "fullscreen" ]
then
scrollsize=4
else
scrollsize=2
fi
while true
do
size=${init_size}
opacity="1.0"
tput cup ${terminal_height} 0

[ -S ${sock_name} ] || break
kitty @ --to unix:${sock_name} set-background-opacity ${opacity} 2> /dev/null
kitty @ --to unix:${sock_name} scroll-window ${scrollsize} 2> /dev/null
opacity=$(echo "${opacity}*0.95" | bc)
while [ ${size} -gt 0 ]
do
kitty @ --to unix:${sock_name} set-font-size ${size} 2> /dev/null
kitty @ --to unix:${sock_name} set-background-opacity ${opacity} 2> /dev/null
size=$((size-2))
opacity=$(echo "${opacity}*0.95" | bc)
done
sleep 0.2
size=2
opacity=$(echo "${opacity}*1.05" | bc)
while [ ${size} -lt ${init_size} ]
do
kitty @ --to unix:${sock_name} set-font-size ${size} 2> /dev/null
size=$((size+2))
kitty @ --to unix:${sock_name} set-background-opacity ${opacity} 2> /dev/null
opacity=$(echo "${opacity}*1.1" | bc)
done
sleep 2
[ "${keepalive}" ] || break
done
[ -S ${sock_name} ] && kitty @ --to unix:${sock_name} close-window 2> /dev/null
}

set_term_sizes() {
terminal_size=$(stty size)
terminal_height=${terminal_size% *}
terminal_width=${terminal_size#* }
tput cup ${terminal_height} 0
}

print_info() {
no_color=$(printf '%b' "${1}" | sed -e 's/\x1B\[[0-9;]*[JKmsu]//g')

[ "$(printf '%s' "${no_color}" | wc --lines)" -gt ${terminal_height} ] && {
return 1
}
[ "$(printf '%s' "${no_color}" | wc --max-line-length)" -gt "${terminal_width}" ] && {
return 1
}

gum style --align center --width="${terminal_width}" "${1}" ''
printf '%b' "\033[A"
return 0
}

display_ascii() {
# Landscape layout
group_info_color=$(gum join --vertical --align center "${info}" '' "${color}")
print_info "$(gum join --horizontal --align center "${art}" ' ' "${group_info_color}")"

# Portrait layout
[ $? -eq 0 ] || {
print_info "$(gum join --vertical --align center "${art}" '' "${group_info_color}")"
}

# Other layout
[ $? -eq 0 ] || {
print_info "${group_info_color}"
print_info "${info}"
}
}

animate=
fullscreen=normal
keepalive=
quick=
zoom=
while getopts "afkqzu" flag; do
case $flag in
a)
animate=1
;;
f)
fullscreen=fullscreen
;;
k)
keepalive=1
;;
q)
quick=1
;;
z)
have_kitty=`type -p kitty`
[ "${have_kitty}" ] && zoom=1
;;
u)
usage
;;
esac
done
shift $(( OPTIND - 1 ))

clear
[ "${fullscreen}" == "fullscreen" ] && [ "${animate}" ] && add_fullscreen

trap 'cleanup' SIGINT SIGKILL SIGTERM

title=$(gum style --bold --foreground 12 "$(figlet -d /usr/local/Roon/etc -f Graceful Roon)")
subtitle=$(gum style --italic --foreground 12 'Automated Control of Roon')
info=$(gum join --vertical --align center "${title}" "${subtitle}")

art=$(gum style --foreground "${main_color}" ' ,~-.
( ʻ )-. ,~ʻ`-.
,~ʻ ` ʻ ) ) _( _) )
( ( .--.===.--. ( ` ʻ )
`.%%.###|888.:`. `-ʻ`~~=~ʻ
/%%/####|8888\::\
|%%/#####|88888\::|
|%%|#####|88888|::|.,-.
\%%|#####|88888|::/ )_
\%\#####|88888/:/ ( `ʻ )
\%\####|8888/:/( , -ʻ`-.
,~-. `%\###|888/:ʻ( ( ʻ) )
( ) )_ `\__|__/ʻ `~-~=--~~=ʻ
( ` ʻ) ) [VVVVV]
(_(_.~~~ʻ \|_|/
[XXX]
`"""ʻ')

color=$(gum style '        
        ')

[ "${quick}" ] || sleep 1
set_term_sizes

display_ascii

[ "${animate}" ] && animate
[ "${zoom}" ] && zoom
[ "${keepalive}" ] && {
while true
do
sleep 1
done
}
exit 0
47 changes: 31 additions & 16 deletions bin/roon
Original file line number Diff line number Diff line change
Expand Up @@ -2745,22 +2745,37 @@ shuffled=
if [ "${open_roon_gui}" ]; then
[ -x ${ROON}/bin/roongui ] && ${ROON}/bin/roongui
else
[ "${show_roon_menu}" ] && {
set_fzf_release
set_gui_release
set_tui_release
main_menu
}
fi
if [ "${open_roon_tui}" ]; then
[ -x ${ROON}/bin/roontui ] && ${ROON}/bin/roontui
else
[ "${show_roon_menu}" ] && {
set_fzf_release
set_gui_release
set_tui_release
main_menu
}
if [ "${open_roon_tui}" ]; then
[ -x ${ROON}/bin/roontui ] && ${ROON}/bin/roontui
else
[ "${show_roon_menu}" ] && {
clear
have_gum=$(type -p gum)
have_figlet=$(type -p figlet)
if [ "${have_gum}" ] && [ "${have_figlet}" ]; then
if [ -x ${ROON}/bin/asciifetch ]; then
${ROON}/bin/asciifetch -a -q
[ $? -eq 0 ] || tput cup 0 0 2>/dev/null
else
if [ "${have_rich}" ]; then
heading "Welcome to RoonCommandLine" "Roon Command"
else
printf "\nWelcome to RoonCommandLine\n"
fi
fi
else
if [ "${have_rich}" ]; then
heading "Welcome to RoonCommandLine" "Roon Command"
else
printf "\nWelcome to RoonCommandLine\n"
fi
fi
set_fzf_release
set_gui_release
set_tui_release
main_menu
}
fi
fi

exit 0
18 changes: 18 additions & 0 deletions bin/wait_for_pid
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
#
# wait_for_pid - wait for pid provided by first argument to exit
# then send signal to parent pid
#
# Useful when a process needs to wait on a child to exit but also
# needs to continue to execute (e.g. the parent is acting as the
# controller of the child, issuing commands over a socket). The
# parent cannot suspend execution to wait for the child to exit.
# Instead, the parent can spawn this process to wait on the pid.

wait_pid=$1

# Can't use 'wait', that requires this process be the parent of the pid
# Use tail instead of wait
tail --pid=${wait_pid} -f /dev/null

kill -USR1 ${PPID}
Loading

0 comments on commit f799f56

Please sign in to comment.