Skip to content

Commit

Permalink
feat(seed): add seed to the config so that we can have idempotent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Sep 4, 2024
1 parent 8238186 commit a215977
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
53 changes: 33 additions & 20 deletions src/Core.mad
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Query } from "@/Query"
import type { Tag } from "@/Tag"

import { now, toISOString } from "Date"
Expand All @@ -25,7 +24,7 @@ export alias SideEffect b = String -> b -> b
* The message decoration
* @since 0.0.1
*/
export alias Decorator a = Tag -> String -> a -> String
export alias Decorator a = String -> Tag -> String -> a -> String

/**
* The configuration of the signal
Expand All @@ -36,6 +35,7 @@ export alias Config = {
check :: PartyPredicate a,
decorate :: Decorator a,
effect :: SideEffect b,
seed :: String,
}

/**
Expand All @@ -46,9 +46,9 @@ export alias Config = {
signalWithConfig :: Config -> Tag -> String -> a -> a
export signalWithConfig = (conf, tag, message, x) => {
return where(conf) {
{ decorate, effect, change, check } =>
{ seed, decorate, effect, change, check } =>
do {
msg = decorate(tag, message, x)
msg = decorate(seed, tag, message, x)
return if (check(tag, msg, x)) do {
effect(msg, change(x))
return x
Expand All @@ -71,26 +71,26 @@ export always = (_, _, _) => true
* @since 0.0.1
*/
decorateRaw :: Decorator a
export decorateRaw = (_, m, _) => m
export decorateRaw = (_, _, m, _) => m


/**
* A means of colorizing a message by tag
* @since 0.0.1
*/
colorizeTag :: Tag -> String
export colorizeTag = pipe(
colorizeTag :: String -> Tag -> String
export colorizeTag = (seed, x) => pipe(
serialize,
(tagId) => colorize(tagId, " " ++ tagId ++ " "),
)
(tagId) => colorize(seed, " " ++ tagId ++ " "),
)(x)

