feat(script): Added Bluetooth script

This commit is contained in:
sankalp committed 2024-02-03 23:10:13 +05:45
1 parent d11fbee2f8
commit c67bc37345
3 files changed
+350 -45

No files matched your search

+1 -1
View File
@@ -36,7 +36,7 @@ wifi() {
}
# Toggle Bluetooth
bluetooth() {
toggle_bluetooth() {
if [ "$bluetooth_status" == "no" ]; then
bluetoothctl power on
notify-send -u low -i "$notif" 'Bluetooth ON'
+1 -8
View File
@@ -62,14 +62,7 @@ RunCMD="$SCRIPTSDIR/RunCMD.sh"
# For Note
NOTES_AUTHOR="$(whoami)"
NOTES_FOLDER="$HOME/Documents/Notes"
NOTES_EDITOR="kitty nvim"
NOTES_EDITOR="kitty --class fly_is_kitty -e nvim"
# Misc
lock="$SCRIPTSDIR/device_cmd.sh lockscreen"
# For panel
theme="$HOME/.config/rofi/config-panel.rasi"
# For Bluetooth
bluetooth_status=$(bluetoothctl show | grep "Powered" | awk '{print $2}')
+348 -36
View File
@@ -77,67 +77,88 @@ main
# Take note (ALT B)
note() {
[ ! -d "$NOTES_FOLDER" ] && mkdir -p "$NOTES_FOLDER"
}
if [[ ! -d "${NOTES_FOLDER}" ]]; then
mkdir -p "$NOTES_FOLDER"
fi
get_notes() {
ls "$NOTES_FOLDER"
ls "${NOTES_FOLDER}"
}
edit_note() {
$NOTES_EDITOR "$1"
note_location=$1
$NOTES_EDITOR "$note_location"
}
delete_note() {
note=$1
action=$(echo -e "Yes\nNo" | rofi -dmenu -p "Delete $note? ")
local note=$1
local action=$(echo -e "Yes\nNo" | rofi -dmenu -p "Are you sure you want to delete $note? ")
case $action in
"Yes")
rm "$NOTES_FOLDER/$note"
main ;;
main
;;
"No")
main ;;
main
esac
}
note_context() {
note=$1
note_location="$NOTES_FOLDER/$note"
action=$(echo -e "Edit\nDelete" | rofi -dmenu -p "$note > ")
local note=$1
local note_location="$NOTES_FOLDER/$note"
local action=$(echo -e "Edit\nDelete" | rofi -dmenu -p "$note > ")
case $action in
"Edit") edit_note "$note_location"; exit 0 ;;
"Delete") delete_note "$note"; exit 0 ;;
"Edit")
edit_note "$note_location"
exit 0;;
"Delete")
delete_note "$note"
exit 0;;
esac
exit 1
exit 1
}
new_note() {
title=$(echo -e "Cancel" | rofi -dmenu -p "Input title: ")
local title=$(echo -e "Cancel" | rofi -dmenu -p "Input title: ")
case "$title" in
"Cancel") main ;;
"Cancel")
main
;;
*)
file=$(echo "$title" | sed 's/ /_/g;s/\(.*\)/\L\1/g')
template="---\ntitle: $title\ndate: $(date --rfc-3339=seconds)\nauthor: $NOTES_AUTHOR\n---\n"
note_location="$NOTES_FOLDER/$file.md"
local file=$(echo "$title" | sed 's/ /_/g;s/\(.*\)/\L\1/g')
local template=$(cat <<- END
---
title: $title
date: $(date --rfc-3339=seconds)
author: $AUTHOR
---
[ "$title" != "" ] && echo -e "$template" > "$note_location" | edit_note "$note_location"
exit 0
# $title
END
)
note_location="$NOTES_FOLDER/$file.md"
if [ "$title" != "" ]; then
echo "$template" > "$note_location" | edit_note "$note_location"
exit 0
fi
;;
esac
}
main() {
all_notes="$(get_notes)"
first_menu="New Note"
[ "$all_notes" ] && first_menu="New Note\n$all_notes"
note=$(echo -e "$first_menu" | rofi -dmenu -i -p "Note: ")
main()
{
local all_notes="$(get_notes)"
local first_menu="New"
if [ "$all_notes" ];then
first_menu="New\n${all_notes}"
fi
local note=$(echo -e "$first_menu" | rofi -dmenu -p "󰚸 Notes")
case $note in
"New Note") new_note ;;
"") exit 1 ;;
*) note_context "$note" & ;;
"New")
new_note
;;
"")
exit 1;;
*)
note_context "$note" && exit 0 # handle esc key in note_context
esac
exit 1
exit 1
}
main
}
@@ -214,3 +235,294 @@ emoji() {
A="$(rofi -dmenu -p "Emoji:" -config ~/.config/rofi/config-long.rasi < "$HOME/.config/hypr/configs/emojis" | cut -d ' ' -f 1 | tr -d '\n')"
[[ -n "$A" ]] && wl-copy -- "$A"
}
bluetooth(){
goback=" Back"
theme=~/.config/rofi/config-long.rasi
# Checks if bluetooth controller is powered on
power_on() {
if bluetoothctl show | grep -q "Powered: yes"; then
return 0
else
return 1
fi
}
# Toggles power state
toggle_power() {
if power_on; then
bluetoothctl power off
show_menu
else
if rfkill list bluetooth | grep -q 'blocked: yes'; then
rfkill unblock bluetooth && sleep 3
fi
bluetoothctl power on
show_menu
fi
}
# Checks if controller is scanning for new devices
scan_on() {
if bluetoothctl show | grep -q "Discovering: yes"; then
echo "󰂰 Scan: ON"
return 0
else
echo "󰂰 Scan: OFF"
return 1
fi
}
# Toggles scanning state
toggle_scan() {
if scan_on; then
kill $(pgrep -f "bluetoothctl scan on")
bluetoothctl scan off
show_menu
else
bluetoothctl scan on &
echo "Scanning..."
sleep 5
show_menu
fi
}
# Checks if controller is able to pair to devices
pairable_on() {
if bluetoothctl show | grep -q "Pairable: yes"; then
echo "󰂴 Pairable: ON"
return 0
else
echo "󰂴 Pairable: OFF"
return 1
fi
}
# Toggles pairable state
toggle_pairable() {
if pairable_on; then
bluetoothctl pairable off
show_menu
else
bluetoothctl pairable on
show_menu
fi
}
# Checks if controller is discoverable by other devices
discoverable_on() {
if bluetoothctl show | grep -q "Discoverable: yes"; then
echo "󰂳 Discoverable: ON"
return 0
else
echo "󰂳 Discoverable: OFF"
return 1
fi
}
# Toggles discoverable state
toggle_discoverable() {
if discoverable_on; then
bluetoothctl discoverable off
show_menu
else
bluetoothctl discoverable on
show_menu
fi
}
# Checks if a device is connected
device_connected() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Connected: yes"; then
return 0
else
return 1
fi
}
# Toggles device connection
toggle_connection() {
if device_connected $1; then
bluetoothctl disconnect $1
device_menu "$device"
else
bluetoothctl connect $1
device_menu "$device"
fi
}
# Checks if a device is paired
device_paired() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Paired: yes"; then
echo "Paired: "
return 0
else
echo "Paired: "
return 1
fi
}
# Toggles device paired state
toggle_paired() {
if device_paired $1; then
bluetoothctl remove $1
device_menu "$device"
else
bluetoothctl pair $1
device_menu "$device"
fi
}
# Checks if a device is trusted
device_trusted() {
device_info=$(bluetoothctl info "$1")
if echo "$device_info" | grep -q "Trusted: yes"; then
echo "Trusted: "
return 0
else
echo "Trusted: "
return 1
fi
}
# Toggles device connection
toggle_trust() {
if device_trusted $1; then
bluetoothctl untrust $1
device_menu "$device"
else
bluetoothctl trust $1
device_menu "$device"
fi
}
# Prints a short string with the current bluetooth status
# Useful for status bars like polybar, etc.
print_status() {
if power_on; then
printf ''
mapfile -t paired_devices < <(bluetoothctl paired-devices | grep Device | cut -d ' ' -f 2)
counter=0
for device in "${paired_devices[@]}"; do
if device_connected $device; then
device_alias=$(bluetoothctl info $device | grep "Alias" | cut -d ' ' -f 2-)
if [ $counter -gt 0 ]; then
printf ", %s" "$device_alias"
else
printf " %s" "$device_alias"
fi
((counter++))
fi
done
printf "\n"
else
echo ""
fi
}
# A submenu for a specific device that allows connecting, pairing, and trusting
device_menu() {
device=$1
# Get device name and mac address
device_name=$(echo $device | cut -d ' ' -f 3-)
mac=$(echo $device | cut -d ' ' -f 2)
# Build options
if device_connected $mac; then
connected="Connected: "
else
connected="Connected: "
fi
paired=$(device_paired $mac)
trusted=$(device_trusted $mac)
options="$connected\n$paired\n$trusted\n$goback\n󰍃 Exit"
# Open rofi menu, read chosen option
chosen="$(echo -e "$options" | $rofi_command "$device_name")"
# Match chosen option to command
case $chosen in
$connected)
toggle_connection $mac
;;
$paired)
toggle_paired $mac
;;
$trusted)
toggle_trust $mac
;;
$goback)
show_menu
;;
esac
}
# Opens a rofi menu with current bluetooth status and options to connect
show_menu() {
# Get menu options
if power_on; then
power="󰂯 Power: ON"
# Human-readable names of devices, one per line
# If scan is off, will only list paired devices
devices=$(bluetoothctl devices | grep Device | cut -d ' ' -f 3-)
# Get controller flags
scan=$(scan_on)
pairable=$(pairable_on)
discoverable=$(discoverable_on)
# Options passed to rofi
options="$devices\n$power\n$scan\n$pairable\n$discoverable\n󰍃 Exit"
else
power="󰂲 Power: OFF"
options="$power\n󰍃 Exit"
fi
# Open rofi menu, read chosen option
chosen="$(echo -e "$options" | $rofi_command " Bluetooth")"
# Match chosen option to command
case $chosen in
$power)
toggle_power
;;
$scan)
toggle_scan
;;
$discoverable)
toggle_discoverable
;;
$pairable)
toggle_pairable
;;
*)
device=$(bluetoothctl devices | grep "$chosen")
# Open a submenu if a device is selected
if [[ $device ]]; then device_menu "$device"; fi
;;
esac
}
# Rofi command to pipe into, can add any options here
rofi_command="rofi -dmenu -no-fixed-num-lines -yoffset -100 -i -theme $theme -p"
case "$1" in
--status)
print_status
;;
*)
show_menu
;;
esac
}