<commit_before>port module Main exposing (..)

import Html exposing (Html, Attribute, div, text, ul, li, button, header, input)
import Html.Attributes exposing (disabled, checked, type')
import Html.Events exposing (onClick)
import Html.App as App
import Http
import Time exposing (Time, every)
import WebSocket
import Css exposing ((#))
import Css.Elements
import ParseInt exposing (toHex)
import CssCommon
import DebuggerCommand exposing (DebuggerCommand(Break))
import CpuSnapshot
import Instruction
import ToggleBreakpoint
import Continue
import Registers
import Step
import Colors


{ id, class, classList } =
    CssCommon.helpers



--


wsDebuggerEndpoint : String
wsDebuggerEndpoint =
    "ws://localhost:9976"


main : Program Never
main =
    App.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }



-- MODEL


type alias Model =
    { messages : List String
    , instructions : List Instruction.Model
    , registers : Registers.Model
    , autoStepEnabled : Bool
    , autoStepInterval : Float
    }


init : ( Model, Cmd AppMessage )
init =
    ( { messages = [ "Welcome to the rs-nes debugger!" ], instructions = [], registers = Registers.new, autoStepEnabled = False, autoStepInterval = Time.inMilliseconds 20 }, Cmd.none )



-- UPDATE


type AppMessage
    = DebuggerCommandReceiveSuccess DebuggerCommand
    | DebuggerCommandReceiveFail String
    | SetBreakpointClick Int
    | SetBreakpointRequestSuccess ToggleBreakpoint.Model
    | SetBreakpointRequestFail Http.Error
    | StepClick
    | StepRequestSuccess Step.Model
    | StepRequestFail Http.Error
    | ContinueClick
    | ContinueRequestSuccess Continue.Model
    | ContinueRequestFail Http.Error
    | ToggleAutoStep
    | AutoStepTick Time
    | NoOp


update : AppMessage -> Model -> ( Model, Cmd AppMessage )
update msg model =
    case msg of
        ToggleAutoStep ->
            ( { model | autoStepEnabled = not model.autoStepEnabled }, Cmd.none )

        AutoStepTick time ->
            ( model, Step.request StepRequestFail StepRequestSuccess )

        DebuggerCommandReceiveSuccess cmd ->
            handleDebuggerCommand model cmd

        DebuggerCommandReceiveFail msg ->
            ( { model | messages = (("Unable to receive debugger command: " ++ msg) :: model.messages) }, Cmd.none )

        SetBreakpointClick address ->
            ( model, ToggleBreakpoint.request address SetBreakpointRequestFail SetBreakpointRequestSuccess )

        SetBreakpointRequestSuccess resp ->
            ( { model | messages = (("Breakpoint set at " ++ toString resp.address) :: model.messages) }, Cmd.none )

        SetBreakpointRequestFail err ->
            ( { model | messages = (("Set breakpoint fail: " ++ toString err) :: model.messages) }, Cmd.none )

        StepClick ->
            ( model, Step.request StepRequestFail StepRequestSuccess )

        StepRequestSuccess resp ->
            ( model, Cmd.none )

        StepRequestFail err ->
            ( { model | messages = (("Step request fail: " ++ toString err) :: model.messages) }, Cmd.none )

        ContinueClick ->
            ( { model | autoStepEnabled = False }, Continue.request ContinueRequestFail ContinueRequestSuccess )

        ContinueRequestSuccess resp ->
            ( { model | messages = ("Continued!" :: model.messages) }, Cmd.none )

        ContinueRequestFail err ->
            ( { model | messages = (("Continue request fail: " ++ toString err) :: model.messages) }, Cmd.none )

        NoOp ->
            ( model, Cmd.none )


handleDebuggerCommand : Model -> DebuggerCommand -> ( Model, Cmd AppMessage )
handleDebuggerCommand model cmd =
    case cmd of
        Break snapshot ->
            let
                pc =
                    snapshot.registers.pc
            in
                ( { model | messages = (("Breaking @ 0x" ++ toHex pc) :: model.messages), instructions = snapshot.instructions, registers = snapshot.registers }, Cmd.none )


subscriptions : Model -> Sub AppMessage
subscriptions model =
    let
        autoStepSubscription =
            if model.autoStepEnabled then
                Time.every model.autoStepInterval AutoStepTick
            else
                Sub.none
    in
        Sub.batch
            [ WebSocket.listen wsDebuggerEndpoint <| DebuggerCommand.decode DebuggerCommandReceiveFail DebuggerCommandReceiveSuccess
            , autoStepSubscription
            ]

<commit_msg>Auto step tweaks<commit_after>port module Main exposing (..)

import Html exposing (Html, Attribute, div, text, ul, li, button, header, input)
import Html.Attributes exposing (disabled, checked, type')
import Html.Events exposing (onClick)
import Html.App as App
import Http
import Time exposing (Time, every)
import WebSocket
import Css exposing ((#))
import Css.Elements
import ParseInt exposing (toHex)
import CssCommon
import DebuggerCommand exposing (DebuggerCommand(Break))
import CpuSnapshot
import Instruction
import ToggleBreakpoint
import Continue
import Registers
import Step
import Colors


{ id, class, classList } =
    CssCommon.helpers



--


wsDebuggerEndpoint : String
wsDebuggerEndpoint =
    "ws://localhost:9976"


main : Program Never
main =
    App.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }



-- MODEL


type alias Model =
    { messages : List String
    , instructions : List Instruction.Model
    , registers : Registers.Model
    , autoStepEnabled : Bool
    , autoStepInterval : Float
    , stepRequestPending : Bool
    }


init : ( Model, Cmd AppMessage )
init =
    let
        model =
            { messages = [ "Welcome to the rs-nes debugger!" ]
            , instructions = []
            , registers = Registers.new
            , autoStepEnabled = False
            , autoStepInterval = Time.inMilliseconds 100
            , stepRequestPending = False
            }
    in
        ( model, Cmd.none )



-- UPDATE


type AppMessage
    = DebuggerCommandReceiveSuccess DebuggerCommand
    | DebuggerCommandReceiveFail String
    | SetBreakpointClick Int
    | SetBreakpointRequestSuccess ToggleBreakpoint.Model
    | SetBreakpointRequestFail Http.Error
    | StepClick
    | StepRequestSuccess Step.Model
    | StepRequestFail Http.Error
    | ContinueClick
    | ContinueRequestSuccess Continue.Model
    | ContinueRequestFail Http.Error
    | ToggleAutoStep
    | AutoStepTick Time
    | NoOp


update : AppMessage -> Model -> ( Model, Cmd AppMessage )
update msg model =
    case msg of
        ToggleAutoStep ->
            ( { model | autoStepEnabled = not model.autoStepEnabled }, Cmd.none )

        AutoStepTick time ->
            stepRequest model

        DebuggerCommandReceiveSuccess cmd ->
            handleDebuggerCommand model cmd

        DebuggerCommandReceiveFail msg ->
            ( { model | messages = (("Unable to receive debugger command: " ++ msg) :: model.messages) }, Cmd.none )

        SetBreakpointClick address ->
            ( model, ToggleBreakpoint.request address SetBreakpointRequestFail SetBreakpointRequestSuccess )

        SetBreakpointRequestSuccess resp ->
            ( { model | messages = (("Breakpoint set at " ++ toString resp.address) :: model.messages) }, Cmd.none )

        SetBreakpointRequestFail err ->
            ( { model | messages = (("Set breakpoint fail: " ++ toString err) :: model.messages) }, Cmd.none )

        StepClick ->
            stepRequest model

        StepRequestSuccess resp ->
            ( { model | stepRequestPending = False }, Cmd.none )

        StepRequestFail err ->
            ( { model | autoStepEnabled = False, messages = (("Step request fail: " ++ toString err) :: model.messages) }, Cmd.none )

        ContinueClick ->
            ( { model | autoStepEnabled = False }, Continue.request ContinueRequestFail ContinueRequestSuccess )

        ContinueRequestSuccess resp ->
            ( { model | messages = ("Continued!" :: model.messages) }, Cmd.none )

        ContinueRequestFail err ->
            ( { model | messages = (("Continue request fail: " ++ toString err) :: model.messages) }, Cmd.none )

        NoOp ->
            ( model, Cmd.none )


stepRequest : Model -> (Model, Cmd AppMessage)
stepRequest model =
    if model.stepRequestPending then
        ( model, Cmd.none )
    else
        ( { model | stepRequestPending = True }, Step.request StepRequestFail StepRequestSuccess )


handleDebuggerCommand : Model -> DebuggerCommand -> ( Model, Cmd AppMessage )
handleDebuggerCommand model cmd =
    case cmd of
        Break snapshot ->
            let
                pc =
                    snapshot.registers.pc
            in
                ( { model | messages = (("Breaking @ 0x" ++ toHex pc) :: model.messages), instructions = snapshot.instructions, registers = snapshot.registers }, Cmd.none )


subscriptions : Model -> Sub AppMessage
subscriptions model =
    let
        autoStepSubscription =
            if model.autoStepEnabled then
                Time.every model.autoStepInterval AutoStepTick
            else
                Sub.none
    in
        Sub.batch
            [ WebSocket.listen wsDebuggerEndpoint <| DebuggerCommand.decode DebuggerCommandReceiveFail DebuggerCommandReceiveSuccess
            , autoStepSubscription
            ]


{'fraction_non_alphanumeric': 0.08457764505119454, 'fraction_numerical': 0.0015998293515358361, 'mean_word_length': 2.4742497221193034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '6290350', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2374, 'n_words': 677}