<commit_before>module Main exposing (main)

import Html exposing (Html, a, button, div, form, h2, nav, option, pre, select, text, textarea)
import Html.App
import Html.Attributes exposing (class, href, name, rows, selected)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode as Json exposing ((:=))
import List exposing ((::))
import List.Extra as List
import Task exposing (Task)


-- MODEL


type alias Urls =
    { examples : String
    , eval : String
    }


type alias Example =
    { name : String
    , src : String
    }


type alias Model =
    { urls : Urls
    , examples : List Example
    , selectedExample : Maybe String
    , src : String
    , evalResult : String
    }


init : ( Model, Cmd Msg )
init =
    let
        model =
            { urls =
                { examples = "examples"
                , eval = "eval"
                }
            , examples = []
            , selectedExample = Nothing
            , src = ""
            , evalResult = ""
            }
    in
        ( model, getExamples model )


initExamples : List Example -> Model -> Model
initExamples examples model =
    case List.head examples of
        Just example ->
            setExample example.name { model | examples = examples }

        Nothing ->
            model


getExample : String -> Model -> Maybe String
getExample name model =
    model.examples
        |> List.find (\example -> example.name == name)
        |> Maybe.map (.src)


setExample : String -> Model -> Model
setExample name model =
    let
        ( selectedExample, src ) =
            case getExample name model of
                Just src ->
                    ( Just name, src )

                Nothing ->
                    ( Nothing, model.src )
    in
        { model | selectedExample = selectedExample, src = src }


setSource : String -> Model -> Model
setSource src model =
    { model | src = src, selectedExample = Nothing }



-- MESSAGES


type Msg
    = Eval
    | EvalSucceed String
    | EvalFail Http.Error
    | ExamplesSucceed (List Example)
    | ExamplesFail Http.Error
    | SelectExample String
    | EditSource String



-- UPDATE


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Eval ->
            ( model, postEval model )

        EvalSucceed result ->
            ( { model | evalResult = result }, Cmd.none )

        EvalFail _ ->
            ( model, Cmd.none )

        ExamplesSucceed examples ->
            ( initExamples examples model, Cmd.none )

        ExamplesFail _ ->
            ( model, Cmd.none )

        SelectExample name ->
            ( setExample name model, Cmd.none )

        EditSource src ->
            ( setSource src model, Cmd.none )



-- VIEW


exampleSelect : Model -> Html Msg
exampleSelect model =
    let
        selectedAttr key =
            selected (model.selectedExample == key)

        exampleOption example =
            option [ name example.name, selectedAttr (Just example.name) ]
                [ text example.name ]

        defaultOption =
            option [ selectedAttr Nothing ] [ text "Select example…" ]
    in
        div [ class "form pull-xs-right" ]
            [ select [ class "form-control custom-select", onInput SelectExample ]
                (defaultOption :: List.map exampleOption model.examples)
            ]


editor : Model -> Html Msg
editor model =
    textarea [ class "editor form-control", rows 25, onInput EditSource ]
        [ text model.src ]


evalResult : Model -> Html Msg
evalResult model =
    div [ class "card" ]
        [ div [ class "card-header" ]
            [ nav [ class "nav" ]
                [ text "Result"
                , button [ class "btn btn-primary pull-xs-right", onClick Eval ]
                    [ text "Eval" ]
                ]
            ]
        , div [ class "card-block" ] [ pre [] [ text model.evalResult ] ]
        ]<commit_msg>Add compiling state<commit_after>module Main exposing (main)

import Html exposing (Html, a, button, div, form, h2, nav, option, pre, select, text, textarea)
import Html.App
import Html.Attributes exposing (class, href, name, rows, selected)
import Html.Events exposing (onClick, onInput)
import Http
import Json.Decode as Json exposing ((:=))
import List exposing ((::))
import List.Extra as List
import Task exposing (Task)


-- MODEL


type alias Urls =
    { examples : String
    , eval : String
    }


type alias Example =
    { name : String
    , src : String
    }


type alias Model =
    { urls : Urls
    , examples : List Example
    , selectedExample : Maybe String
    , src : String
    , evalResult : String
    }


init : ( Model, Cmd Msg )
init =
    let
        model =
            { urls =
                { examples = "examples"
                , eval = "eval"
                }
            , examples = []
            , selectedExample = Nothing
            , src = ""
            , evalResult = ""
            }
    in
        ( model, getExamples model )


initExamples : List Example -> Model -> Model
initExamples examples model =
    case List.head examples of
        Just example ->
            setExample example.name { model | examples = examples }

        Nothing ->
            model


getExample : String -> Model -> Maybe String
getExample name model =
    model.examples
        |> List.find (\example -> example.name == name)
        |> Maybe.map (.src)


setExample : String -> Model -> Model
setExample name model =
    let
        ( selectedExample, src ) =
            case getExample name model of
                Just src ->
                    ( Just name, src )

                Nothing ->
                    ( Nothing, model.src )
    in
        { model | selectedExample = selectedExample, src = src }


setSource : String -> Model -> Model
setSource src model =
    { model | src = src, selectedExample = Nothing }



-- MESSAGES


type Msg
    = EvalRequested
    | EvalSucceed String
    | EvalFail Http.Error
    | ExamplesSucceed (List Example)
    | ExamplesFail Http.Error
    | SelectExample String
    | EditSource String



-- UPDATE


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        EvalRequested ->
            -- TODO: Disable the eval button
            ( { model | evalResult = "Compiling..." }, postEval model )

        EvalSucceed result ->
            ( { model | evalResult = result }, Cmd.none )

        EvalFail _ ->
            ( { model | evalResult = "HTTP failure" }, Cmd.none )

        ExamplesSucceed examples ->
            ( initExamples examples model, Cmd.none )

        ExamplesFail _ ->
            ( model, Cmd.none )

        SelectExample name ->
            ( setExample name model, Cmd.none )

        EditSource src ->
            ( setSource src model, Cmd.none )



-- VIEW


exampleSelect : Model -> Html Msg
exampleSelect model =
    let
        selectedAttr key =
            selected (model.selectedExample == key)

        exampleOption example =
            option [ name example.name, selectedAttr (Just example.name) ]
                [ text example.name ]

        defaultOption =
            option [ selectedAttr Nothing ] [ text "Select example…" ]
    in
        div [ class "form pull-xs-right" ]
            [ select [ class "form-control custom-select", onInput SelectExample ]
                (defaultOption :: List.map exampleOption model.examples)
            ]


editor : Model -> Html Msg
editor model =
    textarea [ class "editor form-control", rows 25, onInput EditSource ]
        [ text model.src ]


evalResult : Model -> Html Msg
evalResult model =
    div [ class "card" ]
        [ div [ class "card-header" ]
            [ nav [ class "nav" ]
                [ text "Result"
                , button [ class "btn btn-primary pull-xs-right", onClick EvalRequested ]
                    [ text "Eval" ]
                ]
            ]
        , div [ class "card-block" ] [ pre [] [ text model.evalResult ] ]
        ]
{'fraction_non_alphanumeric': 0.09501246882793017, 'fraction_numerical': 0.0007481296758104738, 'mean_word_length': 1.7125464998309097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, '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': '3471314', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2099, 'n_words': 686}