Something went wrong. Try again.
Making a new TUI editor maybe
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162module Glyph.Key.Count where
----------------------------------------------------------------------------------------------------
import qualified Data.Char as Charimport qualified Data.List as List
import Glyph.Class.Stringable ( Stringable (..) )import Glyph.Key.Chord ( Chord (..) )
import Graphics.Vty.Input.Events ( Key (..) )
----------------------------------------------------------------------------------------------------
data Count -- Nada = NoCount -- Digits are being collected, newest last. | Reading (List Char) -- Digits finished and folded into a number. | Counted Natural deriving (Eq, Show)
instance Stringable Count where toString NoCount = "" toString (Reading chars) = chars toString (Counted num) = show num
-- One way fromString _ = Nothing
----------------------------------------------------------------------------------------------------
canCount :: Count -> List Chord -> Key -> BoolcanCount count [] (KChar c) = case count of NoCount -> Char.isDigit c Reading _ -> Char.isDigit c Counted _ -> FalsecanCount _ _ _ = False
pushDigit :: Key -> Count -> CountpushDigit (KChar c) count = case count of NoCount -> Reading [c] Reading digits -> Reading (digits <> [c]) Counted _ -> Reading [c]pushDigit _ count = Counted <| settle count
settle :: Count -> Naturalsettle = \case NoCount -> 1 Reading digits -> List.foldl' (\accum char -> accum * 10 + digitValue char) 0 digits Counted value -> value
digitValue :: Char -> NaturaldigitValue char | Char.isDigit char = Char.ord char - Char.ord '0' |> toEnum | otherwise = 0