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

View file

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