mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-03-02 14:59:34 +00:00
feat(bin): update scripts to function format
This commit is contained in:
@@ -14,168 +14,119 @@
|
||||
# pkill (coreutils)
|
||||
#
|
||||
# 2021-2022 : João F. © BeyondMagic <koetemagie@gmail.com>
|
||||
# 2024- : Ismo Vuorinen <https://github.com/ivuorinen>
|
||||
|
||||
# 1. Variables.
|
||||
# Enable verbosity with VERBOSE=1
|
||||
VERBOSE="${VERBOSE:-0}"
|
||||
|
||||
# Variables
|
||||
frame_rate=30
|
||||
name='camera'
|
||||
path_recordings="$HOME/.cache/recording"
|
||||
replace_id="$HOME/.cache/recording.id"
|
||||
|
||||
# Function to print messages if VERBOSE is enabled
|
||||
# $1 - message (string)
|
||||
msg()
|
||||
{
|
||||
# A. Indepedent of variables.
|
||||
frame_rate=30
|
||||
time=10
|
||||
name='カメラ'
|
||||
|
||||
# B. Depedent of variables.
|
||||
path_recordings="$HOME/.cache/recording"
|
||||
icons="$HOME/.local/share/icons"
|
||||
replace_id="$HOME/.cache/recording.id"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "$1"
|
||||
}
|
||||
|
||||
# 2. Functions to minise code.
|
||||
#{
|
||||
|
||||
# I.
|
||||
# Notify function
|
||||
notify()
|
||||
{
|
||||
|
||||
notify-call \
|
||||
--replace-file "$replace_id" \
|
||||
"$@"
|
||||
|
||||
notify-call --replace-file "$replace_id" "$@"
|
||||
}
|
||||
|
||||
# II.
|
||||
# Stop recording function
|
||||
stop()
|
||||
{
|
||||
|
||||
# A.
|
||||
#pkill -INT -f 'ffmpeg -f alsa -ac 1 -i pulse -f x11grab -r 30 -s '
|
||||
giph --stop
|
||||
|
||||
# C.
|
||||
eww update record_menu=false
|
||||
|
||||
}
|
||||
#}
|
||||
|
||||
# #. Kill previous giph process.
|
||||
if [ "$(pgrep -f 'bash.+giph')" ]; then
|
||||
|
||||
# A. Let the user decide.
|
||||
next=$(notify \
|
||||
-d 'echo yes' \
|
||||
"$name" \
|
||||
'Do you want to stop current recording?')
|
||||
|
||||
# A. End with previous giph session.
|
||||
[ "$next" = 'yes' ] && stop
|
||||
|
||||
# B. Just exit cleanly.
|
||||
exit 0
|
||||
|
||||
fi
|
||||
|
||||
# 2. To see if current fyletype is supported.
|
||||
{
|
||||
|
||||
case "$1" in
|
||||
|
||||
# A. Supported.
|
||||
'mkv' | 'gif' | 'webm' | 'mp4') ;;
|
||||
|
||||
# B. Not supported.
|
||||
*)
|
||||
|
||||
# I. Let the user decide.
|
||||
format=$(notify \
|
||||
-o 'echo mkv:MKV' \
|
||||
-o 'echo webm:WEBM' \
|
||||
-o 'echo mp4:MP4' \
|
||||
-o 'echo gif:GIF' \
|
||||
"$name" \
|
||||
'What is the filetype you want to record?')
|
||||
|
||||
# II. Execute itself.
|
||||
exec $0 $format $2
|
||||
|
||||
;;
|
||||
|
||||
esac
|
||||
|
||||
}
|
||||
|
||||
# 3. Whether to choose fullscreen recording or sizeable one.
|
||||
# Function to check for required applications
|
||||
check_dependencies()
|
||||
{
|
||||
if [ "$2" = 'fullscreen' ]; then
|
||||
|
||||
# A. From <WxH+X+Y> monitor.
|
||||
geometry="$(xrandr | awk '/ primary/{print $4}')"
|
||||
|
||||
elif [ "$2" = 'set' ]; then
|
||||
|
||||
# A. To get size & position of the recording set.
|
||||
geometry="$(slop -f "%wx%h+%x+%y")"
|
||||
|
||||
else
|
||||
|
||||
# I. Let the user decide.
|
||||
next=$(notify \
|
||||
-o 'echo fullscreen:The whole_screen!' \
|
||||
-o 'echo set:Let me set.' \
|
||||
"$name" \
|
||||
'How exactly do you want to record?')
|
||||
|
||||
# II. Execute itself.
|
||||
exec $0 $1 $next
|
||||
for cmd in ffmpeg notify-send.sh pkill eww giph slop; do
|
||||
if ! command -v "$cmd" &> /dev/null; then
|
||||
echo "Required command '$cmd' not found. Please install it before running this script."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function to kill previous giph process if running
|
||||
kill_previous_process()
|
||||
{
|
||||
if pgrep -f 'bash.+giph' > /dev/null; then
|
||||
next=$(notify -d 'echo yes' "$name" 'Do you want to stop current recording?')
|
||||
[ "$next" = 'yes' ] && stop
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
# 4. Start recording.
|
||||
# Function to check if the current file type is supported
|
||||
check_file_type()
|
||||
{
|
||||
case "$1" in
|
||||
'mkv' | 'gif' | 'webm' | 'mp4') ;;
|
||||
*)
|
||||
format=$(
|
||||
notify \
|
||||
-o 'echo mkv:MKV' \
|
||||
-o 'echo webm:WEBM' \
|
||||
-o 'echo mp4:MP4' \
|
||||
-o 'echo gif:GIF' \
|
||||
"$name" \
|
||||
'What is the filetype you want to record?'
|
||||
)
|
||||
exec "$0" "$format" "$2"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Function to determine recording scope
|
||||
determine_scope()
|
||||
{
|
||||
if [ "$2" = 'fullscreen' ]; then
|
||||
geometry=$(xrandr | awk '/ primary/{print $4}')
|
||||
elif [ "$2" = 'set' ]; then
|
||||
geometry=$(slop -f "%wx%h+%x+%y")
|
||||
else
|
||||
next=$(notify -o 'echo fullscreen:The whole_screen!' -o 'echo set:Let me set.' "$name" 'How exactly do you want to record?')
|
||||
exec "$0" "$1" "$next"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to start recording
|
||||
start_recording()
|
||||
{
|
||||
mkdir -p "$path_recordings"
|
||||
name_file="$path_recordings/$2-$(date +'%a_%b_%d_%H:%M:%S').$1"
|
||||
|
||||
# A. Timer.
|
||||
#for i in {1..$time}; do
|
||||
|
||||
# # I.
|
||||
# notify "Starting in $i seconds."
|
||||
|
||||
# # II. Wait for next second.
|
||||
# sleep ${i}s
|
||||
|
||||
#done
|
||||
|
||||
# B. Send a sign of recording to lemonbar so that you know it is being recorded.
|
||||
eww update record_menu=true
|
||||
|
||||
# C. Start recording.
|
||||
giph \
|
||||
-g "$geometry" \
|
||||
-f "$frame_rate" \
|
||||
"$name_file"
|
||||
giph -g "$geometry" -f "$frame_rate" "$name_file"
|
||||
|
||||
# D.
|
||||
stop
|
||||
|
||||
# E.
|
||||
responder="$(notify \
|
||||
-o 'echo open:See file?' \
|
||||
-o 'echo none:Hell no' \
|
||||
"$name" \
|
||||
'Recording has finished.')"
|
||||
responder=$(notify -o 'echo open:See file?' -o 'echo none:Hell no' "$name" 'Recording has finished.')
|
||||
|
||||
# F. For action-driven response.
|
||||
if [ "$responder" = 'open' ]; then
|
||||
|
||||
# I.
|
||||
nohup \
|
||||
gtk-launch \
|
||||
"$(xdg-mime query default inode/directory)" \
|
||||
"$path_recordings/" \
|
||||
> /dev/null 2>&1 &
|
||||
|
||||
nohup gtk-launch "$(xdg-mime query default inode/directory)" "$path_recordings/" > /dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
rm -f "$replace_id"
|
||||
}
|
||||
|
||||
rm -f "$replace_id"
|
||||
main()
|
||||
{
|
||||
check_dependencies
|
||||
kill_previous_process "$@"
|
||||
check_file_type "$@"
|
||||
determine_scope "$@"
|
||||
start_recording "$@"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
||||
Reference in New Issue
Block a user