/**
* Color by tag and prepend to message
* @since 0.0.1
*/
decorateWithStyle :: Decorator a
export decorateWithStyle = (tag, message, _) => {
idWithColor = colorizeTag(tag)
export decorateWithStyle = (seed, tag, message, _) => {
idWithColor = colorizeTag(seed, tag)
return idWithColor ++ " " ++ message
}

Expand All @@ -99,31 +99,44 @@ export decorateWithStyle = (tag, message, _) => {
* @since 0.0.1
*/
decorateWithStyleAndDate :: Decorator a
export decorateWithStyleAndDate = (tag, message, _) => {
idWithColor = colorizeTag(tag)
export decorateWithStyleAndDate = (seed, tag, message, _) => {
idWithColor = colorizeTag(seed, tag)
return idWithColor ++ " " ++ toISOString(now()) ++ " ▸ " ++ message
}

// (this currently has an issue)
// (this currently has an issue w/r/t partial application - it works but not as well as it should)
/**
* A sugar method to create a Config record, usually by partial application
* @since 0.0.1
*/
configurate :: Decorator a -> SideEffect b -> (a -> b) -> PartyPredicate a -> Config
export configurate = (decorate, effect, change, check) => ({ decorate, effect, change, check })
configurateWithSeed :: String
-> Decorator a
-> SideEffect b
-> (a -> b)
-> PartyPredicate a
-> Config
export configurateWithSeed = (seed, decorate, effect, change, check) => (
{ seed, decorate, effect, change, check }
)

export configurate = configurateWithSeed("party-bus")

export confSeed = (seed, conf) => (
{ seed, effect: conf.effect, change: conf.change, check: conf.check, decorate: conf.decorate }
)

export confDecorator = (decorate, conf) => (
{ decorate, effect: conf.effect, change: conf.change, check: conf.check }
{ decorate, effect: conf.effect, change: conf.change, check: conf.check, seed: conf.seed }
)

export confEffect = (effect, conf) => (
{ decorate: conf.decorate, effect, change: conf.change, check: conf.check }
{ decorate: conf.decorate, effect, change: conf.change, check: conf.check, seed: conf.seed }
)

export confChange = (change, conf) => (
{ change, decorate: conf.decorate, effect: conf.effect, check: conf.check }
{ change, decorate: conf.decorate, effect: conf.effect, check: conf.check, seed: conf.seed }
)

export confCheck = (check, conf) => (
{ check, decorate: conf.decorate, effect: conf.effect, change: conf.change }
{ check, decorate: conf.decorate, effect: conf.effect, change: conf.change, seed: conf.seed }
)
50 changes: 26 additions & 24 deletions src/Core.spec.mad
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
confCheck,
confDecorator,
confEffect,
confSeed,
configurate,
decorateRaw,
decorateWithStyle,
Expand All @@ -32,15 +33,15 @@ test(
},
)

test("decorateRaw", () => assertEquals(decorateRaw(tag("yo"), "cool", "blah"), "cool"))
test("decorateRaw", () => assertEquals(decorateRaw("party-bus", tag("yo"), "cool", "blah"), "cool"))

ANSI_ESCAPE = '\x1b'

test(
"decorateWithStyle",
() => assertEquals(
pipe(
decorateWithStyle($, "yo", "blah"),
decorateWithStyle("party-bus", $, "yo", "blah"),
String.toList,
)(tag("silly")),
[
Expand Down Expand Up @@ -68,13 +69,15 @@ test(
';',
'2',
';',
'5',
'1',
'0',
'3',
';',
'5',
'1',
'4',
'4',
';',
'2',
'7',
'5',
'm',
' ',
's',
Expand Down Expand Up @@ -105,7 +108,7 @@ test(
"decorateWithStyleAndDate",
() => assertEquals(
pipe(
decorateWithStyleAndDate(tag("hey"), "yo"),
decorateWithStyleAndDate("party-bus", tag("hey"), "yo"),
skipDateInStyledString,
)("blah"),
[
Expand All @@ -117,32 +120,28 @@ test(
';',
'2',
';',
'2',
'5',
'5',
'0',
';',
'2',
'5',
'5',
'0',
';',
'2',
'5',
'5',
'0',
';',
'4',
'8',
';',
'2',
';',
'1',
'9',
'9',
'2',
'0',
'0',
';',
'3',
'1',
'2',
'2',
';',
'8',
'4',
'1',
'1',
'6',
'm',
],
['h', 'e', 'y'],
Expand All @@ -159,6 +158,7 @@ test(
"STYLED_CONFIG-ish / taggedSignal-ish",
() => do {
conf = {
seed: "party-bus",
effect: binaryIdentity,
decorate: STYLED_CONFIG.decorate,
change: STYLED_CONFIG.change,
Expand All @@ -177,6 +177,7 @@ test(
"DEFAULT_CONFIG-ish / signal-ish",
() => do {
conf = {
seed: "party-bus",
effect: binaryIdentity,
decorate: DEFAULT_CONFIG.decorate,
change: DEFAULT_CONFIG.change,
Expand All @@ -192,13 +193,14 @@ test(
)

test(
"confDecorator + confChange",
"confDecorator + confChange + confSeed",
() => do {
conf = pipe(
confSeed("ahoy"),
confDecorator(decorateRaw),
confChange((_) => "x"),
)(DEFAULT_CONFIG)
_ <- assertEquals(conf.decorate(tag("a"), "b", "c"), "b")
_ <- assertEquals(conf.decorate("ahoy", tag("a"), "b", "c"), "b")
return assertEquals(conf.change("hello?"), "x")
},
)
Expand Down
5 changes: 5 additions & 0 deletions src/PartyBus.mad
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
confCheck,
confDecorator,
confEffect,
confSeed,
configurate,
decorateRaw,
decorateWithStyle,
Expand Down Expand Up @@ -41,6 +42,7 @@ export confChange
export confCheck
export confDecorator
export confEffect
export confSeed
export configurate
export decorateRaw
export decorateWithStyle
Expand All @@ -55,6 +57,7 @@ export tagWithScope
* @since 0.0.1
*/
export DEFAULT_CONFIG = {
seed: "party-bus",
decorate: decorateRaw,
check: always,
effect: IO.pTrace,
Expand All @@ -74,6 +77,7 @@ export signal = signalWithConfig(DEFAULT_CONFIG)
* @since 0.0.1
*/
export STYLED_CONFIG = {
seed: "party-bus",
decorate: decorateWithStyle,
effect: IO.pTrace,
change: identity,
Expand All @@ -92,6 +96,7 @@ export taggedSignal = signalWithConfig(STYLED_CONFIG)
* @since 0.0.1
*/
export DATED_CONFIG = {
seed: "party-bus",
decorate: decorateWithStyleAndDate,
effect: IO.pTrace,
change: identity,
Expand Down

0 comments on commit a215977

Please sign in to comment.