keybindingHelpWidget: do not require input to come with a section heading

This commit is contained in:
Jonathan Daugherty 2022-08-02 19:40:23 -07:00
parent a359dc0d82
commit ccad8abb27
2 changed files with 26 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import Lens.Micro ((^.))
import Lens.Micro.TH (makeLenses)
import Lens.Micro.Mtl ((<~), (.=), (%=), use)
import Control.Monad (void)
import qualified Data.Text.IO as Text
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
@ -76,7 +77,7 @@ drawUi st = [body]
return $ K.Binding { K.kbKey = k
, K.kbMods = mods
}
keybindingHelp = K.keybindingHelpWidget (st^.keyConfig) txt ("Keybinding Help", handlers)
keybindingHelp = K.keybindingHelpWidget (st^.keyConfig) handlers
status = hLimit 40 $
padRight Max $
vBox [ txt $ "Last key: " <> K.ppMaybeBinding binding
@ -85,7 +86,7 @@ drawUi st = [body]
]
body = C.center $
(padRight (Pad 7) $ B.borderWithLabel (txt "Status") status) <+>
B.border keybindingHelp
B.borderWithLabel (txt "Keybinding Help") keybindingHelp
app :: M.App St e ()
app =
@ -104,3 +105,11 @@ main = do
, _lastKeyHandled = False
, _counter = 0
}
let sections = [("Main", handlers)]
putStrLn "Generated plain text help:"
Text.putStrLn $ K.keybindingTextTable kc sections
putStrLn "Generated Markdown help:"
Text.putStrLn $ K.keybindingMarkdownTable kc sections

View File

@ -38,11 +38,13 @@ keybindingMarkdownTable :: (Ord e)
keybindingMarkdownTable kc sections = title <> keybindSectionStrings
where title = "# Keybindings\n"
keybindSectionStrings = T.concat $ sectionText <$> sections
sectionText = mkKeybindEventSectionHelp kc keybindEventHelpMarkdown T.unlines mkHeading
sectionText (heading, handlers) =
mkHeading heading <>
mkKeybindEventSectionHelp kc keybindEventHelpMarkdown T.unlines handlers
mkHeading n =
"\n# " <> n <>
"\n| Keybinding | Event Name | Description |" <>
"\n| ---------- | ---------- | ----------- |"
"\n| ---------- | ---------- | ----------- |\n"
-- | Generate a plain text document of sections indicating the key
-- binding state for each event handler.
@ -55,12 +57,15 @@ keybindingTextTable :: (Ord e)
keybindingTextTable kc sections = title <> keybindSectionStrings
where title = "Keybindings\n===========\n"
keybindSectionStrings = T.concat $ sectionText <$> sections
sectionText = mkKeybindEventSectionHelp kc (keybindEventHelpText keybindingWidth eventNameWidth) T.unlines mkHeading
sectionText (heading, handlers) =
mkHeading heading <>
mkKeybindEventSectionHelp kc (keybindEventHelpText keybindingWidth eventNameWidth) T.unlines handlers
keybindingWidth = 15
eventNameWidth = 30
mkHeading n =
"\n" <> n <>
"\n" <> (T.replicate (T.length n) "=")
"\n" <> (T.replicate (T.length n) "=") <>
"\n"
keybindEventHelpText :: Int -> Int -> (TextHunk, T.Text, [TextHunk]) -> T.Text
keybindEventHelpText width eventNameWidth (evName, desc, evs) =
@ -77,12 +82,10 @@ mkKeybindEventSectionHelp :: (Ord e)
=> KeyConfig e
-> ((TextHunk, T.Text, [TextHunk]) -> a)
-> ([a] -> a)
-> (T.Text -> a)
-> (T.Text, [KeyEventHandler e m])
-> [KeyEventHandler e m]
-> a
mkKeybindEventSectionHelp kc mkKeybindHelpFunc vertCat mkHeading (sectionName, kbs) =
vertCat $ (mkHeading sectionName) :
(mkKeybindHelpFunc <$> (mkKeybindEventHelp kc <$> kbs))
mkKeybindEventSectionHelp kc mkKeybindHelpFunc vertCat kbs =
vertCat $ mkKeybindHelpFunc <$> (mkKeybindEventHelp kc <$> kbs)
keybindEventHelpMarkdown :: (TextHunk, T.Text, [TextHunk]) -> T.Text
keybindEventHelpMarkdown (evName, desc, evs) =
@ -128,11 +131,9 @@ mkKeybindEventHelp kc h =
keybindingHelpWidget :: (Ord e)
=> KeyConfig e
-- ^ The key binding configuration in use.
-> (T.Text -> Widget n)
-- ^ A function to render a section heading.
-> (T.Text, [KeyEventHandler e m])
-- ^ The name of the section and the list of the
-- event handlers.
-> [KeyEventHandler e m]
-- ^ The list of the event handlers to include in
-- the help display.
-> Widget n
keybindingHelpWidget kc =
mkKeybindEventSectionHelp kc keybindEventHelpWidget vBox