diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-05 12:32:24 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-05 13:40:47 +0530 |
| commit | a4144c0c6e0d3df3740c70b9ad947642d9d48ac6 (patch) | |
| tree | 9fc8315aeb5d5ebf867c804d17920216c5396ab1 /lib/Daffm/Keymap.hs | |
| parent | 556f890c3bb799dd6bc1e83c5dcff12f25ed5c24 (diff) | |
| download | daffm-a4144c0c6e0d3df3740c70b9ad947642d9d48ac6.tar.gz daffm-a4144c0c6e0d3df3740c70b9ad947642d9d48ac6.zip | |
Show keyseq,selections in statusline + minor refactor
Diffstat (limited to 'lib/Daffm/Keymap.hs')
| -rw-r--r-- | lib/Daffm/Keymap.hs | 47 |
1 files changed, 47 insertions, 0 deletions
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 ' ') = "<space>" +showKey (V.KChar '\t') = "<tab>" +showKey (V.KChar c) = Text.singleton c +showKey V.KEnter = "<cr>" +showKey V.KEsc = "<esc>" +showKey V.KBS = "<bs>" +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 "<tab>" -> (Just rest')) -> parse rest' $ keys <> [V.KChar '\t'] + (Text.stripPrefix "<space>" -> (Just rest')) -> parse rest' $ keys <> [V.KChar ' '] + (Text.stripPrefix "<bs>" -> (Just rest')) -> parse rest' $ keys <> [V.KBS] + (Text.stripPrefix "<cr>" -> (Just rest')) -> parse rest' $ keys <> [V.KEnter] + (Text.stripPrefix "<esc>" -> (Just rest')) -> parse rest' $ keys <> [V.KEsc] + (Text.splitAt 1 -> (c, rest)) -> do + ch <- fst <$> Text.uncons c + parse rest $ keys <> [V.KChar ch] |
