<commit_before>module Scorched.View.Widget.NumericField where

import Signal (Message, send)
import Text (fromString, leftAligned, monospace)

import Graphics.Collage (Form, collage, toForm, moveX, move)
import Graphics.Element (Element, widthOf)

import Graphics.Input (customButton)

import Scorched.Action (Action(NoOp), updates)

import Scorched.View.Widget.BorderTriangle as BorderTriangle

type Operation = Increment | Decrement

type alias Settings = {
  messenger: (Int -> Message),
  text: String,
  value: Int,
  min: Int,
  max: Int,
  step: Int
}

defaultSettings : Settings
defaultSettings =
  { messenger = (\v -> send updates NoOp)
  , text = "Number"
  , value = 2
  , min = 2
  , max = 10
  , step = 1
  }

build : Settings -> Element
build settings =
  let
    width = 100
    labelElement = label settings.text settings.value
  in
    collage width 30
      [ increment settings |> moveX -45
      , decrement settings |> move (-45, -10)
      , labelElement |> toForm |> move (-(toFloat (width - (widthOf labelElement)) / 2) + 14, -3)
      ]

increment : Settings -> Form
increment settings = button settings BorderTriangle.Up

decrement : Settings -> Form
decrement settings = button settings BorderTriangle.Down

button : Settings -> BorderTriangle.Direction -> Form
button ({value,messenger} as settings) direction =
  let
    btn = collage 15 15 [BorderTriangle.build 7 False direction]
    btnPressed = collage 15 15 [BorderTriangle.build 7 True direction]

    operation = if direction == BorderTriangle.Up then Increment else Decrement
  in
    toForm (customButton (messenger (guard settings operation)) btn btn btnPressed)

label : String -> Int -> Element
label text value =
  let
    formattedText = monospace (fromString (text ++ ": " ++ (toString value)))
  in
    leftAligned formattedText

guard : Settings -> Operation -> Int
guard {value,min,max,step} operation =
  clamp min max (new operation value step)

new : Operation -> Int -> Int -> Int
new operation current step =
  case operation of
    Increment -> current + step
    Decrement -> current - step<commit_msg>use justify function for clarity<commit_after>module Scorched.View.Widget.NumericField where

import Signal (Message, send)
import Text (fromString, justified, monospace)

import Graphics.Collage (Form, collage, toForm, moveX, move)
import Graphics.Element (Element, widthOf)

import Graphics.Input (customButton)

import Scorched.Action (Action(NoOp), updates)

import Scorched.View.Widget.BorderTriangle as BorderTriangle

type Operation = Increment | Decrement

type alias Settings = {
  messenger: (Int -> Message),
  text: String,
  value: Int,
  min: Int,
  max: Int,
  step: Int
}

defaultSettings : Settings
defaultSettings =
  { messenger = (\v -> send updates NoOp)
  , text = "Number"
  , value = 2
  , min = 2
  , max = 10
  , step = 1
  }

build : Settings -> Element
build settings =
  let
    width = 100
    labelElement = label settings.text settings.value
  in
    collage width 30
      [ increment settings |> moveX -45
      , decrement settings |> move (-45, -10)
      , labelElement |> toForm |> move (-(toFloat (width - (widthOf labelElement)) / 2) + 14, -3)
      ]

increment : Settings -> Form
increment settings = button settings BorderTriangle.Up

decrement : Settings -> Form
decrement settings = button settings BorderTriangle.Down

button : Settings -> BorderTriangle.Direction -> Form
button ({value,messenger} as settings) direction =
  let
    btn = collage 15 15 [BorderTriangle.build 7 False direction]
    btnPressed = collage 15 15 [BorderTriangle.build 7 True direction]

    operation = if direction == BorderTriangle.Up then Increment else Decrement
  in
    toForm (customButton (messenger (guard settings operation)) btn btn btnPressed)

label : String -> Int -> Element
label text value =
  let
    formattedText = monospace (fromString (text ++ ": " ++ (toString value)))
  in
    justified formattedText

guard : Settings -> Operation -> Int
guard {value,min,max,step} operation =
  clamp min max (new operation value step)

new : Operation -> Int -> Int -> Int
new operation current step =
  case operation of
    Increment -> current + step
    Decrement -> current - step
{'fraction_non_alphanumeric': 0.09694963348309293, 'fraction_numerical': 0.014187751241428233, 'mean_word_length': 3.592833876221498, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 43, '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': '1770275', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1219, 'n_words': 435}