From a4144c0c6e0d3df3740c70b9ad947642d9d48ac6 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 5 Oct 2025 12:32:24 +0530 Subject: Show keyseq,selections in statusline + minor refactor --- lib/Daffm/Keymap.hs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/Daffm/Keymap.hs (limited to 'lib/Daffm/Keymap.hs') diff --git a/lib/Daffm/Keymap.hs b/lib/Daffm/Keymap.hs new file mode 100644 index 0000000..aae4a95 --- /dev/null +++ b/lib/Daffm/Keymap.hs @@ -0,0 +1,47 @@ +module Daffm.Keymap where + +import Daffm.Types +import qualified Data.Map as Map +import Data.Maybe (fromMaybe) +import qualified Data.Text as Text +import qualified Graphics.Vty as V + +showKeySequence :: KeySequence -> Text.Text +showKeySequence = Text.intercalate "" . map showKey + +-- TODO: More chars +showKey :: Key -> Text.Text +showKey (V.KChar ' ') = "" +showKey (V.KChar '\t') = "" +showKey (V.KChar c) = Text.singleton c +showKey V.KEnter = "" +showKey V.KEsc = "" +showKey V.KBS = "" +showKey _ = "" + +matchKeySequence :: Keymap -> KeySequence -> KeyMatchResult +matchKeySequence keymaps keys + | Map.member keys keymaps = + MatchSuccess . fromMaybe CmdNoop $ Map.lookup keys keymaps + | otherwise = partial keymaps keys + where + partial _ [] = MatchFailure + partial (Map.null -> True) _ = MatchFailure + partial keymaps' keys' = if hasMatch then MatchPartial else MatchFailure + where + hasMatch = any (startsWith keys' . fst) (Map.toList keymaps') + startsWith ls1 ls2 = ls1 == take (length ls1) ls2 + +parseKeySequence :: Text.Text -> Maybe [Key] +parseKeySequence keytxt = parse keytxt [] + where + parse k keys = case k of + (Text.null -> True) -> pure keys + (Text.stripPrefix "" -> (Just rest')) -> parse rest' $ keys <> [V.KChar '\t'] + (Text.stripPrefix "" -> (Just rest')) -> parse rest' $ keys <> [V.KChar ' '] + (Text.stripPrefix "" -> (Just rest')) -> parse rest' $ keys <> [V.KBS] + (Text.stripPrefix "" -> (Just rest')) -> parse rest' $ keys <> [V.KEnter] + (Text.stripPrefix "" -> (Just rest')) -> parse rest' $ keys <> [V.KEsc] + (Text.splitAt 1 -> (c, rest)) -> do + ch <- fst <$> Text.uncons c + parse rest $ keys <> [V.KChar ch] -- cgit v1.3.1