Haskell read raw keyboard input

前端 未结 6 1374
暗喜
暗喜 2020-11-28 12:04

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-edi

6条回答
  •  没有蜡笔的小新
    2020-11-28 12:37

    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
    

提交回复
热议问题