I'm writing a terminal-mode program in Haskell. How would I go about reading raw keypress information?
In particular, there seems to be something providing line-editing facilities on top of Haskell. If I do getLine, I seem to be able to use the up-arrow to get previous lines, edit the text, and only when I press Enter does the text become visible to the Haskell application itself.
What I'm after is the ability to read individual keypresses, so I can implement the line-editing stuff myself.
Perhaps my question was unclear. Basically I want to build something like Vi or Emacs (or Yi). I already know there are terminal bindings that will let me do fancy console-mode printing, so the output side shouldn't be an issue. I'm just looking for a way to get at raw keypress input, so I can do things like (for example) add K to the current line of text when the user presses the letter K, or save the file to disk when the user presses Ctrl+S.
This might be the simplest solution, resembling typical code in other programming languages:
import System.IO (stdin, hReady)
getKey :: IO [Char]
getKey = reverse <$> getKey' ""
where getKey' chars = do
char <- getChar
more <- hReady stdin
(if more then getKey' else return) (char:chars)
It works by reading more than one character “at a time”. Allowing E.g. the ↑ key, which consists of the three characters ['\ESC','[','A'] to be distinguished from an actual \ESC character input.
Usage example:
import System.IO (stdin, hSetEcho, hSetBuffering, NoBuffering)
import Control.Monad (when)
-- Simple menu controller
main = do
hSetBuffering stdin NoBuffering
hSetEcho stdin False
key <- getKey
when (key /= "\ESC") $ do
case key of
"\ESC[A" -> putStr "↑"
"\ESC[B" -> putStr "↓"
"\ESC[C" -> putStr "→"
"\ESC[D" -> putStr "←"
"\n" -> putStr "⎆"
"\DEL" -> putStr "⎋"
_ -> return ()
main
This is a bit hackish, since in theory, a user could input more keys before the program gets to the hReady. Which could happen if the terminal allows pasting. But in practice, for interactive input, this is not a realistic scenario.
Fun fact: The cursor strings can be putStrd to actually move the cursor programmatically.
Sounds like you want readline support. There are a couple of packages to do this, but haskeline is probably the easiest to use with the most supported platforms.
import Control.Monad.Trans
import System.Console.Haskeline
type Repl a = InputT IO a
process :: String -> IO ()
process = putStrLn
repl :: Repl ()
repl = do
minput <- getInputLine "> "
case minput of
Nothing -> outputStrLn "Goodbye."
Just input -> (liftIO $ process input) >> repl
main :: IO ()
main = runInputT defaultSettings repl
Incomplete:
After several hours of web surfing, I can report the following:
readline has a huge interface with virtually no documentation whatsoever. From the function names and type signatures you could maybe guess what this stuff does... but it's far from trivial. At any rate, this library seems to provide a high-level editing interface - which is the thing I'm trying to implement myself. I need something more low-level.
After wading through the source of haskeline, it seems it has a huge tangle low-level code, seperately for Win32 and POSIX. If there is an easy way to do console I/O, this library does not demonstrate it. The code appears to be so tightly integrated and highly specific to haskeline that I doubt I can reuse any of it. But perhaps by reading it I can learn enough to write my own?
Yi is... freaking massive. The Cabal file lists > 150 exposed modules. (!!) It appears, though, that underneath it's using a package called vty, which is POSIX-only. (I wonder how the hell Yi works on Windows then?) vty looks like it might be directly useful to me without further modification. (But again, not on Windows.)
unix has... basically nothing interesting. It has a bunch of stuff to set things on a terminal, but absolutely nothing for reading from a terminal. (Except maybe to check whether echo is on, etc. Nothing about keypresses.)
unix-compat has absolutely nothing of interest.
One option would be to use ncurses. A minimalistic example:
import Control.Monad
import UI.NCurses
main :: IO ()
main = runCurses $ do
w <- defaultWindow
forever $ do
e <- getEvent w Nothing
updateWindow w $ do
moveCursor 0 0
drawString (show e)
render
I think you are looking for hSetBuffering. StdIn is line buffered by default, but
you want to receive the keys right away.
I think the unix library provides the most lightweight solution for this, especially if you have some familiarity with termios, which is mirrored by the System.Posix.Terminal module.
There's a good page over at gnu.org that describes using termios to set up non-canonical input mode for a terminal, and you can do this with System.Posix.Terminal.
Here's my solution, which transforms a computation in IO to use non-canonical mode:
{- from unix library -}
import System.Posix.Terminal
import System.Posix.IO (fdRead, stdInput)
{- from base -}
import System.IO (hFlush, stdout)
import Control.Exception (finally, catch, IOException)
{- run an application in raw input / non-canonical mode with given
- VMIN and VTIME settings. for a description of these, see:
- http://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html
- as well as `man termios`.
-}
withRawInput :: Int -> Int -> IO a -> IO a
withRawInput vmin vtime application = do
{- retrieve current settings -}
oldTermSettings <- getTerminalAttributes stdInput
{- modify settings -}
let newTermSettings =
flip withoutMode EnableEcho . -- don't echo keystrokes
flip withoutMode ProcessInput . -- turn on non-canonical mode
flip withTime vtime . -- wait at most vtime decisecs per read
flip withMinInput vmin $ -- wait for >= vmin bytes per read
oldTermSettings
{- install new settings -}
setTerminalAttributes stdInput newTermSettings Immediately
{- restore old settings no matter what; this prevents the terminal
- from becoming borked if the application halts with an exception
-}
application
`finally` setTerminalAttributes stdInput oldTermSettings Immediately
{- sample raw input method -}
tryGetArrow = (do
(str, bytes) <- fdRead stdInput 3
case str of
"\ESC[A" -> putStrLn "\nUp"
"\ESC[B" -> putStrLn "\nDown"
"\ESC[C" -> putStrLn "\nRight"
"\ESC[D" -> putStrLn "\nLeft"
_ -> return ()
) `catch` (
{- if vmin bytes have not been read by vtime, fdRead will fail
- with an EOF exception. catch this case and do nothing.
- The type signature is necessary to allow other exceptions
- to get through.
-}
(const $ return ()) :: IOException -> IO ()
)
{- sample application -}
loop = do
tryGetArrow
putStr "." >> hFlush stdout
loop
{- run with:
- VMIN = 0 (don't wait for a fixed number of bytes)
- VTIME = 1 (wait for at most 1/10 sec before fdRead returns)
-}
main = withRawInput 0 1 $ loop
Related
I'm writing a terminal-mode program in Haskell. How would I go about reading raw keypress information?
In particular, there seems to be something providing line-editing facilities on top of Haskell. If I do getLine, I seem to be able to use the up-arrow to get previous lines, edit the text, and only when I press Enter does the text become visible to the Haskell application itself.
What I'm after is the ability to read individual keypresses, so I can implement the line-editing stuff myself.
Perhaps my question was unclear. Basically I want to build something like Vi or Emacs (or Yi). I already know there are terminal bindings that will let me do fancy console-mode printing, so the output side shouldn't be an issue. I'm just looking for a way to get at raw keypress input, so I can do things like (for example) add K to the current line of text when the user presses the letter K, or save the file to disk when the user presses Ctrl+S.
This might be the simplest solution, resembling typical code in other programming languages:
import System.IO (stdin, hReady)
getKey :: IO [Char]
getKey = reverse <$> getKey' ""
where getKey' chars = do
char <- getChar
more <- hReady stdin
(if more then getKey' else return) (char:chars)
It works by reading more than one character “at a time”. Allowing E.g. the ↑ key, which consists of the three characters ['\ESC','[','A'] to be distinguished from an actual \ESC character input.
Usage example:
import System.IO (stdin, hSetEcho, hSetBuffering, NoBuffering)
import Control.Monad (when)
-- Simple menu controller
main = do
hSetBuffering stdin NoBuffering
hSetEcho stdin False
key <- getKey
when (key /= "\ESC") $ do
case key of
"\ESC[A" -> putStr "↑"
"\ESC[B" -> putStr "↓"
"\ESC[C" -> putStr "→"
"\ESC[D" -> putStr "←"
"\n" -> putStr "⎆"
"\DEL" -> putStr "⎋"
_ -> return ()
main
This is a bit hackish, since in theory, a user could input more keys before the program gets to the hReady. Which could happen if the terminal allows pasting. But in practice, for interactive input, this is not a realistic scenario.
Fun fact: The cursor strings can be putStrd to actually move the cursor programmatically.
Sounds like you want readline support. There are a couple of packages to do this, but haskeline is probably the easiest to use with the most supported platforms.
import Control.Monad.Trans
import System.Console.Haskeline
type Repl a = InputT IO a
process :: String -> IO ()
process = putStrLn
repl :: Repl ()
repl = do
minput <- getInputLine "> "
case minput of
Nothing -> outputStrLn "Goodbye."
Just input -> (liftIO $ process input) >> repl
main :: IO ()
main = runInputT defaultSettings repl
Incomplete:
After several hours of web surfing, I can report the following:
readline has a huge interface with virtually no documentation whatsoever. From the function names and type signatures you could maybe guess what this stuff does... but it's far from trivial. At any rate, this library seems to provide a high-level editing interface - which is the thing I'm trying to implement myself. I need something more low-level.
After wading through the source of haskeline, it seems it has a huge tangle low-level code, seperately for Win32 and POSIX. If there is an easy way to do console I/O, this library does not demonstrate it. The code appears to be so tightly integrated and highly specific to haskeline that I doubt I can reuse any of it. But perhaps by reading it I can learn enough to write my own?
Yi is... freaking massive. The Cabal file lists > 150 exposed modules. (!!) It appears, though, that underneath it's using a package called vty, which is POSIX-only. (I wonder how the hell Yi works on Windows then?) vty looks like it might be directly useful to me without further modification. (But again, not on Windows.)
unix has... basically nothing interesting. It has a bunch of stuff to set things on a terminal, but absolutely nothing for reading from a terminal. (Except maybe to check whether echo is on, etc. Nothing about keypresses.)
unix-compat has absolutely nothing of interest.
One option would be to use ncurses. A minimalistic example:
import Control.Monad
import UI.NCurses
main :: IO ()
main = runCurses $ do
w <- defaultWindow
forever $ do
e <- getEvent w Nothing
updateWindow w $ do
moveCursor 0 0
drawString (show e)
render
I think you are looking for hSetBuffering. StdIn is line buffered by default, but
you want to receive the keys right away.
I think the unix library provides the most lightweight solution for this, especially if you have some familiarity with termios, which is mirrored by the System.Posix.Terminal module.
There's a good page over at gnu.org that describes using termios to set up non-canonical input mode for a terminal, and you can do this with System.Posix.Terminal.
Here's my solution, which transforms a computation in IO to use non-canonical mode:
{- from unix library -}
import System.Posix.Terminal
import System.Posix.IO (fdRead, stdInput)
{- from base -}
import System.IO (hFlush, stdout)
import Control.Exception (finally, catch, IOException)
{- run an application in raw input / non-canonical mode with given
- VMIN and VTIME settings. for a description of these, see:
- http://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html
- as well as `man termios`.
-}
withRawInput :: Int -> Int -> IO a -> IO a
withRawInput vmin vtime application = do
{- retrieve current settings -}
oldTermSettings <- getTerminalAttributes stdInput
{- modify settings -}
let newTermSettings =
flip withoutMode EnableEcho . -- don't echo keystrokes
flip withoutMode ProcessInput . -- turn on non-canonical mode
flip withTime vtime . -- wait at most vtime decisecs per read
flip withMinInput vmin $ -- wait for >= vmin bytes per read
oldTermSettings
{- install new settings -}
setTerminalAttributes stdInput newTermSettings Immediately
{- restore old settings no matter what; this prevents the terminal
- from becoming borked if the application halts with an exception
-}
application
`finally` setTerminalAttributes stdInput oldTermSettings Immediately
{- sample raw input method -}
tryGetArrow = (do
(str, bytes) <- fdRead stdInput 3
case str of
"\ESC[A" -> putStrLn "\nUp"
"\ESC[B" -> putStrLn "\nDown"
"\ESC[C" -> putStrLn "\nRight"
"\ESC[D" -> putStrLn "\nLeft"
_ -> return ()
) `catch` (
{- if vmin bytes have not been read by vtime, fdRead will fail
- with an EOF exception. catch this case and do nothing.
- The type signature is necessary to allow other exceptions
- to get through.
-}
(const $ return ()) :: IOException -> IO ()
)
{- sample application -}
loop = do
tryGetArrow
putStr "." >> hFlush stdout
loop
{- run with:
- VMIN = 0 (don't wait for a fixed number of bytes)
- VTIME = 1 (wait for at most 1/10 sec before fdRead returns)
-}
main = withRawInput 0 1 $ loop
Writing a program in Haskell, I am struggling to handle key presses of the form ctrl-s and ctrl-l
I am using the following code:
main :: IO ()
main = do
hSetBuffering stdin NoBuffering
x <- getChar
putStrLn ("You pressed: " ++ [x])
How can I make it recognise the Ctrl button being pressed?
getChar gives you access to characters, not keypresses. Which character you get depends on your user's operating system, keyboard layout, and choice of input method. There is no 'standard' Character which will be generated by the keypresses Ctrl-S or Ctrl-L (although, certainly, some systems will give the standard ASCII codes for those control characters, others will not).
If you want proper keypress handling you need a real input library - like, for example, SDL or WxWidgets or GTK; each of which is much more than just an input library but they do have keypress abstractions.
I am currently on a non-unix system (causing unix, a dependency of vty, to fail to install), but the following may work.
import Control.Exception
import Graphics.Vty.LLInput
import System.Console.Terminfo
main :: IO ()
main = do
term <- setupTermFromEnv
bracket (initTermInput 0 term) (\ (_, exit) -> exit) $ \ (readEvent, _) -> do
let readKeyEvent = do
ev <- readEvent
case ev of
EvKey k ms -> return (k, ms)
_ -> readKeyEvent
readKeyEvent >>= print
I'm trying to use the interact function, but I'm having an issue with the following code:
main::IO()
main = interact test
test :: String -> String
test [] = show 0
test a = show 3
I'm using EclipseFP and taking one input it seems like there is an error. Trying to run main again leads to a:
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)
I'm not sure why this is not working, the type of test is String -> String and show is Show a => a -> String, so it seems like it should be a valid input for interact.
EDIT/UPDATE
I've tried the following and it works fine. How does the use of unlines and lines cause interact to work as expected?
main::IO()
main = interact respondPalindromes
respondPalindromes :: String -> String
respondPalindromes =
unlines .
map (\xs -> if isPal xs then "palindrome" else "not a palindrome") .
lines
isPal :: String -> Bool
isPal xs = xs == reverse xs
GHCi and Unsafe I/O
You can reduce this problem (the exception) to:
main = getContents >> return ()
(interact calls getContents)
The problem is that stdin (getContents is really hGetContents stdin) remains evaluated in GHCi in-between calls to main. If you look up stdin, it's implemented as:
stdin :: Handle
stdin = unsafePerformIO $ ...
To see why this is a problem, you could load this into GHCi:
import System.IO.Unsafe
f :: ()
f = unsafePerformIO $ putStrLn "Hi!"
Then, in GHCi:
*Main> f
Hi!
()
*Main> f
()
Since we've used unsafePerformIO and told the compiler that f is a pure function, it thinks it doesn't need to evaluate it a second time. In the case of stdin, all of the initialization on the handle isn't run a second time and it's still in a semi-closed state (which hGetContents puts it in), which causes the exception. So I think that GHCi is "correct" in this case and the problem lies in the definition of stdin which is a practical convenience for compiled programs that will just evaluate stdin once.
Interact and Lazy I/O
As for why interact quits after a single line of input while the unlines . lines version continues, let's try reducing that as well:
main :: IO ()
main = interact (const "response\n")
If you test the above version, interact won't even wait for input before printing response. Why? Here's the source for interact (in GHC):
interact f = do s <- getContents
putStr (f s)
getContents is lazy I/O, and since f in this case doesn't need s, nothing is read from stdin.
If you change your test program to:
main :: IO ()
main = interact test
test :: String -> String
test [] = show 0
test a = show a
you should notice different behavior. And that suggests that in your original version (test a = show 3), the compiler is smart enough to realize that it only needs enough input to determine if the string read is empty or not (because if it's not empty, it doesn't need to know what a is, it just needs to print "3"). Since the input is presumably line-buffered on a terminal, it reads up until you press the return key.
Earlier today I wrote a small test app for iteratees that composed an iteratee for writing progress with an iteratee for actually copying data. I wound up with values like these:
-- NOTE: this snippet is with iteratees-0.8.5.0
-- side effect: display progress on stdout
displayProgress :: Iteratee ByteString IO ()
-- side effect: copy the bytestrings of Iteratee to Handle
fileSink :: Handle -> Iteratee ByteString IO ()
writeAndDisplayProgress :: Handle -> Iteratee ByteString IO ()
writeAndDisplayProgress handle = sequence_ [fileSink handle, displayProgress]
In looking at the enumerator library, I don't see an analog of sequence_ or enumWith. All I want to do is compose two iteratees so they act as one. I could discard the result (it's going to be () anyway) or keep it, I don't care. (&&&) from Control.Arrow is what I want, only for iteratees rather than arrows.
I tried these two options:
-- NOTE: this snippet is with enumerator-0.4.10
run_ $ enumFile source $$ sequence_ [iterHandle handle, displayProgress]
run_ $ enumFile source $$ sequence_ [displayProgress, iterHandle handle]
The first one copies the file, but doesn't show progress; the second one shows progress, but doesn't copy the file, so obviously the effect of the built-in sequence_ on enumerator's iteratees is to run the first iteratee until it terminates and then run the other, which is not what I want. I want to be running the iteratees in parallel rather than serially. I feel like I'm missing something obvious, but in reading the wc example for the enumerator library, I see this curious comment:
-- Exactly matching wc's output is too annoying, so this example
-- will just print one line per file, and support counting at most
-- one statistic per run
I wonder if this remark indicates that combining or composing iteratees within the enumerations framework isn't possible out of the box. What's the generally-accepted right way to do this?
Edit:
It seems as though there is no built-in way to do this. There's discussion on the Haskell mailing list about adding combinators like enumSequence and manyToOne but so far, there doesn't seem to be anything actually in the enumerator package that furnishes this capability.
It seems to me like rather than trying to have two Iteratees consume the sequence in parallel, it would be better to feed the stream through an identity Enumeratee that simply counts the bytes passing it.
Here's a simple example that copies a file and prints the number of bytes copied after each chunk.
import System.Environment
import System.IO
import Data.Enumerator
import Data.Enumerator.Binary (enumFile, iterHandle)
import Data.Enumerator.List (mapAccumM)
import qualified Data.ByteString as B
printBytes :: Enumeratee B.ByteString B.ByteString IO ()
printBytes = flip mapAccumM 0 $ \total bytes -> do
let total' = total + B.length bytes
print total'
return (total', bytes)
copyFile s t = withBinaryFile t WriteMode $ \h -> do
run_ $ (enumFile s $= printBytes) $$ iterHandle h
main = do
[source, target] <- getArgs
copyFile source target
I'm pretty new to Haskell, so I'm looking for a simple-ish way to detect keypresses, rather than using getLine.
If anyone knows any libraries, or some trick to doing this, it would be great!
And if there is a better place to ask this, please direct me there, I'd appreciate it.
If you don't want blocking you can use hReady to detect whether a key has been pressed yet. This is useful for games where you want the program to run and pick up a key press whenever it has happened without pausing the game.
Here's a convenience function I use for this:
ifReadyDo :: Handle -> IO a -> IO (Maybe a)
ifReadyDo hnd x = hReady hnd >>= f
where f True = x >>= return . Just
f _ = return Nothing
Which can be used like this:
stdin `ifReadyDo` getChar
Returning a Maybe that is Just if a key was pressed and Nothing otherwise.
import System.IO
main :: IO ()
main = do
hSetBuffering stdin NoBuffering
x <- getChar
putStrLn ("You pressed: " ++ [x])
I don't know when this is guaranteed to work. Putting the terminal into a "raw" mode is a system-dependent process. But it works for me with GHC 6.12.1 on Linux.