Skip to content

Commit

Permalink
Add group functions (#42)
Browse files Browse the repository at this point in the history
* Add group functions

Fixes #41

* Add test

* Add to CHANGELOG

* Add grouped function

---------

Co-authored-by: Peter Murphy <[email protected]>
  • Loading branch information
pete-murphy and pete-murphy authored Oct 8, 2023
1 parent 3b83d7b commit 3a0cf2e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
17 changes: 17 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 @@ -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
16 changes: 16 additions & 0 deletions src/Effect/Console.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
20 changes: 20 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 @@ -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
8 changes: 8 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions test/expected_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ warn
error
info
debug
group
log in group
groupCollapsed
log in groupCollapsed
grouped
log in grouped

0 comments on commit 3a0cf2e

Please sign in to comment.