TheRepoClub-DotFiles/localbin/.local/bin/dropdown_btop
The-Repo-Club 568f59a1d9
Dropdown
2022-11-08 15:34:00 +00:00

109 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#-*-coding:utf-8 -*-
#Auto updated?
# Yes
#File:
# dropdown_btop
#Author:
# The-Repo-Club [wayne6324@gmail.com]
#Github:
# https://github.com/The-Repo-Club/
#
#Created:
# Thu 27 October 2022, 06:11:40 PM [GMT+1]
#Modified:
# Tue 08 November 2022, 03:33:21 PM [GMT]
#
#Description:
# <Todo>
#
#Dependencies:
# alacritty xdotools
#
# shellcheck disable=all
# Controls a dropdown btop for use in i3wm
DISPLAY_WIDTH=1920
TERMINAL_WIDTH=1200
TERMINAL_HEIGHT=500
TERMINAL=alacritty # Change this to whatever btop emulator you want
# Temporary file to record the btop state
TERMINAL_STATE=/tmp/dropdown_pavucontrol_state
# Adjust the sleep time and step size as necessary to make the animation smooth
STEP_SIZE=50
SLEEP_TIME=0.02
usage() {
echo "Open/closes a dropdown btop"
echo "Usage: dropdown_btop <start|open|close|toggle>"
}
term_launch() {
$TERMINAL --class=dropdown-terminal -e btop &
local term_pid=$!
# Wait for btop to open
xdotool search --sync --pid $term_pid >/dev/null
i3-msg -q mark dropdown_btop # Mark the btop so we can identify it
i3-msg -q [con_mark=dropdown_btop] move scratchpad
echo "closed" >$TERMINAL_STATE
}
term_open() {
if [ "$(cat $TERMINAL_STATE)" == "closed" ]; then
CHECK=$(xdotool getmouselocation --shell | awk -F= 'NR==1{print $2}')
if [ $CHECK -ge "1920" ]; then
DISPLAY_WIDTH=$(DISPLAY_WIDTH * 2)
fi
echo "animating" >$TERMINAL_STATE
i3-msg -q [con_mark=dropdown_btop] scratchpad show
i3-msg -q resize set $TERMINAL_WIDTH $TERMINAL_HEIGHT
i3-msg -q move absolute position $(((DISPLAY_WIDTH - TERMINAL_WIDTH) / 2)) 30
i3-msg -q move up $((TERMINAL_HEIGHT - 1))
for i in $(seq 1 $STEP_SIZE $TERMINAL_HEIGHT); do
i3-msg -q [con_mark=dropdown_btop] move down $STEP_SIZE
sleep $SLEEP_TIME
done
echo "open" >$TERMINAL_STATE
fi
}
term_close() {
if [ "$(cat $TERMINAL_STATE)" == "open" ]; then
echo "animating" >$TERMINAL_STATE
i3-msg -q [con_mark=dropdown_btop] focus
for i in $(seq 1 $STEP_SIZE $TERMINAL_HEIGHT); do
i3-msg -q [con_mark=dropdown_btop] move up $STEP_SIZE
sleep $SLEEP_TIME
done
i3-msg -q [con_mark=dropdown_btop] scratchpad show
echo "closed" >$TERMINAL_STATE
fi
}
term_toggle() {
cmdList=$(i3-msg -t get_marks)
me=$(basename "$0")
if [[ ${cmdList[*]} =~ $me ]]; then
case "$(cat $TERMINAL_STATE)" in
open) term_close ;;
closed) term_open ;;
*) ;;
esac
else
$me "start"
$me "toggle"
fi
}
case $1 in
start) term_launch ;;
open) term_open ;;
close) term_close ;;
toggle) term_toggle ;;
*) usage ;;
esac