<commit_before>module Panel exposing (..)

import Html exposing (Html, div, text, span)
--import Html.Events exposing ()
import Html.Attributes exposing (class)
import Char exposing (..)
import String exposing (..)
import Debug exposing (..)
import Keyboard
import List exposing (..)

-- MODEL

type alias Model =
    -- split buffer into String array
    { lines: List String
    }

initialModel : Model
initialModel =
    { lines =
          [ "bello world"
          , "other stuff"
          ]
    }

-- MESSAGES
-- key pressed
-- shortcuts etc
type Msg
    = KeyPressedMsg Keyboard.KeyCode
    | KeyDownMsg Keyboard.KeyCode


-- VIEW

renderLine : String -> Html a
renderLine l =
    div [ ]
        [ span [ class "line" ] [ text l ] ]

view : Model -> Html Msg
view model =
    div [ ] (List.map renderLine model.lines)

-- UPDATE

getLast : List a -> List a
getLast list =
    case list of
        [] -> []
        l ->
            case head (List.reverse l) of
                Nothing -> []
                Just a -> [a]

removeLast : List a -> List a
removeLast list =
    case list of
        [] -> []
        l ->
            List.reverse (drop 1 (List.reverse l))

update : Msg -> Model -> (Model, Cmd Msg)
update message model =
    case message of
        KeyDownMsg 8 ->
            case model.lines of
                [] ->
                    ( model, Cmd.none )
                lines ->
                    let
                        last =
                            case head (getLast lines) of
                                Nothing -> []
                                Just a ->
                                    case dropRight 1 a of
                                        "" -> []
                                        a -> [a]

                        lastLess = removeLast lines
                    in
                        ( { model | lines = lastLess ++ last }, Cmd.none )

        KeyDownMsg 13 ->
            ( { model | lines = model.lines ++ [""] }, Cmd.none )
        KeyPressedMsg keyCode ->
            let
                string = keyToString keyCode
                last =
                    case head (getLast model.lines) of
                        Nothing -> ""
                        Just a -> a
                lastLess = removeLast model.lines
            in
                ( { model | lines = lastLess ++ [(last ++ string)] }, Cmd.none )

        _ ->
            ( model, Cmd.none )


-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Keyboard.presses KeyPressedMsg
        , Keyboard.downs KeyDownMsg
        ]

keyToString : Keyboard.KeyCode -> String
keyToString keyCode =
    fromChar ( fromCode keyCode )


<commit_msg>better remove last line structure<commit_after>module Panel exposing (..)

import Html exposing (Html, div, text, span)
--import Html.Events exposing ()
import Html.Attributes exposing (class)
import Char exposing (..)
import String exposing (..)
import Debug exposing (..)
import Keyboard
import List exposing (..)

-- MODEL

type alias Model =
    -- split buffer into String array
    { lines: List String
    }

initialModel : Model
initialModel =
    { lines =
          [ "bello world"
          , "other stuff"
          ]
    }

-- MESSAGES
-- key pressed
-- shortcuts etc
type Msg
    = KeyPressedMsg Keyboard.KeyCode
    | KeyDownMsg Keyboard.KeyCode


-- VIEW

renderLine : String -> Html a
renderLine l =
    div [ ]
        [ span [ class "line" ] [ text l ] ]

view : Model -> Html Msg
view model =
    div [ ] (List.map renderLine model.lines)

-- UPDATE

getLast : List a -> List a
getLast list =
    case list of
        [] -> []
        l ->
            case head (List.reverse l) of
                Nothing -> []
                Just a -> [a]

removeLast : List a -> List a
removeLast list =
    case list of
        [] -> []
        l ->
            List.reverse (drop 1 (List.reverse l))

update : Msg -> Model -> (Model, Cmd Msg)
update message model =
    case message of
        KeyDownMsg 8 ->
            case model.lines of
                [] ->
                    ( model, Cmd.none )
                lines ->
                    let
                        last =
                            case head (getLast lines) of
                                Nothing -> []
                                Just a ->
                                    case log "last" dropRight 1 a of
                                        "" -> []
                                        a -> [a]

                        lastLess = removeLast lines
                    in
                        ( { model | lines = lastLess ++ last }, Cmd.none )

        KeyDownMsg 13 ->
            ( { model | lines = model.lines ++ [""] }, Cmd.none )
        KeyPressedMsg keyCode ->
            let
                string = keyToString keyCode
                last =
                    case head (getLast model.lines) of
                        Nothing -> ""
                        Just a -> a
                lastLess = removeLast model.lines
            in
                ( { model | lines = lastLess ++ [(last ++ string)] }, Cmd.none )

        _ ->
            ( model, Cmd.none )


-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Keyboard.presses KeyPressedMsg
        , Keyboard.downs KeyDownMsg
        ]

keyToString : Keyboard.KeyCode -> String
keyToString keyCode =
    fromChar ( fromCode keyCode )



{'fraction_non_alphanumeric': 0.0950646512474959, 'fraction_numerical': 0.001821161901293025, 'mean_word_length': 1.1976790716286514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '292738', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1403, 'n_words': 450}