From f2b18aab5a84d04a36f4194473680ad70bdb5578 Mon Sep 17 00:00:00 2001 From: Peter Son Struschka Date: Sun, 2 Feb 2020 12:50:32 +0800 Subject: script to find and edit config files --- basics/.local/bin/battery-monitor | 41 +++++++++++++++++++++++++++++++++++++++ basics/.local/bin/edit-config | 6 ++++++ basics/.local/bin/locate-config | 24 +++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100755 basics/.local/bin/battery-monitor create mode 100755 basics/.local/bin/edit-config create mode 100755 basics/.local/bin/locate-config (limited to 'basics/.local/bin') diff --git a/basics/.local/bin/battery-monitor b/basics/.local/bin/battery-monitor new file mode 100755 index 0000000..ff1ef8b --- /dev/null +++ b/basics/.local/bin/battery-monitor @@ -0,0 +1,41 @@ +battery_level=`acpi -b | cut -d ' ' -f 4 | grep -o '[0-9]*'` +battery_state=$(acpi | grep 'Battery' | sed 's/Battery\s[0-9]*: //' | sed 's/, [0-9][0-9]*\%.*//') +battery_remaining=$(acpi | grep -oh '[0-9:]* remaining' | sed 's/:\w\w remaining$/ Minutes/' | sed 's/00://' | sed 's/:/h /') + +if [ ! -f "/tmp/.battery" ]; then + echo "$battery_level" > /tmp/.battery + echo "$battery_state" >> /tmp/.battery + exit +fi + +previous_battery_level=$(cat /tmp/.battery | head -n 1) +previous_battery_state=$(cat /tmp/.battery | tail -n 1) +echo "$battery_level" > /tmp/.battery +echo "$battery_state" >> /tmp/.battery + +checkBatteryLevel() { + if [ $battery_state != "Discharging" ] || [ "${battery_level}" == "${previous_battery_level}" ]; then + exit + fi + + if [ $battery_level -le 3 ]; then + sudo systemctl suspend + elif [ $battery_level -le 5 ]; then + notify-send "Low Battery" "Your computer will suspend soon unless plugged into a power outlet." -u critical + elif [ $battery_level -le 10 ]; then + notify-send "Low Battery" "${battery_level}% (${battery_remaining}) of battery remaining." -u normal + fi +} + +checkBatteryStateChange() { + if [ "$battery_state" != "Discharging" ] && [ "$previous_battery_state" == "Discharging" ]; then + notify-send "Charging" "Battery is now plugged in." -u low + fi + + if [ "$battery_state" == "Discharging" ] && [ "$previous_battery_state" != "Discharging" ]; then + notify-send "Power Unplugged" "Your computer has been disconnected from power." -u low + fi +} + +checkBatteryStateChange +checkBatteryLevel \ No newline at end of file diff --git a/basics/.local/bin/edit-config b/basics/.local/bin/edit-config new file mode 100755 index 0000000..7d1baae --- /dev/null +++ b/basics/.local/bin/edit-config @@ -0,0 +1,6 @@ +#/bin/sh + +FILE=$(locate-config) +[ -z "$FILE" ] && exit 1 + +$EDITOR $FILE diff --git a/basics/.local/bin/locate-config b/basics/.local/bin/locate-config new file mode 100755 index 0000000..d798a39 --- /dev/null +++ b/basics/.local/bin/locate-config @@ -0,0 +1,24 @@ +#! /usr/bin/env bash + +set -e + +CONFIG_FILES=`ls -1 ~/.local/share/config-files | cut -d "" -f 1 | sort -d` +CONFIG_ARRAY=(`find ~/.local/share/config-files/ -maxdepth 1 -mindepth 1 -print0 | sort -z | xargs -r0 echo`) + +INDEX=`echo "$CONFIG_FILES" | rofi -dmenu -format "i" -no-custom -mesg "Find config file for..."` +[ -z "$INDEX" ] && exit 1 +SOURCE=${CONFIG_ARRAY[$INDEX]} +#echo "$CONFIG_FILES" +#for item in ${CONFIG_ARRAY[*]} +#do +# printf " %s\n" $item +#done +# (https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself) +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" + +echo $SOURCE -- cgit v1.2.3