Skip to content

Commit

Permalink
Add 'all' option for --notify-action; change 'command'
Browse files Browse the repository at this point in the history
Previously, '--notify-action command' implied '--notify-action final'
i.e. a notification is sent after each command __and__ when shrun
itself finished (the latter is what 'final' does).

This is not ideal in the sense that there are valid reasons one may
want command notifs but not the final one (e.g. redundant when running
a single command or too noisy).

Thus we relax 'command' to only send notifs after the commands
themselves. A new option 'all' has been added that sends off notifs
for everything.
  • Loading branch information
tbidne committed Feb 21, 2024
1 parent 0a47c62 commit 56a6c7c
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ the major/minor/patch definitions apply to the application's interface / usage
(e.g. cli args, config file), not the library.

## [Unreleased]
### Changed
* `--notify-action command` (`notify.action = command`) no longer implies
`--notify-action final`. There is a new option `--notify-action all` for
that.

### Added
* New option `--cmd-log-size` (toml: `cmd-log-size`) that controls the size of
logs we read from command with `--cmd-log` and `--file-log`.
Expand Down
4 changes: 2 additions & 2 deletions configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ These options configure `shrun` to send off desktop notifications for certain ac

### Notify Action

**Arg:** `--notify-action (final|command)`
**Arg:** `--notify-action (final|command|all)`

**Description:** Sends notifications for various actions. `final` sends off a notification when `shrun` itself finishes whereas `command` (which implies `final`) sends one off each time a command finishes.
**Description:** Sends notifications for various actions. `final` sends off a notification when `shrun` itself finishes whereas `command` sends one off each time a command finishes. `all` implies `final` and `command`.

**Example:**

Expand Down
4 changes: 2 additions & 2 deletions examples/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ line-trunc = 150
# Mandatory, turns on notifications.
#
# "final" -> sends off a notification when all commands finish
# "command" -> sends off a notification when each command finishes. Implies
# "final"
# "command" -> sends off a notification when each command finishes.
# "all" -> implies "final" and "command".
action = "command"

# For linux, must be one of "dbus" or "notify-send". Defaults to "dbus".
Expand Down
4 changes: 2 additions & 2 deletions examples/config_osx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ line-trunc = 150
# Mandatory, turns on notifications.
#
# "final" -> sends off a notification when all commands finish
# "command" -> sends off a notification when each command finishes. Implies
# "final"
# "command" -> sends off a notification when each command finishes.
# "all" -> implies "final" and "command".
action = "command"

# For linux, must be one of "dbus" or "notify-send". Defaults to "dbus".
Expand Down
4 changes: 2 additions & 2 deletions examples/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ val = 'b'
# Mandatory, turns on notifications.
#
# "final" -> sends off a notification when all commands finish
# "command" -> sends off a notification when each command finishes. Implies
# "final"
# "command" -> sends off a notification when each command finishes.
# "all" -> implies "final" and "command".
#action = "command"

# For linux, must be one of "dbus" or "notify-send". Defaults to "dbus".
Expand Down
24 changes: 17 additions & 7 deletions src/Shrun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ import Shrun.Logging.Types
)
import Shrun.Notify qualified as Notify
import Shrun.Notify.MonadNotify (MonadNotify)
import Shrun.Notify.Types (_NotifyCommand)
import Shrun.Notify.Types
( NotifyAction
( NotifyAll,
NotifyCommand,
NotifyFinal
),
)
import Shrun.Prelude
import Shrun.ShellT (ShellT, runShellT)
import Shrun.Utils qualified as Utils
Expand Down Expand Up @@ -167,10 +173,12 @@ runCommand cmd = do
keyHide = logging ^. #keyHide
formattedCmd = LogFmt.formatCommand keyHide cmdNameTrunc cmd

