haskellread-eval-print-loophaskeline

How do I get the command history using Haskelline?


I can't seem to figure out how to get Haskeline to allow the user to use the arrow keys to go through command history.

I read Hackage and tried using Settings { complete = completeFilename, historyFile = Nothing, autoAddHistory = True } like the default they gave and that didn't work, I tried giving historyFile a value and then it would only save the first command I typed in and only after the program was quit, and none of the small tweaks I tried have helped. I can provide the rest of my code if it's relevant.


Solution

  • Not a full answer, but can you test the following minimal example:

    import System.Console.Haskeline
    
    main :: IO ()
    main = runInputT settings loop
      where
    
        loop = do
          getInputLine "> "
          loop
    
        settings = Settings
          { complete = completeFilename
          , historyFile = Just ".foo_history"
          , autoAddHistory = True }
    

    You should be able to enter multiple input lines at the prompt and scroll through the history with the arrow keys:

    $ runghc Line.hs
    > foo
    > bar
    > baz
    > [use arrow keys here]
    

    If you Ctrl-C out after entering some lines, they should all appear in the ".foo_history" file:

    > [Ctrl-C]
    
    $ cat .foo_history 
    baz
    bar
    foo
    

    If the arrow keys don't work, try the Emacs-compatible keystrokes (Ctrl-P and Ctrl-N for previous and next).

    Based on this: