Skip to content

Commit 3a0cf2e

Browse files
authored
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>
1 parent 3b83d7b commit 3a0cf2e

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Added `group`, `groupCollapsed`, and `groupEnd` (#42 by @pete-murphy)
1011

1112
Bugfixes:
1213

src/Effect/Class/Console.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Effect.Class.Console where
22

3+
import Control.Bind (discard, bind, pure)
34
import Data.Function ((<<<))
45
import Data.Show (class Show)
56
import Data.Unit (Unit)
@@ -47,3 +48,19 @@ timeEnd = liftEffect <<< EffConsole.timeEnd
4748

4849
clear :: forall m. MonadEffect m => m Unit
4950
clear = liftEffect EffConsole.clear
51+
52+
group :: forall m. MonadEffect m => String -> m Unit
53+
group = liftEffect <<< EffConsole.group
54+
55+
groupCollapsed :: forall m. MonadEffect m => String -> m Unit
56+
groupCollapsed = liftEffect <<< EffConsole.groupCollapsed
57+
58+
groupEnd :: forall m. MonadEffect m => m Unit
59+
groupEnd = liftEffect EffConsole.groupEnd
60+
61+
grouped :: forall m a. MonadEffect m => String -> m a -> m a
62+
grouped name inner = do
63+
group name
64+
result <- inner
65+
groupEnd
66+
pure result

src/Effect/Console.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,19 @@ export const timeEnd = function (s) {
4949
export const clear = function () {
5050
console.clear();
5151
};
52+
53+
export const group = function (s) {
54+
return function () {
55+
console.group(s);
56+
};
57+
};
58+
59+
export const groupCollapsed = function (s) {
60+
return function () {
61+
console.groupCollapsed(s);
62+
};
63+
};
64+
65+
export const groupEnd = function () {
66+
console.groupEnd();
67+
};

src/Effect/Console.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Effect.Console where
22

3+
import Control.Bind (discard, bind, pure)
34
import Effect (Effect)
45

56
import Data.Show (class Show, show)
@@ -66,3 +67,22 @@ foreign import timeEnd :: String -> Effect Unit
6667

6768
-- | Clears the console
6869
foreign import clear :: Effect Unit
70+
71+
-- | Creates a new inline group in the console. This indents following console
72+
-- | messages by an additional level, until `groupEnd` is called.
73+
foreign import group :: String -> Effect Unit
74+
75+
-- | Same as `group`, but groups are collapsed by default.
76+
foreign import groupCollapsed :: String -> Effect Unit
77+
78+
-- | Exits the current inline group in the console.
79+
foreign import groupEnd :: Effect Unit
80+
81+
-- | Perform an effect within the context of an inline group in the console.
82+
-- | Calls `group` and `groupEnd` before and after the effect, respectively.
83+
grouped :: forall a. String -> Effect a -> Effect a
84+
grouped name inner = do
85+
group name
86+
result <- inner
87+
groupEnd
88+
pure result

test/Main.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ main = do
1212
Console.error "error"
1313
Console.info "info"
1414
Console.debug "debug"
15+
Console.group "group"
16+
Console.log "log in group"
17+
Console.groupCollapsed "groupCollapsed"
18+
Console.log "log in groupCollapsed"
19+
Console.groupEnd
20+
Console.groupEnd
21+
Console.grouped "grouped" do
22+
Console.log "log in grouped"

test/expected_output.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ warn
33
error
44
info
55
debug
6+
group
7+
log in group
8+
groupCollapsed
9+
log in groupCollapsed
10+
grouped
11+
log in grouped

0 commit comments

Comments
 (0)