-- Sent off notif if NotifyCommand is set
-- Sent off notif if NotifyAll or NotifyCommand is set
cfg <- asks getNotifyConfig
when (is (_Just % #action % _NotifyCommand) cfg)
$ Notify.sendNotif (formattedCmd <> " Finished") timeMsg urgency
case cfg ^? (_Just % #action) of
Just NotifyAll -> Notify.sendNotif (formattedCmd <> " Finished") timeMsg urgency
Just NotifyCommand -> Notify.sendNotif (formattedCmd <> " Finished") timeMsg urgency
_ -> pure ()

printFinalResult ::
forall m env e b.
Expand Down Expand Up @@ -222,10 +230,12 @@ printFinalResult totalTime result = withRegion Linear $ \r -> do
anyError <- readTVarA =<< asks getAnyError
let urgency = if anyError then Critical else Normal

-- Sent off notif if notifications are on
-- Sent off notif if NotifyAll or NotifyFinal is set
cfg <- asks getNotifyConfig
when (is _Just cfg)
$ Notify.sendNotif "Shrun Finished" totalTimeTxt urgency
case cfg ^? (_Just % #action) of
Just NotifyAll -> Notify.sendNotif "Shrun Finished" totalTimeTxt urgency
Just NotifyFinal -> Notify.sendNotif "Shrun Finished" totalTimeTxt urgency
_ -> pure ()

Logging.putRegionLog r finalLog

Expand Down
5 changes: 3 additions & 2 deletions src/Shrun/Configuration/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,9 @@ notifyActionParser =
helpTxt =
mconcat
[ "Sends notifications for various actions. 'Final' sends off a ",
"notification when Shrun itself finishes whereas 'command' (which ",
"implies 'final') sends off one each time a command finishes."
"notification when Shrun itself finishes whereas 'command' sends ",
"off one each time a command finishes. 'All' implies 'final' and ",
"'command'."
]

noNotifyActionParser :: Parser Bool
Expand Down
8 changes: 5 additions & 3 deletions src/Shrun/Notify/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ import Text.Read qualified as TR
data NotifyAction
= -- | Send a notification after all commands are completed.
NotifyFinal
| -- | Send notifications when each command completes. Implies
-- 'NotifyFinal'.
| -- | Send notifications when each command completes.
NotifyCommand
| -- | NotifyFinal and NotifyCommand.
NotifyAll
deriving stock (Eq, Show)

makePrisms ''NotifyAction
Expand All @@ -70,6 +71,7 @@ instance DecodeTOML NotifyAction where
parseNotifyAction :: (MonadFail m) => Text -> m NotifyAction
parseNotifyAction "final" = pure NotifyFinal
parseNotifyAction "command" = pure NotifyCommand
parseNotifyAction "all" = pure NotifyAll
parseNotifyAction other =
fail
$ mconcat
Expand All @@ -81,7 +83,7 @@ parseNotifyAction other =

-- | Available 'NotifyAction' strings.
notifyActionStr :: (IsString a) => a
notifyActionStr = "(final|command)"
notifyActionStr = "(final|command|all)"

-- | Maps DBus to its phased param.
type family DBusF p where
Expand Down
2 changes: 1 addition & 1 deletion test/functional/Functional/Examples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ notifyTimeoutNever = testCase "Runs --notify-timeout never" $ do
[ "--notify-system",
notifySystemArg,
"--notify-action",
"command",
"all",
"--notify-timeout",
"never",
"sleep 2",
Expand Down
36 changes: 30 additions & 6 deletions test/functional/Functional/Notify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ notifyTests :: [TestTree]
notifyTests =
[ notifySystem,
notifyActionCommand,
notifyActionAll,
notifyTimeout5
]

Expand All @@ -30,7 +31,7 @@ notifyTests =
-- - notifyActionFinal
-- - notifyActionTimeoutNever
--
-- are part of the Readme module
-- are part of the Examples test module

notifySystem :: TestTree
notifySystem = testCase ("Runs --notify-system " <> notifySystemArg) $ do
Expand All @@ -48,12 +49,35 @@ notifySystem = testCase ("Runs --notify-system " <> notifySystemArg) $ do
]
expected =
[ MkShrunNote
{ summary = "Shrun Finished",
{ summary = "[sleep 3] Finished",
body = "3 seconds",
urgency = Normal,
timeout = NotifyTimeoutSeconds 10
},
MkShrunNote
{ summary = "[sleep 2] Finished",
body = "2 seconds",
urgency = Normal,
timeout = NotifyTimeoutSeconds 10
}
]

notifyActionCommand :: TestTree
notifyActionCommand = testCase "Runs --notify-action command" $ do
results <- readIORef =<< runNotes args
expected @=? results
where
args =
withNoConfig
[ "--notify-action",
"command",
"--notify-system",
notifySystemArg,
"sleep 2",
"sleep 3"
]
expected =
[ MkShrunNote
{ summary = "[sleep 3] Finished",
body = "3 seconds",
urgency = Normal,
Expand All @@ -67,15 +91,15 @@ notifySystem = testCase ("Runs --notify-system " <> notifySystemArg) $ do
}
]

notifyActionCommand :: TestTree
notifyActionCommand = testCase "Runs --notify-action command" $ do
notifyActionAll :: TestTree
notifyActionAll = testCase "Runs --notify-action all" $ do
results <- readIORef =<< runNotes args
expected @=? results
where
args =
withNoConfig
[ "--notify-action",
"command",
"all",
"--notify-system",
notifySystemArg,
"sleep 2",
Expand Down Expand Up @@ -110,7 +134,7 @@ notifyTimeout5 = testCase "Runs --notify-timeout 5" $ do
args =
withNoConfig
[ "--notify-action",
"command",
"all",
"--notify-system",
notifySystemArg,
"--notify-timeout",
Expand Down
4 changes: 2 additions & 2 deletions test/integration/Integration/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Shrun.Data.TimerFormat
),
)
import Shrun.Notify.Types
( NotifyAction (NotifyCommand, NotifyFinal),
( NotifyAction (NotifyAll, NotifyCommand, NotifyFinal),
NotifySystem (AppleScript, DBus, NotifySend),
NotifyTimeout (NotifyTimeoutNever, NotifyTimeoutSeconds),
)
Expand Down Expand Up @@ -113,7 +113,7 @@ usesDefaultConfigFile = testCase "No arguments should use config from default fi
cmdLogLineTrunc = Just 150,
fileLog = True,
fileLogStripControl = Just StripControlNone,
notifyAction = Just NotifyCommand,
notifyAction = Just NotifyAll,
#if OSX
notifySystem = Just AppleScript,
#else
Expand Down
4 changes: 2 additions & 2 deletions test/integration/Integration/Miscellaneous.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Shrun.Configuration.Env.Types
import Shrun.Data.Command (Command (MkCommand))
import Shrun.Data.TimerFormat (TimerFormat (DigitalFull, ProseCompact))
import Shrun.Notify.Types
( NotifyAction (NotifyCommand),
( NotifyAction (NotifyAll, NotifyCommand),
NotifySystem (AppleScript, DBus),
NotifyTimeout (NotifyTimeoutNever),
)
Expand Down Expand Up @@ -148,7 +148,7 @@ usesRecursiveCmdExample = testCase "Uses recursive command from example" $ do
cmdLogLineTrunc = Just 150,
fileLog = True,
fileLogStripControl = Just StripControlNone,
notifyAction = Just NotifyCommand,
notifyAction = Just NotifyAll,
#if OSX
notifySystem = Just AppleScript,
#else
Expand Down
2 changes: 1 addition & 1 deletion test/integration/toml/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ strip-control = "none"

[notify]
system = "dbus"
action = "command"
action = "all"
timeout = "never"
2 changes: 1 addition & 1 deletion test/integration/toml/osx/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ strip-control = "none"

[notify]
system = "apple-script"
action = "command"
action = "allss"
timeout = "never"
10 changes: 9 additions & 1 deletion test/unit/Unit/Shrun/Configuration/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import Shrun.Data.TimerFormat
),
)
import Shrun.Notify.Types
( NotifyAction (NotifyCommand, NotifyFinal),
( NotifyAction (NotifyAll, NotifyCommand, NotifyFinal),
NotifySystem (AppleScript, DBus, NotifySend),
NotifyTimeout (NotifyTimeoutNever, NotifyTimeoutSeconds),
)
Expand Down Expand Up @@ -795,6 +795,7 @@ notifyActionSpecs =
"Notify action parsing"
[ parseNotifyActionFinal,
parseNotifyActionCommand,
parseNotifyActionAll,
parseNoNotifyAction
]

Expand All @@ -812,6 +813,13 @@ parseNotifyActionCommand = testCase desc $ verifyResult argList expected
argList = ["--notify-action", "command", "command"]
expected = updateDefArgs #notifyAction NotifyCommand

parseNotifyActionAll :: TestTree
parseNotifyActionAll = testCase desc $ verifyResult argList expected
where
desc = "Should parse --notify-action all"
argList = ["--notify-action", "all", "command"]
expected = updateDefArgs #notifyAction NotifyAll

parseNoNotifyAction :: TestTree
parseNoNotifyAction =
testCase "Parse --no-notify-action"
Expand Down

0 comments on commit 56a6c7c

Please sign in to comment.