<commit_before>port module Main exposing (..)


import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json


main =
  Html.programWithFlags
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }


-- MODEL


type alias Model =
  { userId : String
  , input : String
  , messages : List Message
  }


type alias Message =
  { authorId : String
  , message : String
  }


type alias Flags =
  { userId : String
  }


init : Flags -> (Model, Cmd Msg)
init {userId} =
  ( Model userId "" []
  , Cmd.none
  )


-- UPDATE


type Msg
  = Input String
  | SendOnEnter Int
  | Fetch (List Message)


port save : Message -> Cmd msg


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Input newInput ->
      ( { model | input = newInput }
      , Cmd.none
      )

    SendOnEnter code ->
      if code == 13 then
        let
          message =
            Message model.userId model.input
        in
          ( { model | input = "" }
          , save message
          )
      else
        (model, Cmd.none)

    Fetch messages ->
      ( { model | messages = messages }
      , Cmd.none
      )


-- SUBSCRIPTIONS


port fromHorizon : (List Message -> msg) -> Sub msg


subscriptions : Model -> Sub Msg
subscriptions model =
  fromHorizon Fetch


-- VIEW


view : Model -> Html Msg
view model =
  div []
    [ ul [ class "message-list" ] (List.map chatMessage model.messages)
    , input
      [ type' "text"
      , class "new-message-field"
      , autofocus True
      , value model.input
      , onInput Input
      , onKeyPress SendOnEnter
      ] []
    ]


chatMessage : Message -> Html Msg
chatMessage {authorId, message} =
  let
    avatarUrl =
      "http://api.adorable.io/avatars/50/" ++ authorId ++ ".png"
  in
    li [ class "message" ]
      [ img [ class "avatar", src avatarUrl ] []
      , span [ class "message-text" ] [ text message ]
      ]


onKeyPress : (Int -> msg) -> Attribute msg
onKeyPress tagger =
  on "keypress" (Json.map tagger keyCode)<commit_msg>Update code style based on TodoMVC example<commit_after>port module Main exposing (..)


import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json


main =
  Html.programWithFlags
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }


-- MODEL


type alias Model =
  { userId : String
  , input : String
  , messages : List Message
  }


type alias Message =
  { authorId : String
  , message : String
  }


type alias Flags =
  { userId : String
  }


emptyModel : Model
emptyModel =
  { userId = ""
  , input = ""
  , messages = []
  }


init : Flags -> (Model, Cmd Msg)
init {userId} =
  { emptyModel
    | userId = userId
  }
    ! []


-- UPDATE


type Msg
  = NoOp
  | UpdateInput String
  | Save
  | Fetch (List Message)


port save : Message -> Cmd msg


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp ->
      model ! []

    UpdateInput newInput ->
      { model | input = newInput }
        ! []

    Save ->
      { model | input = "" }
        ! [ save (Message model.userId model.input) ]

    Fetch messages ->
      { model | messages = messages }
        ! []


-- SUBSCRIPTIONS


port fromHorizon : (List Message -> msg) -> Sub msg


subscriptions : Model -> Sub Msg
subscriptions model =
  fromHorizon Fetch


-- VIEW


view : Model -> Html Msg
view model =
  div []
    [ ul [ class "message-list" ] (List.map chatMessage model.messages)
    , input
      [ type' "text"
      , class "new-message-field"
      , autofocus True
      , value model.input
      , onInput UpdateInput
      , onEnter NoOp Save
      ] []
    ]


chatMessage : Message -> Html Msg
chatMessage {authorId, message} =
  let
    avatarUrl =
      "http://api.adorable.io/avatars/50/" ++ authorId ++ ".png"
  in
    li [ class "message" ]
      [ img [ class "avatar", src avatarUrl ] []
      , span [ class "message-text" ] [ text message ]
      ]


onEnter : msg -> msg -> Attribute msg
onEnter fail success =
  let
    tagger code =
      if code == 13 then success else fail
  in
    on "keyup" (Json.map tagger keyCode)
{'fraction_non_alphanumeric': 0.11359963057030709, 'fraction_numerical': 0.0018471484645578389, 'mean_word_length': 2.083274021352313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1719218', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1390, 'n_words': 418}