module Glyph.Key.Edge where ---------------------------------------------------------------------------------------------------- import qualified Data.List as List import Glyph.Class.Stringable ( Stringable (..) ) import Graphics.Vty.Input.Events ( Key (..) ) ---------------------------------------------------------------------------------------------------- data CaptureType = CaptureChar | CaptureNum | CaptureUntil Key deriving (Eq, Ord, Show) instance Stringable CaptureType where toString CaptureChar = "{char}" toString CaptureNum = "{number}" toString (CaptureUntil key) = "{until " <> toString key <> "}" fromString "{char}" = Just CaptureChar fromString "{number}" = Just CaptureNum fromString str = case str of _ | (List.take 7 str == "{until ") `and` ((List.reverse str |> List.take 1) == "}") -> List.drop 7 str |> List.reverse |> List.drop 1 |> List.reverse |> fromString @Key |> (<$>) CaptureUntil | otherwise -> Nothing ---------------------------------------------------------------------------------------------------- data Edge a = Literal a | Capture CaptureType deriving (Eq, Ord, Show) instance forall a. (Stringable a) => Stringable (Edge a) where toString (Literal a) = toString a toString (Capture cap) = toString cap fromString str = case str of -- Required, to separate "{" from Captures "{" -> fromString @a "{" <&> Literal '{' : _ -> fromString @CaptureType str <&> Capture _ -> fromString @a str <&> Literal