haskell / haskeline

A Haskell library for line input in command-line programs.
https://hackage.haskell.org/package/haskeline
BSD 3-Clause "New" or "Revised" License
221 stars 75 forks source link

Handling of control sequences is inconsistent when the prompt spans multiple lines #72

Closed MarLinn closed 5 years ago

MarLinn commented 6 years ago

Consider these testcases:

t1 = getInputLine " \ESC[36m%\ESC[0m "
t2 = getInputLine " \ESC[36m\STX%\ESC[0m\STX "
t3 = getInputLine "\ESC[32m\STX–––\ESC[0m\STX\n \ESC[36m\STX%\ESC[0m\STX "
t4 = getInputLine "\ESC[32m–––\ESC[0m\n \ESC[36m\STX%\ESC[0m\STX "
t5 = getInputLine "\SOH\ESC[32m\STX–––\SOH\ESC[0m\STX\n\SOH\ESC[36m\STX%\SOH\ESC[0m\STX "

runTest = do
    runInputT defaultSettings (t1 >> t1) >>= print
    runInputT defaultSettings (t2 >> t2) >>= print
    runInputT defaultSettings (t3 >> t3) >>= print
    runInputT defaultSettings (t4 >> t4) >>= print

Testcase 1 is just to demonstrate why the \STX is needed, just as the wiki says. Enter too many chars, press enter, recall the last entry, pres pos1, edit, press enter, and see that the cursor position was off. So far, nothing new. Testcase 2 shows the official, working solution. Testcase 3 uses a multi-line prompt following the official solution. It works, but now weird characters show up in the first line (tested on xfce4-terminal) Testcase 4 shows what's necessary to combine working cursor positions with multiline prompts. It works, but it is neither consistent nor documented. Testcase 5 shows that the issue goes further once you need additional control characters. One application is if you want to pipe ghci output through a post processor.

I'm not sure how much of this is a bug and how much of this is just a gap in the documentation.