From 29cafac68249e2f5e677eb3803d58f0199ef9a54 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Mon, 14 Oct 2024 20:19:00 +1100 Subject: [PATCH] feat: Add some more scripts --- bt.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ message.sh | 24 ++++++++++++++++ music.sh | 24 ++++++++++++++-- ts.sh | 47 +++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 3 deletions(-) create mode 100755 bt.sh create mode 100755 message.sh diff --git a/bt.sh b/bt.sh new file mode 100755 index 0000000..b71a16f --- /dev/null +++ b/bt.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +# Check if first arg is colour +if [ "$1" = "colour" ]; then + RED='%{F#ff0000}' + GREEN='%{F#ff60d090}' + YELLOW='%{F#ffff00}' + BLUE='%{F#33fdfefe}' + NC='%{F-}' +else + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[0;33m' + BLUE='\033[0;34m' + NC='\033[0m' # No Color +fi + +# Check if Bluetooth is powered on +bluetooth_status=$(bluetoothctl show | grep "Powered:" | awk '{print $2}') + +if [ "$bluetooth_status" == "yes" ]; then + # Check if toggle is passed + if [ "$#" -eq 1 ] && [ "$1" = "toggle" ]; then + echo "Disabling Bluetooth" + bluetoothctl power off + exit 0 + fi + + # Check if any devices are connected + connected_devices=$(bluetoothctl devices Connected | grep Device | cut -d ' ' -f 2) + + if [ -z "$connected_devices" ]; then + # No devices connected, show disconnected icon + echo -en "${GREEN}󰂯${NC}" + else + # Devices are connected, show connected icon + echo -en "${GREEN}󰂱${NC}" + + # Loop through the connected devices + for device in $connected_devices; do + # Get the device info + device_info=$(bluetoothctl info "$device") + device_battery_percent=$(echo "$device_info" | grep "Battery Percentage" | awk -F'[()]' '{print $2}') + device_battery_icon="" + device_output="" + device_name=$(echo "$device_info" | grep "Alias" | cut -d ' ' -f 2-) + + if [ -z "$device_name" ]; then + device_name=$(echo "$device_info" | grep "Name" | cut -d ' ' -f 2-) + if [ -z "$device_name" ]; then + device_name="Unknown Device" + fi + fi + + if [ -n "$device_battery_percent" ]; then + if [ "$device_battery_percent" -gt 90 ]; then + device_battery_icon="${GREEN}" + elif [ "$device_battery_percent" -gt 60 ]; then + device_battery_icon="${GREEN}" + elif [ "$device_battery_percent" -gt 35 ]; then + device_battery_icon="${YELLOW}" + elif [ "$device_battery_percent" -gt 15 ]; then + device_battery_icon="${YELLOW}" + else + device_battery_icon="${RED}" + fi + device_output="$device_battery_icon $device_battery_percent%${NC}" + fi + + # Print the device info + echo -en " $device_name ($device_output)" + done + fi + +else + # Check if toggle is passed + if [ "$#" -eq 1 ] && [ "$1" = "toggle" ]; then + echo "Enabling Bluetooth" + bluetoothctl power on + exit 0 + fi + echo -en "${RED}󰂲${NC}" +fi diff --git a/message.sh b/message.sh new file mode 100755 index 0000000..cbce19a --- /dev/null +++ b/message.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +message() { + if tty -s; then + echo "$1" >/tmp/message + else + # Get from pipe + cat >/tmp/message + fi + output=$(polybar-msg action message hook 0) + # Check if the output contains Successfully + if [[ $output == *"Successfully"* ]]; then + echo "Message sent successfully" + else + echo "Error sending message" + fi +} + +# Call the copy function with the provided arguments or from a pipe +if [ -t 0 ]; then + message "$@" +else + message +fi diff --git a/music.sh b/music.sh index 818e6fe..f75bb1e 100755 --- a/music.sh +++ b/music.sh @@ -50,6 +50,9 @@ brave="" # Loop through each player and check its status for player in $players; do + if [ "$verbose" -eq 1 ]; then + echo "Checking player $player" + fi status=$(playerctl -p "$player" status 2>/dev/null) # Check if player name contains brave if [[ "$player" == *"brave"* ]]; then @@ -60,6 +63,9 @@ for player in $players; do fi if [[ "$status" == "Playing" || "$status" == "Paused" ]]; then + if [[ $verbose -eq 1 ]]; then + echo "Player $player is active" + fi active_players+=("$player:$status") fi done @@ -78,6 +84,10 @@ truncate_string() { # Check how many active players we found active_count=${#active_players[@]} +if [[ $verbose -eq 1 ]]; then + echo "Active players: $active_count" +fi + if [ "$active_count" -eq 1 ]; then # If exactly one player is active, print its metadata IFS=":" read -r player status <<<"${active_players[0]}" @@ -123,7 +133,9 @@ elif [ "$active_count" -gt 1 ]; then truncated_artist=$(truncate_string "$artist" $MAX_ARTIST_LENGTH) output+="🎵 $truncated_title [$truncated_artist]" fi - + if [ "$verbose" -eq 1 ]; then + echo "Player $player: $title - $artist" + fi fi done if [ "$playing_count" -eq 1 ]; then @@ -142,8 +154,9 @@ elif [ "$active_count" -gt 1 ]; then fi else brave_status=$(playerctl -p "$brave" status 2>/dev/null) - title=$(playerctl -p "$brave" metadata --format '{{title}}') - artist=$(playerctl -p "$brave" metadata --format '{{artist}}') + + title=$(playerctl -p "$brave" metadata --format '{{title}}' 2>/dev/null) + artist=$(playerctl -p "$brave" metadata --format '{{artist}}' 2>/dev/null) # Print the title and artist if [ "$verbose" -eq 1 ]; then echo "Brave is playing" @@ -153,6 +166,11 @@ elif [ "$active_count" -gt 1 ]; then # Check if artist is empty if [ -z "$artist" ]; then + if [ -z "$title" ]; then + echo "Multiple players playing" + exit 0 + fi + truncated_title=$(truncate_string "$title" $(($MAX_TITLE_LENGTH + $MAX_ARTIST_LENGTH))) if [ "$brave_status" == "Playing" ]; then echo "🎵 $truncated_title" diff --git a/ts.sh b/ts.sh index 69a1e7d..8bfefe8 100755 --- a/ts.sh +++ b/ts.sh @@ -10,6 +10,7 @@ if [ "$#" -eq 0 ]; then echo "List: Lists all nodes" echo "EN: Exit node (clear)" echo "EN {node}: Use node" + echo "Short: List info short" exit 1 fi @@ -24,13 +25,59 @@ if [ "$1" = "en" ]; then tailscale set --exit-node= exit 0 fi + if [ "$2" = "toggle" ]; then + output=$(tailscale status | grep "; exit node;") + hostname=$(echo "$output" | awk '{print $2}') + # Check if node is exit node + if [ -z "$hostname" ]; then + tailscale set --exit-node=docker01 + else + tailscale set --exit-node= + fi + exit 0 + fi + echo "Enabling node $2" tailscale set --exit-node="$2" exit 0 fi +if [ "$1" = "short" ]; then + # Check if a third arg is sent + if [ -z "$2" ]; then + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[0;33m' + BLUE='\033[0;34m' + NC='\033[0m' # No Color + else + RED='%{F#ff0000}' + GREEN='%{F#ff60d090}' + YELLOW='%{F#ffff00}' + BLUE='%{F#0000ff}' + NC='%{F-}' + fi + + # Check if connected + output=$(tailscale status) + if [[ "$output" == *"stopped"* ]]; then + echo -e "${RED}󰕥${NC}" + exit 0 + fi + + output=$(tailscale status | grep "; exit node;") + hostname=$(echo "$output" | awk '{print $2}') + # Check if node is exit node + if [ -z "$hostname" ]; then + echo -e "${GREEN}󰕥${NC}" + exit 0 + fi + echo -e "${BLUE}󰕥${NC} $hostname" + exit 0 +fi echo "Usage: ts [list|en|en {node}]" echo "List: Lists all nodes" echo "EN: Exit node (clear)" echo "EN {node}: Use node" +echo "Short: List info short" exit 1