index->string allows printing keystrokes readably

This commit is contained in:
2023-01-01 15:08:45 +00:00
parent ad2628ddfc
commit c3e9c14186
2 changed files with 27 additions and 1 deletions
+21 -1
View File
@@ -1,5 +1,6 @@
(local { : Gdk } (require :lgi))
(local { : view } (require :fennel))
(local lume (require :lume))
(local modifier-keyvals
;; we need to detect and discard modifier-only key events when
@@ -51,6 +52,25 @@
(bor m (. Gdk.ModifierType k)))]
(spec->index {:keyval event.keyval : modmask})))
(fn compact [xs]
(icollect [_ v (ipairs xs)] v))
(fn index->string [index]
(let [Mod Gdk.ModifierType
[keyval modmask] (lume.map (lume.split index ":") tonumber)
chars []]
(if (> (band modmask Mod.CONTROL_MASK) 0) (table.insert chars "C"))
(if (> (band modmask Mod.MOD1_MASK) 0) (table.insert chars "M"))
(if (> (band modmask Mod.MOD4_MASK) 0) (table.insert chars "S"))
(table.insert chars (Gdk.keyval_name keyval))
(table.concat chars "-")))
(let [v (index->string "103:0")] (assert (= v "g") v))
(let [v (index->string "65:0")] (assert (= v "A") v))
(let [v (index->string "120:4")] (assert (= v "C-x") v))
(let [v (index->string "100:8")] (assert (= v "M-d") v))
(let [v (index->string "100:12")] (assert (= v "C-M-d") v))
(fn command? [tbl]
;; a keymap entry has a string as key, a command
;; definition is a numerically-indexed array
@@ -92,7 +112,7 @@
_
(do
(set m keymap)
(values nil (.. "No binding for " (view e) " ")))))))
(values nil (.. "No binding for " (index->string c) " ")))))))
}))