From 3a0cf2ee816d9d93063a2c0d07a3e54a635f136b Mon Sep 17 00:00:00 2001 From: Pete Murphy <26548438+pete-murphy@users.noreply.github.com> Date: Sun, 8 Oct 2023 13:06:01 -0400 Subject: [PATCH] Add group functions (#42) * Add group functions Fixes #41 * Add test * Add to CHANGELOG * Add grouped function --------- Co-authored-by: Peter Murphy <26548438+ptrfrncsmrph@users.noreply.github.com> --- CHANGELOG.md | 1 + src/Effect/Class/Console.purs | 17 +++++++++++++++++ src/Effect/Console.js | 16 ++++++++++++++++ src/Effect/Console.purs | 20 ++++++++++++++++++++ test/Main.purs | 8 ++++++++ test/expected_output.txt | 6 ++++++ 6 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4018d87..924de61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Added `group`, `groupCollapsed`, and `groupEnd` (#42 by @pete-murphy) Bugfixes: diff --git a/src/Effect/Class/Console.purs b/src/Effect/Class/Console.purs index 8f22f8e..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) @@ -47,3 +48,19 @@ timeEnd = liftEffect <<< EffConsole.timeEnd clear :: forall m. MonadEffect m => m Unit clear = liftEffect EffConsole.clear + +group :: forall m. MonadEffect m => String -> m Unit +group = liftEffect <<< EffConsole.group + +groupCollapsed :: forall m. MonadEffect m => String -> m Unit +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.js b/src/Effect/Console.js index b96fed8..8db1f20 100644 --- a/src/Effect/Console.js +++ b/src/Effect/Console.js @@ -49,3 +49,19 @@ export const timeEnd = function (s) { export const clear = function () { console.clear(); }; + +export const group = function (s) { + return function () { + console.group(s); + }; +}; + +export const groupCollapsed = function (s) { + return function () { + console.groupCollapsed(s); + }; +}; + +export const groupEnd = function () { + console.groupEnd(); +}; diff --git a/src/Effect/Console.purs b/src/Effect/Console.purs index 9aec0ed..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) @@ -66,3 +67,22 @@ foreign import timeEnd :: String -> Effect Unit -- | Clears the console foreign import clear :: Effect Unit + +-- | Creates a new inline group in the console. This indents following console +-- | messages by an additional level, until `groupEnd` is called. +foreign import group :: String -> Effect Unit + +-- | Same as `group`, but groups are collapsed by default. +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 f4947f1..fee924f 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -12,3 +12,11 @@ main = do Console.error "error" Console.info "info" Console.debug "debug" + Console.group "group" + Console.log "log in group" + Console.groupCollapsed "groupCollapsed" + 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 2c91449..08e6d63 100644 --- a/test/expected_output.txt +++ b/test/expected_output.txt @@ -3,3 +3,9 @@ warn error info debug +group + log in group + groupCollapsed + log in groupCollapsed +grouped + log in grouped