Initial commit of bash scripts

This commit is contained in:
2026-06-05 23:49:54 -04:00
parent b7cf4a0224
commit 8e0e4dfc79
16 changed files with 519 additions and 70 deletions
+141 -69
View File
@@ -2,9 +2,40 @@
export HYPRLAND_INSTANCE_SIGNATURE=$(hyprctl -j instances | jq -r '.[0] | .instance')
unset workspaces_conf
unset monitors
unset active_mon
unset active_ws_name
unset total_ws
unset workspaces
unset last_id
workspaces_conf="${XDG_CONFIG_HOME}/hypr/workspaces.conf"
set -x
monitors=$(hyprctl -j monitors)
active_mon=$(hyprctl -j monitors | jq --raw-output '.[] | select(.focused) | .name')
active_ws_name="$(hyprctl -j activeworkspace | jq --raw-output '.name')"
#set +x
total_ws=$(hyprctl -j workspaces | jq --compact-output --raw-output 'sort_by(.id)')
if (( $(jq length <<< "${monitors}") > 1 )); then
workspaces="$(jq --compact-output --raw-output \
--arg mon "${active_mon}" '[.[] | select(.monitor == $mon)]' \
<<< "${total_ws}")"
else
workspaces="${total_ws}"
fi
set +x
clients=$(hyprctl -j clients)
cycle_workspace () {
set -x
local names=$(jq --raw-output --compact-output '[.[].name]' <<< ${workspaces})
local prev_ws=$(jq --raw-output --arg ws "${active_ws_name}" '.[index($ws) - 1]' <<< "${names}")
local next_ws=$(jq --raw-output --arg ws "${active_ws_name}" '.[index($ws) + 1]' <<< "${names}")
if [[ -n "${@}" ]]; then
direction="${1}"
@@ -13,89 +44,130 @@ cycle_workspace () {
exit 1
fi
monitors=$(hyprctl -j monitors)
workspaces=$(hyprctl -j workspaces)
active_mon=$(hyprctl -j monitors | jq '.[] | select(.focused) | .id')
active_ws=$(hyprctl -j monitors | jq --argjson mon ${active_mon} '.[$mon].activeWorkspace.id')
first_ws=$(jq '.[0].id' <<< "${workspaces}")
last_ws=$(jq '.[-1].id' <<< "${workspaces}")
#set -x
case "${direction}" in
"next")
if [[ ${active_ws} -eq ${last_ws} ]]; then
hyprctl dispatch workspace ${first_ws}
else
hyprctl dispatch workspace +1
fi
prev)
hyprctl dispatch workspace m-1
;;
"prev")
if [[ ${active_ws} -eq ${first_ws} ]]; then
hyprctl dispatch workspace ${last_ws}
else
hyprctl dispatch workspace -1
fi
next)
#if [[ "${next_ws}" != null ]]; then
hyprctl dispatch workspace m+1
#else
# hyprctl dispatch workspace "name:$(jq --raw-output '.[0]' <<< ${names})"
#fi
;;
*)
print "Invalid direction! Exiting..." >&2
exit 2
;;
esac
set +x
}
add_persistent_ws () {
set -x
new_id=${1}; shift
new_name=${1}; shift
if [[ -n "${1}" ]]; then
after_name=${1}; shift
fi
not_written=true
tmp=$(mktemp /tmp/ws.confXXX)
if [[ -n ${after_name} ]]; then
while read -r line; do
print "${line}"
if grep -q ${after_name} <<< "${line}" && ${not_written}; then
printf "workspace = %s, " ${new_id}
printf "defaultName:%s, " "${new_name}"
printf "monitor:%s, " "${active_mon}"
print "persistent:true, default:true"
not_written=false
fi
done < "${workspaces_conf}" | tee "${tmp}"
else
cp "${workspaces_conf}" "${tmp}"
printf "workspace = %s, " ${new_id} | tee -a "${tmp}"
printf "defaultName:%s, " "${new_name}" | tee -a "${tmp}"
printf "monitor:%s, " "${active_mon}" | tee -a "${tmp}"
print "persistent:true, default:true" | tee -a "${tmp}"
fi
mv "${tmp}" "${workspaces_conf}"
set +x
}
reorder_ws_conf () {
set -x
local found=false
local new_id=${1}; shift
local in="${1}"; shift
local tmp=$(mktemp /tmp/ws.confXXX)
while read -r line; do
if ! ${found}; then
print "${line}"
if grep -q ${new_id} <<< "${line}"; then
found=true
old_id=${curr_id}
new_id=$(( curr_id + 1 ))
fi
else
perl -pse 's/^workspace\s*=\s*$old,\s*/workspace = $new, /' -- -old="${curr_id}" -new="${new_id}" <<< "${line}"
old_id=${curr_id}
curr_id=$(( curr_id + 1 ))
fi
done < "${in}" | tee "${tmp}"
mv "${tmp}" "${in}"
}
create_workspace () {
active_mon=$(hyprctl -j monitors | jq '.[] | select(.focused) | .id')
workspaces="$(hyprctl -j workspaces | jq --compact-output .)"
first_ws=$(jq '.[0].id' <<< "${workspaces}")
last_ws=$(jq '.[-1].id' <<< "${workspaces}")
set -x
curr_id=$(hyprctl -j activeworkspace | jq --raw-output '.id')
last_id=$(jq '.[-1].id' <<< "${total_ws}")
new_id=$(( last_id + 1 ))
if [[ -n "${@}" ]]; then
new_name="${1}"
shift
else
print "Must suplly at least a new workspace name! Exiting..." >&2
exit 1
fi
# save current workspace name
print "${active_ws_name}" | \
tee /run/user/${UID}/hypr/${HYPRLAND_INSTANCE_SIGNATURE}/last.ws
if [[ -n "${@}" ]]; then
new_ws="${1}"
else
new_ws=$(( last_ws + 1 ))
fi
case ${#@} in
1)
# add at the end
new_name="${1}"
shift
add_persistent_ws ${new_id} "${new_name}"
;;
2)
new_name="${1}"; shift
after_name="${1}"; shift
after_id=$(jq --compact-output --raw-output --arg after "${after_name}" \
'.[] | select(.name == $after).id' <<< "${total_ws}")
new_id=$(( after_id + 1))
add_persistent_ws ${new_id} "${new_name}"
reorder_ws_conf ${new_id} "${workspaces_conf}"
;;
*)
print "Wrong number of arguments to ${0}! Exiting..." >&2
;;
esac
old_ws=$(hyprctl -j activeworkspace | jq '.id')
if [[ ${old_ws} -ne ${last_ws} ]]; then
hyprctl dispatch workspace ${last_ws}
fi
# create new workspace
hyprctl dispatch workspace +1
active_ws=$(hyprctl -j activeworkspace | jq '.id')
hyprctl dispatch renameworkspace ${active_ws} ${new_name}
hyprctl keyword workspace "${active_ws},defaultName:${new_name}, monitor:${active_mon}, persistent:true"
if [[ ${new_ws} -lt ${last_ws} ]]; then
for ws in {${new_ws}..${last_ws}}; do
curr_name=$(jq --raw-output --argjson ws ${ws} '.[] | select(.id == $ws) | .name' <<< "${workspaces}")
if [[ ${new_ws} -eq ${ws} ]]; then
next_name=${curr_name}
else
hyprctl dispatch renameworkspace ${ws} ${next_name}
next_name=${curr_name}
fi
done
last_ws=$(hyprctl -j workspaces | jq '.[-1].id')
hyprctl dispatch renameworkspace ${last_ws} ${next_name}
fi
hyprctl dispatch renameworkspace ${new_ws} ${new_name}
active_ws=$(hyprctl -j activeworkspace | jq '.id')
hyprctl keyword workspace "${active_ws},defaultName:${next_name}, monitor:${active_mon}, persistent:true"
#set -x
if [[ ${active_ws} -ne ${new_ws} ]]; then
hyprctl dispatch workspace ${new_ws}
create_workspace ${next_name}
hyprctl dispatch workspace ${old_ws}
fi
#set +x
hyprctl reload
set +x
}
delete_workspace () {
set -x
unset old_workspace
old_workspace="$(cat /run/user/${UID}/${HYPRLAND_INSTANCE_SIGNATURE}/last.ws)"
sed -i "/${active_ws_name}/d" ${workspaces_conf}
hyprctl dispatch workspace "name:${old_workspace}"
hyprctl reload
set +x
}
cycle_clients () {
hyprctl dispatch movefocus d
}