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

Fix #130 Don't skip "\ESC...\STX" sequences (when terminal-style) #126

Open mpilgrem opened 4 years ago

mpilgrem commented 4 years ago

This reverses commit f827f10145380ec9e685a72e4b8a7a27ef6717ea of 11 September 2010 (about 5 years before the November 2015 Update to Windows 10 made the native consoles on Windows 10 ANSI-capable). Applications like GHCi and stack enable the ANSI-capability of the native consoles.

In respect of legacy Windows, none of them have mainstream support by Microsoft and Windows 7 loses extended support on 14 January 2020.

It has been tested on Windows 10 with

module Main where

import System.Console.Haskeline

main :: IO ()
main = runInputT defaultSettings loop
 where
  prompt = "\ESC[34m\STXBlue\ESC[39m\STX\n\ESC[31m\STXRed\ESC[39m\STX: "
  loop :: InputT IO ()
  loop = do
    minput <- getInputLine prompt
    case minput of
      Nothing -> pure ()
      Just "quit" -> pure ()
      Just input -> do outputStrLn $ "Input was: " ++ input
                       loop
mpilgrem commented 2 years ago

I have taken a different approach to my original pull request. I use the same logic as in the ansi-terminal package to detect if the Windows terminal is ANSI-capable or not. If not ANSI-capable, the existing code is retained.

It has been tested on Windows 11, with the same example code as above. As in the case of ansi-terminal, I am unable to test the logic on machines without an ANSI-capable terminal as I no longer have access to machines with legacy Windows.

ChrisPenner commented 2 years ago

Hrmm, I tested 3b26f1e on my Windows 11 VM via Parallels and got the following result, note how the ANSI in the prompt isn't being interpreted.

image

However, the simple revert of the original change, d6c2643b0d5c19be7e440615c6f84d603d4bc648, seems to behave as intended:

image