module Glyph.Key.Count where ---------------------------------------------------------------------------------------------------- import qualified Data.Char as Char import 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 -> Bool canCount count [] (KChar c) = case count of NoCount -> Char.isDigit c Reading _ -> Char.isDigit c Counted _ -> False canCount _ _ _ = False pushDigit :: Key -> Count -> Count pushDigit (KChar c) count = case count of NoCount -> Reading [c] Reading digits -> Reading (digits <> [c]) Counted _ -> Reading [c] pushDigit _ count = Counted <| settle count settle :: Count -> Natural settle = \case NoCount -> 1 Reading digits -> List.foldl' (\accum char -> accum * 10 + digitValue char) 0 digits Counted value -> value digitValue :: Char -> Natural digitValue char | Char.isDigit char = Char.ord char - Char.ord '0' |> toEnum | otherwise = 0