ximperconf: add var commands, move module commands

parent 0a570f2a
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
set -euo pipefail set -euo pipefail
SCRIPT_NAME="$(basename "$0")" SCRIPT_NAME="$(basename "$0")"
HYPR_CONF="$HOME/.config/hypr/hyprland.conf"
print_error() { print_error() {
printf "\033[1;31m%s\033[0m\n" "$1" printf "\033[1;31m%s\033[0m\n" "$1"
...@@ -27,26 +28,39 @@ show_help() { ...@@ -27,26 +28,39 @@ show_help() {
print_yellow " hyprland Конфигурацию Hyprland" print_yellow " hyprland Конфигурацию Hyprland"
print_white "" print_white ""
print_white " hyprland Управление модулями Hyprland" print_white " hyprland Управление модулями Hyprland"
print_blue " Флаги:" print_yellow " check Проверить конфиг"
print_yellow " --user Использовать пользовательские модули" print_white " module Управление модулями"
print_yellow " enable <модуль> Включить модуль"
print_yellow " disable <модуль> Отключить модуль"
print_yellow " status <модуль> Статус модуля"
print_yellow " list Список модулей"
print_blue " Флаги:" print_blue " Флаги:"
print_yellow " --enabled Вывести включенные модули" print_yellow " --user Использовать пользовательские модули"
print_yellow " --disabled Вывести отключенные модули" print_yellow " enable <модуль> Включить модуль"
print_yellow " disable <модуль> Отключить модуль"
print_yellow " toggle <модуль> Переключить модуль"
print_yellow " status <модуль> Статус модуля"
print_yellow " list Список модулей"
print_blue " Флаги:"
print_yellow " --enabled Вывести включенные модули"
print_yellow " --disabled Вывести отключенные модули"
print_white " var Управление переменными"
print_yellow " list Показать список переменных"
print_yellow " info Показать значения переменных"
print_yellow " get <имя> Получить значение переменной"
print_yellow " set <имя> <значение> Установить новое значение переменной"
print_yellow " unset <имя> Установить новое значение переменной"
print_white "" print_white ""
print_blue "Примеры:" print_blue "Примеры:"
print_white " $SCRIPT_NAME create base" print_white " $SCRIPT_NAME create base"
print_white " $SCRIPT_NAME create hyprland" print_white " $SCRIPT_NAME create hyprland"
print_white " $SCRIPT_NAME hyprland enable theme" print_white ""
print_white " $SCRIPT_NAME hyprland enable --user theme" print_white " $SCRIPT_NAME hyprland check"
print_white " $SCRIPT_NAME hyprland disable theme" print_white ""
print_white " $SCRIPT_NAME hyprland status theme" print_white " $SCRIPT_NAME hyprland module enable theme"
print_white " $SCRIPT_NAME hyprland list" print_white " $SCRIPT_NAME hyprland module enable --user theme"
print_white " $SCRIPT_NAME hyprland list --user" print_white " $SCRIPT_NAME hyprland module list"
print_white " $SCRIPT_NAME hyprland list --user --enabled" print_white " $SCRIPT_NAME hyprland module list --user --enabled"
print_white ""
print_white " $SCRIPT_NAME hyprland var set terminal kitty"
print_white " $SCRIPT_NAME hyprland var get terminal"
print_white " $SCRIPT_NAME hyprland var list"
exit 0 exit 0
} }
...@@ -88,6 +102,17 @@ create_link_if_missing() { ...@@ -88,6 +102,17 @@ create_link_if_missing() {
fi fi
} }
hyprland_check() {
command -v hyprland >/dev/null 2>&1 || print_error "Hyprland не найден в PATH"
[ ! -f "$HYPR_CONF" ] && print_error "Конфигурация не найдена: $HYPR_CONF"
hyprland --verify-config 2>&1 | awk '
/======== Config parsing result:/ {flag=1; next}
flag && NF {print}
'
}
hyprland_get_module_file() { hyprland_get_module_file() {
local module="$1" local module="$1"
local user="${2:-false}" local user="${2:-false}"
...@@ -101,7 +126,6 @@ hyprland_get_module_file() { ...@@ -101,7 +126,6 @@ hyprland_get_module_file() {
hyprland_module_status() { hyprland_module_status() {
local module="$1" local module="$1"
local user="${2:-false}" local user="${2:-false}"
local conf="$HOME/.config/hypr/hyprland.conf"
local sys_file local sys_file
sys_file=$(hyprland_get_module_file "$module" "$user") sys_file=$(hyprland_get_module_file "$module" "$user")
local line_full="source = $sys_file" local line_full="source = $sys_file"
...@@ -111,55 +135,54 @@ hyprland_module_status() { ...@@ -111,55 +135,54 @@ hyprland_module_status() {
print_red "Модуль '$module' не найден: $sys_file" print_red "Модуль '$module' не найден: $sys_file"
return return
} }
[ ! -f "$conf" ] && { [ ! -f "$HYPR_CONF" ] && {
echo "disabled" echo "disabled"
return return
} }
if grep -qE "^[[:space:]]*${line_full}" "$conf" || grep -qE "^[[:space:]]*${line_tilde}" "$conf"; then if grep -qE "^[[:space:]]*${line_full}" "$HYPR_CONF" || grep -qE "^[[:space:]]*${line_tilde}" "$HYPR_CONF"; then
echo "enabled" echo "enabled"
else else
echo "disabled" echo "disabled"
fi fi
} }
hyprland_toggle_module() { hyprland_set_module() {
local action="$1" local action="$1"
local module="$2" local module="$2"
local user="${3:-false}" local user="${3:-false}"
local conf="$HOME/.config/hypr/hyprland.conf"
local sys_file local sys_file
sys_file=$(hyprland_get_module_file "$module" "$user") sys_file=$(hyprland_get_module_file "$module" "$user")
local line_full="source = $sys_file" local line_full="source = $sys_file"
local line_tilde="source = ~${sys_file#$HOME}" local line_tilde="source = ~${sys_file#$HOME}"
[ ! -f "$sys_file" ] && print_error "Модуль '$module' не найден: $sys_file" [ ! -f "$sys_file" ] && print_error "Модуль '$module' не найден: $sys_file"
mkdir -p "$(dirname "$conf")" mkdir -p "$(dirname "$HYPR_CONF")"
touch "$conf" touch "$HYPR_CONF"
if [ "$action" = "enable" ]; then if [ "$action" = "enable" ]; then
if grep -qE "^[[:space:]]*${line_full}" "$conf" || grep -qE "^[[:space:]]*${line_tilde}" "$conf"; then if grep -qE "^[[:space:]]*${line_full}" "$HYPR_CONF" || grep -qE "^[[:space:]]*${line_tilde}" "$HYPR_CONF"; then
print_yellow "Модуль '$module' уже включён" print_yellow "Модуль '$module' уже включён"
return return
fi fi
if grep -qE "^[[:space:]]*#\s*${line_full}" "$conf"; then if grep -qE "^[[:space:]]*#\s*${line_full}" "$HYPR_CONF"; then
sed -i "s|^[[:space:]]*#\s*${line_full}|${line_full}|" "$conf" sed -i "s|^[[:space:]]*#\s*${line_full}|${line_full}|" "$HYPR_CONF"
elif grep -qE "^[[:space:]]*#\s*${line_tilde}" "$conf"; then elif grep -qE "^[[:space:]]*#\s*${line_tilde}" "$HYPR_CONF"; then
sed -i "s|^[[:space:]]*#\s*${line_tilde}|${line_full}|" "$conf" sed -i "s|^[[:space:]]*#\s*${line_tilde}|${line_full}|" "$HYPR_CONF"
else else
echo "$line_tilde" >>"$conf" echo "$line_tilde" >>"$HYPR_CONF"
fi fi
print_green "Модуль '$module' включён" print_green "Модуль '$module' включён"
elif [ "$action" = "disable" ]; then elif [ "$action" = "disable" ]; then
if grep -qE "^[[:space:]]*#\s*${line_full}" "$conf" || grep -qE "^[[:space:]]*#\s*${line_tilde}" "$conf"; then if grep -qE "^[[:space:]]*#\s*${line_full}" "$HYPR_CONF" || grep -qE "^[[:space:]]*#\s*${line_tilde}" "$HYPR_CONF"; then
print_yellow "Модуль '$module' уже отключён" print_yellow "Модуль '$module' уже отключён"
return return
fi fi
if grep -qE "^[[:space:]]*${line_full}" "$conf"; then if grep -qE "^[[:space:]]*${line_full}" "$HYPR_CONF"; then
sed -i "s|^[[:space:]]*${line_full}|# ${line_full}|" "$conf" sed -i "s|^[[:space:]]*${line_full}|# ${line_full}|" "$HYPR_CONF"
elif grep -qE "^[[:space:]]*${line_tilde}" "$conf"; then elif grep -qE "^[[:space:]]*${line_tilde}" "$conf"; then
sed -i "s|^[[:space:]]*${line_tilde}|# ${line_full}|" "$conf" sed -i "s|^[[:space:]]*${line_tilde}|# ${line_full}|" "$HYPR_CONF"
else else
print_yellow "Модуль '$module' не найден в конфигурации" print_yellow "Модуль '$module' не найден в конфигурации"
fi fi
...@@ -167,6 +190,25 @@ hyprland_toggle_module() { ...@@ -167,6 +190,25 @@ hyprland_toggle_module() {
fi fi
} }
hyprland_toggle_module() {
local module="$1"
local user="${2:-false}"
[ -z "$module" ] && print_error "Укажите модуль для переключения"
local status
status=$(hyprland_module_status "$module" "$user")
case "$status" in
enabled)
hyprland_set_module disable "$module" "$user"
;;
disabled)
hyprland_set_module enable "$module" "$user"
;;
esac
}
hyprland_list_modules() { hyprland_list_modules() {
local user="${1:-false}" local user="${1:-false}"
local filter="${2:-all}" # all | enabled | disabled local filter="${2:-all}" # all | enabled | disabled
...@@ -197,48 +239,141 @@ hyprland_list_modules() { ...@@ -197,48 +239,141 @@ hyprland_list_modules() {
done done
} }
hyprland_var_list() {
[ ! -f "$HYPR_CONF" ] && print_error "Конфигурация не найдена: $HYPR_CONF"
grep -E '^[[:space:]]*\$[A-Za-z0-9_]+[[:space:]]*=' "$HYPR_CONF" |
sed -E 's/^[[:space:]]*\$([A-Za-z0-9_]+)[[:space:]]*=.*/\1/' || true
}
hyprland_var_info() {
[ ! -f "$HYPR_CONF" ] && print_error "Конфигурация не найдена: $HYPR_CONF"
grep -E '^[[:space:]]*\$[A-Za-z0-9_]+[[:space:]]*=' "$HYPR_CONF" |
sed -E 's/^[[:space:]]*\$([A-Za-z0-9_]+)[[:space:]]*=[[:space:]]*(.*)/\1=\2/' || true
}
hyprland_var_get() {
local var="$1"
[ -z "$var" ] && print_error "Укажите имя переменной"
[[ ! "$var" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] && print_error "Недопустимое имя переменной: $var"
[ ! -f "$HYPR_CONF" ] && print_error "Конфигурация не найдена: $HYPR_CONF"
local line
line=$(grep -m1 -E "^[[:space:]]*\\\$$var[[:space:]]*=" "$HYPR_CONF" || true)
if [ -z "$line" ]; then
print_error "Переменная $var не найдена"
fi
local value
value=$(printf '%s\n' "$line" | sed -E \
-e 's/^[[:space:]]*\$[A-Za-z0-9_]+[[:space:]]*=[[:space:]]*//' \
-e 's/[[:space:]]*(#.*)?$//')
print_green "$value"
}
hyprland_var_set() {
local var="$1"
local new_value="$2"
[ -z "$var" ] && print_error "Укажите имя переменной"
[ -z "$new_value" ] && print_error "Укажите новое значение"
[[ ! "$var" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] && print_error "Недопустимое имя переменной: $var"
[ ! -f "$HYPR_CONF" ] && touch "$HYPR_CONF"
if grep -qE "^[[:space:]]*\\\$$var[[:space:]]*=" "$HYPR_CONF"; then
sed -i -E "s|^[[:space:]]*\\\$$var[[:space:]]*=.*|\$$var = $new_value|" "$HYPR_CONF"
print_green "Переменная $var обновлена: $new_value"
return
fi
# блок VARS
local vars_line
vars_line=$(grep -n -E '^#[-]+[[:space:]]*ПЕРЕМЕННЫЕ.*VARS' "$HYPR_CONF" | cut -d: -f1 | head -n1 || true)
if [ -n "$vars_line" ]; then
local insert_line=$((vars_line + 1))
sed -i "${insert_line}i\$${var} = ${new_value}" "$HYPR_CONF"
print_green "Переменная $var добавлена: $new_value"
else
{
printf '\n#---------- ПЕРЕМЕННЫЕ ---- VARS\n'
printf '$%s = %s\n' "$var" "$new_value"
} >>"$HYPR_CONF"
print_green "Блок VARS создан, переменная $var добавлена: $new_value"
fi
}
hyprland_var_unset() {
local var="$1"
[ -z "$var" ] && print_error "Укажите имя переменной"
[[ ! "$var" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] && print_error "Недопустимое имя переменной: $var"
[ ! -f "$HYPR_CONF" ] && print_error "Конфигурация не найдена: $HYPR_CONF"
if grep -qE "^[[:space:]]*\\\$$var[[:space:]]*=" "$HYPR_CONF"; then
sed -i -E "/^[[:space:]]*\\\$$var[[:space:]]*=/d" "$HYPR_CONF"
print_green "Переменная $var удалена"
else
print_error "Переменная $var не найдена"
fi
}
cmd="${1:-}" cmd="${1:-}"
subcmd="${2:-}" subcmd="${2:-}"
shift 2 || true shift 2 || true
case "$cmd" in case "$cmd" in
create) create)
case "$subcmd" in case "$subcmd" in
base) create_base ;; base) create_base ;;
hyprland) create_hyprland ;; hyprland) create_hyprland ;;
"") print_error "Ошибка: не указано, что создавать" ;; "") print_error "Ошибка: не указано, что создавать" ;;
*) print_error "Неизвестная конфигурация: $subcmd" ;; *) print_error "Неизвестная конфигурация: $subcmd" ;;
esac esac
;; ;;
hyprland) hyprland)
user_flag=false case "$subcmd" in
if [ "${1:-}" = "--user" ]; then check) hyprland_check ;;
user_flag=true module)
shift user_flag=false
fi if [ "${1:-}" = "--user" ]; then
case "$subcmd" in user_flag=true
enable) hyprland_toggle_module enable "$1" "$user_flag" ;; shift
disable) hyprland_toggle_module disable "$1" "$user_flag" ;; fi
status) hyprland_module_status "$1" "$user_flag" ;; case "$1" in
list) enable) hyprland_set_module enable "$2" "$user_flag" ;;
list_filter="all" disable) hyprland_set_module disable "$2" "$user_flag" ;;
for arg in "$@"; do toggle) hyprland_toggle_module "$2" "$user_flag" ;;
case "$arg" in status) hyprland_module_status "$2" "$user_flag" ;;
--user) user_flag=true ;; list)
--enabled) list_filter="enabled" ;; list_filter="all"
--disabled) list_filter="disabled" ;; for arg in "$@"; do
esac case "$arg" in
done --user) user_flag=true ;;
hyprland_list_modules "$user_flag" "$list_filter" --enabled) list_filter="enabled" ;;
--disabled) list_filter="disabled" ;;
esac
done
hyprland_list_modules "$user_flag" "$list_filter"
;;
esac
;;
var)
case "$1" in
list) hyprland_var_list ;;
info) hyprland_var_info ;;
get) hyprland_var_get "$2" ;;
set) hyprland_var_set "$2" "$3" ;;
unset) hyprland_var_unset "$2" ;;
esac
;;
esac
;;
help | -h | --help)
show_help
;;
*)
echo "Неизвестная команда: $cmd"
show_help
exit 1
;; ;;
esac
;;
help | -h | --help)
show_help
;;
*)
echo "Неизвестная команда: $cmd"
show_help
exit 1
;;
esac esac
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment