Skip to content

Instantly share code, notes, and snippets.

@Fresheyeball
Last active June 6, 2016 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fresheyeball/94e25fcefc517a74382e55283eec9093 to your computer and use it in GitHub Desktop.
Save Fresheyeball/94e25fcefc517a74382e55283eec9093 to your computer and use it in GitHub Desktop.
module Debug.Pretty exposing (..)
import String
import Native.Pretty
type alias Model =
( Int, List Char )
n : Char
n =
Native.Pretty.n
t : Char
t =
Native.Pretty.t
tab : Int -> List Char
tab depth =
[ n ] ++ List.repeat depth t
type alias Parse =
{ depth : Int
, isTup : Bool
, isList : Bool
, res : List Char
}
init : Parse
init =
{ depth = 1
, isTup = False
, isList = False
, res = []
}
newlineOnOpen : Char -> Parse -> Parse
newlineOnOpen char ({ depth, isTup, isList, res } as parse) =
let
charAdded =
{ parse | res = res ++ [ char ] }
in
case char of
'{' ->
{ parse
| depth = depth + 1
, res = res ++ tab (depth + 1) ++ [ char ]
}
'}' ->
{ parse
| depth = depth - 1
, res = res ++ tab depth ++ [ char ]
}
',' ->
let
tabs = if isTup || isList
then []
else tab depth
in
{ parse
| res =
res ++ tabs ++ [ char ]
}
'(' ->
{ charAdded | isTup = True }
')' ->
{ charAdded | isTup = False }
'[' ->
{ charAdded | isList = True }
']' ->
{ charAdded | isList = False }
_ ->
charAdded
pretty : String -> String
pretty =
String.toList
>> List.foldl newlineOnOpen init
>> .res
>> String.fromList
log : String -> a -> a
log s a =
toString a
|> pretty
|> Native.Pretty.log s
|> always a
/*global F2*/
/*eslint camelcase: 0*/
/*eslint no-unused-vars: 0*/
/*
* Direct `String` access to `console.log`.
* Needed because `Debug.log` doesn't pretty print.
*/
var _capillary$customer$Native_Pretty = (function () {
function log (tag, value) {
var msg = tag + ': ' + value
console.log(msg)
return value
}
return {
log: F2(log),
// expose newline and tabs special chars
n: '\n',
t: '\t'
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment