irongit
hygoland/scripts/RofiThemeSelector.sh
75 lines2.2 KBBourne Again Shell (bash)
1#!/bin/bash
2# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
3# Script for adding a selected theme to the Rofi config
4
5IFS=$'\n\t'
6
7# Define directories and variables
8rofi_theme_dir="$HOME/.config/rofi/themes"
9rofi_config_file="$HOME/.config/rofi/config.rasi"
10SED=$(which sed)
11iDIR="$HOME/.config/swaync/images"
12rofi_theme="$HOME/.config/rofi/config-rofi-theme.rasi"
13
14# Function to display menu options
15menu() {
16 options=()
17 while IFS= read -r file; do
18 options+=("$file")
19 done < <(find -L "$rofi_theme_dir" -maxdepth 1 -type f -exec basename {} \; | sort -V)
20
21 printf '%s\n' "${options[@]}"
22}
23
24# Function to add or update theme in the config.rasi
25add_theme_to_config() {
26 local theme_name="$1"
27 local theme_path="$rofi_theme_dir/$theme_name"
28
29 # if config in $HOME to write as $HOME
30 if [[ "$theme_path" == $HOME/* ]]; then
31 theme_path_with_tilde="~${theme_path#$HOME}"
32 else
33 theme_path_with_tilde="$theme_path"
34 fi
35
36 # If no @theme is in the file, add it
37 if ! grep -q '^\s*@theme' "$rofi_config_file"; then
38 echo -e "\n\n@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
39 echo "Added @theme \"$theme_path_with_tilde\" to $rofi_config_file"
40 else
41 $SED -i "s/^\(\s*@theme.*\)/\/\/\1/" "$rofi_config_file"
42 echo -e "@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
43 echo "Updated @theme line to $theme_path_with_tilde"
44 fi
45
46 # Ensure no more than max # of lines with //@theme lines
47 max_line="9"
48 total_lines=$(grep -c '^\s*//@theme' "$rofi_config_file")
49
50 if [ "$total_lines" -gt "$max_line" ]; then
51 excess=$((total_lines - max_line))
52 # Remove the oldest or the very top //@theme lines
53 for i in $(seq 1 "$excess"); do
54 $SED -i '0,/^\s*\/\/@theme/ { /^\s*\/\/@theme/ {d; q; }}' "$rofi_config_file"
55 done
56 echo "Removed excess //@theme lines"
57 fi
58}
59
60# Main function
61main() {
62 choice=$(menu | rofi rofi -dmenu -i -config $rofi_theme)
63
64 if [[ -z "$choice" ]]; then
65 exit 0
66 fi
67 add_theme_to_config "$choice"
68 notify-send -i "$iDIR/ja.png" -u low 'Rofi Theme applied:' "$choice"
69}
70
71if pgrep -x "rofi" >/dev/null; then
72 pkill rofi
73fi
74
75main