Skip to content

Add group functions #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +81 to +83
Copy link
Contributor Author

@pete-murphy pete-murphy Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming we also want the Effect version I added here, in addition to the MonadEffect type suggested above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well 😄. Thanks!

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