From 22ab796be713c524f2dbcab4babc8d1fe7580cce Mon Sep 17 00:00:00 2001 From: Peter Murphy <26548438+ptrfrncsmrph@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:46:09 -0400 Subject: [PATCH] Add grouped function --- src/Effect/Class/Console.purs | 8 ++++++++ src/Effect/Console.purs | 10 ++++++++++ test/Main.purs | 2 ++ test/expected_output.txt | 2 ++ 4 files changed, 22 insertions(+) diff --git a/src/Effect/Class/Console.purs b/src/Effect/Class/Console.purs index bb0ffca..d11db86 100644 --- a/src/Effect/Class/Console.purs +++ b/src/Effect/Class/Console.purs @@ -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) @@ -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 diff --git a/src/Effect/Console.purs b/src/Effect/Console.purs index d69f8d3..f3d08df 100644 --- a/src/Effect/Console.purs +++ b/src/Effect/Console.purs @@ -1,5 +1,6 @@ module Effect.Console where +import Control.Bind (discard, bind, pure) import Effect (Effect) import Data.Show (class Show, show) @@ -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 diff --git a/test/Main.purs b/test/Main.purs index a32b833..fee924f 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -18,3 +18,5 @@ main = do Console.log "log in groupCollapsed" Console.groupEnd Console.groupEnd + Console.grouped "grouped" do + Console.log "log in grouped" diff --git a/test/expected_output.txt b/test/expected_output.txt index 4f322e8..08e6d63 100644 --- a/test/expected_output.txt +++ b/test/expected_output.txt @@ -7,3 +7,5 @@ group log in group groupCollapsed log in groupCollapsed +grouped + log in grouped