SHARE
TWEET

Untitled

a guest Jan 17th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Debug.Extra exposing (viewModel)
  2.  
  3. import Html exposing (Html, p, pre, text)
  4. import Html.Attributes exposing (style)
  5.  
  6.  
  7. quote =
  8.     "\""
  9.  
  10.  
  11. indentChars =
  12.     "[{("
  13.  
  14.  
  15. outdentChars =
  16.     "}])"
  17.  
  18.  
  19. newLineChars =
  20.     ","
  21.  
  22.  
  23. uniqueHead =
  24.     "##FORMAT##"
  25.  
  26.  
  27. incr =
  28.     20
  29.  
  30.  
  31. viewModel : a -> Html msg
  32. viewModel model =
  33.     let
  34.         lines =
  35.             model
  36.                 |> Debug.toString
  37.                 |> (\m ->
  38.                         "("
  39.                             ++ m
  40.                             ++ ")"
  41.                             |> formatString False 0
  42.                             |> String.split uniqueHead
  43.                    )
  44.     in
  45.     pre [] <| List.map viewLine lines
  46.  
  47.  
  48. viewLine : String -> Html msg
  49. viewLine lineStr =
  50.     let
  51.         ( indent, lineTxt ) =
  52.             splitLine lineStr
  53.     in
  54.     p
  55.         [ style "paddingLeft" (px indent)
  56.         , style "marginTop" "0px"
  57.         , style "marginBottom" "0px"
  58.         ]
  59.         [ text lineTxt ]
  60.  
  61.  
  62. px : Int -> String
  63. px int =
  64.     String.fromInt int
  65.         ++ "px"
  66.  
  67.  
  68. formatString : Bool -> Int -> String -> String
  69. formatString isInQuotes indent str =
  70.     case String.left 1 str of
  71.         "" ->
  72.             ""
  73.  
  74.         firstChar ->
  75.             if isInQuotes then
  76.                 if firstChar == quote then
  77.                     firstChar
  78.                         ++ formatString (not isInQuotes) indent (String.dropLeft 1 str)
  79.  
  80.                 else
  81.                     firstChar
  82.                         ++ formatString isInQuotes indent (String.dropLeft 1 str)
  83.  
  84.             else if String.contains firstChar newLineChars then
  85.                 uniqueHead
  86.                     ++ pad indent
  87.                     ++ firstChar
  88.                     ++ formatString isInQuotes indent (String.dropLeft 1 str)
  89.  
  90.             else if String.contains firstChar indentChars then
  91.                 uniqueHead
  92.                     ++ pad (indent + incr)
  93.                     ++ firstChar
  94.                     ++ formatString isInQuotes (indent + incr) (String.dropLeft 1 str)
  95.  
  96.             else if String.contains firstChar outdentChars then
  97.                 firstChar
  98.                     ++ uniqueHead
  99.                     ++ pad (indent - incr)
  100.                     ++ formatString isInQuotes (indent - incr) (String.dropLeft 1 str)
  101.  
  102.             else if firstChar == quote then
  103.                 firstChar
  104.                     ++ formatString (not isInQuotes) indent (String.dropLeft 1 str)
  105.  
  106.             else
  107.                 firstChar
  108.                     ++ formatString isInQuotes indent (String.dropLeft 1 str)
  109.  
  110.  
  111. pad : Int -> String
  112. pad indent =
  113.     String.padLeft 5 '0' <| Debug.toString indent
  114.  
  115.  
  116. splitLine : String -> ( Int, String )
  117. splitLine line =
  118.     let
  119.         indent =
  120.             String.left 5 line
  121.                 |> String.toInt
  122.                 |> Maybe.withDefault 0
  123.  
  124.         newLine =
  125.             String.dropLeft 5 line
  126.     in
  127.     ( indent, newLine )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top