From 4bc6666d0b84fa271eb11748cfa878f3ca557175 Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 2 Apr 2026 18:44:25 +0100 Subject: [PATCH] feat: mute and unmute require explicit target (all or tag) --- nag | 51 +++++++++++++++++++++++++------------------------- test/mute.bats | 26 +++++++++++++++++-------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/nag b/nag index 20383d6..c11d701 100755 --- a/nag +++ b/nag @@ -1228,8 +1228,8 @@ Usage: ${_ME} tag list alarms with a tag ${_ME} untag remove tags from an alarm ${_ME} check check and fire due alarms - ${_ME} mute mute alarm sounds - ${_ME} unmute unmute alarm sounds + ${_ME} mute mute alarm sounds + ${_ME} unmute unmute alarm sounds ${_ME} edit edit alarms file directly ${_ME} help [] show help ${_ME} version show version @@ -1808,31 +1808,32 @@ every() { describe "mute" <] + ${_ME} mute 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 \\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" <] + ${_ME} unmute 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 \\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 } diff --git a/test/mute.bats b/test/mute.bats index 27fa2bf..4d583f6 100644 --- a/test/mute.bats +++ b/test/mute.bats @@ -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]." ]]