Skip to content
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

Update for 0.19 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions elm-package.json → elm.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"type": "package",
"name": "wintvelt/elm-print-any",
"version": "1.0.0",
"summary": "a tiny library to print any record to DOM or to console",
"repository": "https://github.com/wintvelt/elm-print-any.git",
"license": "BSD3",
"license": "BSD-3-Clause",
"source-directories": [
"src"
],
"exposed-modules": [
"PrintAny"
],
"dependencies": {
"elm-lang/core": "4.0.5 <= v < 6.0.0",
"elm-lang/html": "1.1.0 <= v < 3.0.0"
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/html": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
"test-dependencies": {},
"elm-version": "0.19.0 <= v < 0.20.0"
}
54 changes: 31 additions & 23 deletions src/PrintAny.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module PrintAny exposing (config, log, view, viewWithConfig)
module PrintAny exposing
( view, log
, config, viewWithConfig
)

{-| A tiny library for debugging purposes.
It prints any record to the console or to the DOM.
Expand All @@ -7,15 +10,14 @@ You can simply call `PrintAny.view myRecord` inside your `view` function,
to print `myRecord` to the DOM.

Or use `PrintAny.log myRecord` anywhere, to get a (somewhat) prettified version of the record in the console.

_PS: You may not want to use this with large records.
Performance is not optimal. This module iterates over a
string version of the record, which may take long time._
_PS: You may not want to use this with large records._
_Performance is not optimal. This module iterates over a_
_string version of the record, which may take long time._


# Basics

@docs view,log
@docs view, log


# Advanced
Expand All @@ -29,6 +31,7 @@ import Html.Attributes exposing (class, style)
import String



{- Library constants -}


Expand Down Expand Up @@ -100,6 +103,7 @@ defaultConfig =


{-| renders any record to the Dom.

Usage:

```Elm
Expand Down Expand Up @@ -137,39 +141,38 @@ view model =

-}
viewWithConfig : Config -> a -> Html msg
viewWithConfig (Config config) record =
viewWithConfig (Config conf) record =
let
lines =
record
|> toString
|> Debug.toString
|> splitWithQuotes
|> splitUnquotedWithChars
|> List.concat
|> mergeQuoted
|> addIndents
in
pre
(if config.className == "" then
(if conf.className == "" then
[]

else
[ class config.className ]
[ class conf.className ]
)
<|
List.map (viewLine <| Config config) lines
List.map (viewLine <| Config conf) lines



{- render a single formatted line to DOM -}


viewLine : Config -> ( Int, String ) -> Html msg
viewLine (Config config) ( indent, string ) =
viewLine (Config conf) ( indent, string ) =
p
[ style
[ ( "paddingLeft", px (indent * config.increment) )
, ( "marginTop", "0px" )
, ( "marginBottom", "0px" )
]
[ style "paddingLeft" (px (indent * conf.increment))
, style "marginTop" "0px"
, style "marginBottom" "0px"
]
[ text <| String.join "\n" <| String.split "\\n" string ]

Expand Down Expand Up @@ -213,7 +216,7 @@ log record =
let
lines =
record
|> toString
|> Debug.toString
|> splitWithQuotes
|> splitUnquotedWithChars
|> List.concat
Expand Down Expand Up @@ -249,8 +252,7 @@ logLine ( indent, string ) =

px : Int -> String
px int =
toString int
++ "px"
String.fromInt int ++ "px"


type alias IndentedString =
Expand Down Expand Up @@ -286,8 +288,10 @@ addIndent string startList =
( newIndentBefore, newIndentAfter ) =
if String.contains firstChar constants.indentChars then
( indentAfter + 1, indentAfter + 1 )

else if String.contains firstChar constants.outdentChars then
( indentAfter, indentAfter - 1 )

else
( indentAfter, indentAfter )
in
Expand Down Expand Up @@ -318,6 +322,7 @@ splitUnquotedWithChars stringList =
splitString string =
if String.left 1 string == constants.quote then
[ string ]

else
splitWithChars
(constants.indentChars ++ constants.newLineChars ++ constants.outdentChars)
Expand Down Expand Up @@ -358,6 +363,7 @@ splitWithChar splitter string =
(\ind str ->
if ind > 0 then
splitter ++ str

else
str
)
Expand All @@ -372,8 +378,9 @@ splitWithQuotes string =
String.split "\"" string
|> List.indexedMap
(\i str ->
if rem i 2 == 1 then
if remainderBy 2 i == 1 then
"\"" ++ str ++ "\""

else
str
)
Expand All @@ -399,6 +406,7 @@ mergeOneQuote string startList =

[] ->
[ string ]

else
-- simply add string as line to the list
startList ++ [ string ]
Expand All @@ -410,7 +418,7 @@ mergeOneQuote string startList =

pad : Int -> String
pad indent =
String.padLeft 5 '0' <| toString indent
String.padLeft 5 '0' <| String.fromInt indent


splitLine : String -> ( Int, String )
Expand All @@ -419,7 +427,7 @@ splitLine line =
indent =
String.left 5 line
|> String.toInt
|> Result.withDefault 0
|> Maybe.withDefault 0

newLine =
String.dropLeft 5 line
Expand Down