Skip to content

add getInputChar' which reads any character not just printable #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions System/Console/Haskeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module System.Console.Haskeline(
getInputLine,
getInputLineWithInitial,
getInputChar,
getInputChar',
getPassword,
waitForAnyKey,
-- ** Outputting text
Expand Down Expand Up @@ -232,6 +233,20 @@ acceptOneChar = choiceCmd [useChar $ \c s -> change (insertChar c) s
keyCommand acceptOneChar
, ctrlChar 'd' +> failCmd]

{- | Reads one character of input, including non-printable.
-}
getInputChar' :: (MonadIO m, MonadMask m) => String -- ^ The input prompt
-> InputT m (Maybe Char)
getInputChar' = promptedInput getInputCmdChar' $
runMaybeT . getLocaleChar

getInputCmdChar' :: (MonadIO m, MonadMask m) => TermOps -> Prefix -> InputT m (Maybe Char)
getInputCmdChar' tops prefix = runInputCmdT tops
$ runCommandLoop tops prefix acceptOneChar' emptyIM

acceptOneChar' :: Monad m => KeyCommand m InsertMode (Maybe Char)
acceptOneChar' = useChar' $ \c _s -> return (Just c)

----------
{- | Waits for one key to be pressed, then returns. Ignores the value
of the specific key.
Expand Down
7 changes: 7 additions & 0 deletions System/Console/Haskeline/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module System.Console.Haskeline.Command(
changeFromChar,
(+>),
useChar,
useChar',
choiceCmd,
keyChoiceCmd,
keyChoiceCmdM,
Expand Down Expand Up @@ -97,6 +98,12 @@ useChar act = KeyMap $ \k -> case k of
-> Just $ Consumed (act c)
_ -> Nothing

useChar' :: (Char -> Command m s t) -> KeyCommand m s t
useChar' act = KeyMap $ \k -> case k of
Key _m (KeyChar c)
-> Just $ Consumed (act c)
_ -> Nothing

withoutConsuming :: Command m s t -> KeyCommand m s t
withoutConsuming = KeyMap . const . Just . NotConsumed

Expand Down