Skip to content

Commit

Permalink
Add grouped function
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-murphy committed Oct 4, 2023
1 parent 3103b89 commit 22ab796
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Effect/Class/Console.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Effect.Class.Console where

import Control.Bind (discard, bind, pure)
import Data.Function ((<<<))
import Data.Show (class Show)
import Data.Unit (Unit)
Expand Down Expand Up @@ -56,3 +57,10 @@ groupCollapsed = liftEffect <<< EffConsole.groupCollapsed

groupEnd :: forall m. MonadEffect m => m Unit
groupEnd = liftEffect EffConsole.groupEnd

grouped :: forall m a. MonadEffect m => String -> m a -> m a
grouped name inner = do
group name
result <- inner
groupEnd
pure result
10 changes: 10 additions & 0 deletions src/Effect/Console.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Effect.Console where

import Control.Bind (discard, bind, pure)
import Effect (Effect)

import Data.Show (class Show, show)
Expand Down Expand Up @@ -76,3 +77,12 @@ foreign import groupCollapsed :: String -> Effect Unit

-- | Exits the current inline group in the console.
foreign import groupEnd :: Effect Unit

-- | Perform an effect within the context of an inline group in the console.
-- | Calls `group` and `groupEnd` before and after the effect, respectively.
grouped :: forall a. String -> Effect a -> Effect a
grouped name inner = do
group name
result <- inner
groupEnd
pure result
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ main = do
Console.log "log in groupCollapsed"
Console.groupEnd
Console.groupEnd
Console.grouped "grouped" do
Console.log "log in grouped"
2 changes: 2 additions & 0 deletions test/expected_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ group
log in group
groupCollapsed
log in groupCollapsed
grouped
log in grouped

0 comments on commit 22ab796

Please sign in to comment.