Search code examples
haskellterminalio

Pass string to edit in terminal and confirm with enter-key in Haskell program


Is there a way to "print" a string in terminal and the user can edit this string and press Enter to confirm and pass it back into program?


Solution

  • Yes, using haskeline (a readline-style library). It has the function getInputLineWithInitial which has the behavior you are asking for. Example:

    import System.Console.Haskeline
    
    main = do
      result <- runInputT defaultSettings $ getInputLineWithInitial ""
         ("Pass string to edit in terminal ", "in Haskell program")
      print result