feat: mute and unmute require explicit target (all or tag)

This commit is contained in:
Lewis Wynne 2026-04-02 18:44:25 +01:00
parent 79e90ed8ce
commit 4bc6666d0b
2 changed files with 44 additions and 33 deletions

51
nag
View file

@ -1228,8 +1228,8 @@ Usage:
${_ME} tag <tag> list alarms with a tag
${_ME} untag <id> <tags...> remove tags from an alarm
${_ME} check check and fire due alarms
${_ME} mute mute alarm sounds
${_ME} unmute unmute alarm sounds
${_ME} mute <all|tag> mute alarm sounds
${_ME} unmute <all|tag> unmute alarm sounds
${_ME} edit edit alarms file directly
${_ME} help [<subcommand>] show help
${_ME} version show version
@ -1808,31 +1808,32 @@ every() {
describe "mute" <<HEREDOC
Usage:
${_ME} mute [<tag>]
${_ME} mute <all|tag>
Description:
Mute alarm sounds. With no argument, mutes all alarms globally.
With a tag, mutes only alarms with that tag.
Mute alarm sounds. Use 'all' to mute everything, or specify a tag.
HEREDOC
mute() {
_ensure_nag_dir
local _tag="${1:-}"
local _target="${1:-}"
[[ -n "${_target}" ]] || _exit_1 printf "Usage: %s mute <all|tag>\\n" "${_ME}"
if [[ -z "${_tag}" ]]
_ensure_nag_dir
if [[ "${_target}" == "all" ]]
then
printf "*\\n" > "${_MUTE_FILE}"
printf "Muted all.\\n"
printf "*\n" > "${_MUTE_FILE}"
printf "Muted all.\n"
else
if [[ -f "${_MUTE_FILE}" ]] && grep -Fxq '*' "${_MUTE_FILE}"
then
printf "Muted [%s].\\n" "${_tag}"
printf "Muted [%s].\n" "${_target}"
return 0
fi
if ! [[ -f "${_MUTE_FILE}" ]] || ! grep -Fxq "${_tag}" "${_MUTE_FILE}"
if ! [[ -f "${_MUTE_FILE}" ]] || ! grep -Fxq "${_target}" "${_MUTE_FILE}"
then
printf "%s\\n" "${_tag}" >> "${_MUTE_FILE}"
printf "%s\n" "${_target}" >> "${_MUTE_FILE}"
fi
printf "Muted [%s].\\n" "${_tag}"
printf "Muted [%s].\n" "${_target}"
fi
}
@ -1840,34 +1841,34 @@ mute() {
describe "unmute" <<HEREDOC
Usage:
${_ME} unmute [<tag>]
${_ME} unmute <all|tag>
Description:
Unmute alarm sounds. With no argument, unmutes everything.
With a tag, unmutes only that tag.
Unmute alarm sounds. Use 'all' to unmute everything, or specify a tag.
HEREDOC
unmute() {
local _tag="${1:-}"
local _target="${1:-}"
[[ -n "${_target}" ]] || _exit_1 printf "Usage: %s unmute <all|tag>\\n" "${_ME}"
if [[ -z "${_tag}" ]]
if [[ "${_target}" == "all" ]]
then
if [[ -f "${_MUTE_FILE}" ]]
then
rm -f "${_MUTE_FILE}"
printf "Unmuted all.\\n"
printf "Unmuted all.\n"
else
printf "Sound is not muted.\\n"
printf "Sound is not muted.\n"
fi
else
_ensure_nag_dir
if [[ -f "${_MUTE_FILE}" ]] && grep -Fxq "${_tag}" "${_MUTE_FILE}"
if [[ -f "${_MUTE_FILE}" ]] && grep -Fxq "${_target}" "${_MUTE_FILE}"
then
{ grep -Fxv "${_tag}" "${_MUTE_FILE}" || true; } > "${_MUTE_FILE}.tmp"
{ grep -Fxv "${_target}" "${_MUTE_FILE}" || true; } > "${_MUTE_FILE}.tmp"
mv -f "${_MUTE_FILE}.tmp" "${_MUTE_FILE}"
else
printf "!%s\\n" "${_tag}" >> "${_MUTE_FILE}"
printf "!%s\n" "${_target}" >> "${_MUTE_FILE}"
fi
printf "Unmuted [%s].\\n" "${_tag}"
printf "Unmuted [%s].\n" "${_target}"
fi
}

View file

@ -2,14 +2,19 @@
load test_helper
@test "mute with no args creates mute file with global entry" {
run_nag mute
@test "mute all creates mute file with global entry" {
run_nag mute all
[ "${status}" -eq 0 ]
[[ "${output}" =~ "Muted all." ]]
[ -f "${NAG_DIR}/mute" ]
grep -q "^\*$" "${NAG_DIR}/mute"
}
@test "mute with no args fails" {
run_nag mute
[ "${status}" -eq 1 ]
}
@test "mute tag adds tag to mute file" {
run_nag mute work
[ "${status}" -eq 0 ]
@ -18,29 +23,34 @@ load test_helper
}
@test "mute tag skips if global mute already set" {
run_nag mute
run_nag mute all
run_nag mute work
[ "${status}" -eq 0 ]
[[ "${output}" =~ "Muted [work]." ]]
[ "$(wc -l < "${NAG_DIR}/mute")" -eq 1 ]
}
@test "unmute with no args deletes mute file" {
run_nag mute
run_nag unmute
@test "unmute all deletes mute file" {
run_nag mute all
run_nag unmute all
[ "${status}" -eq 0 ]
[[ "${output}" =~ "Unmuted all." ]]
[ ! -f "${NAG_DIR}/mute" ]
}
@test "unmute when not muted says so" {
@test "unmute with no args fails" {
run_nag unmute
[ "${status}" -eq 1 ]
}
@test "unmute all when not muted says so" {
run_nag unmute all
[ "${status}" -eq 0 ]
[[ "${output}" =~ "not muted" ]]
}
@test "unmute tag adds !tag when global mute is set" {
run_nag mute
run_nag mute all
run_nag unmute work
[ "${status}" -eq 0 ]
[[ "${output}" =~ "Unmuted [work]." ]]