Only set console terminal title when stdout supports ANSI sequences

In some environments, setting the terminal title does not work, like
Emacs. This results in very ugly output when using Cryptol as a REPL
inside emacs with cryptol-mode, because Emacs merely repeats the
escape sequences back to you, completely ruining the main REPL.

This patch only sets the terminal if the `stdout` handle is detected
to support ANSI escape sequences. This is an adequate check for
setting the title. Note the exact semantics are to check that the
output is a terminal, and that `TERM` is not set to `dumb`, which is
what Emacs sets it to. In the future, `shouldSetREPLTitle` may be
expanded to include other cases if that isn't enough.

Note a subtle point about this: the function to check this case is
very specifically named, because while we may not be able to support
certain terminal codes, we *can* support ANSI escape sequences in
places like Emacs, reliably.

Thus, this patch does not have any changes to the color output: that
should be handled by a follow up patch (that allows either autodetect,
or explicit enable/disable, on the command line). Then, Emacs will be
able to explicitly request color support for the REPL when invoked,
while the REPL code itself will reliably abstain from setting the
title in such environments automatically. There shouldn't ever be a
need to export some '--set-ansi-terminal-title' flag with this logic.

Signed-off-by: Austin Seipp <aseipp@pobox.com>
This commit is contained in:
Austin Seipp 2016-08-19 18:44:39 +00:00
parent 12bbfb5d7d
commit c897575af5
2 changed files with 11 additions and 2 deletions

View File

@ -210,7 +210,7 @@ setupREPL opts = do
4 (vcat (map pp smoke)))
exitFailure
displayLogo True
setUpdateREPLTitle setREPLTitle
setUpdateREPLTitle (shouldSetREPLTitle >>= \b -> when b setREPLTitle)
updateREPLTitle
mCryptolPath <- io $ lookupEnv "CRYPTOLPATH"
case mCryptolPath of

View File

@ -24,7 +24,8 @@ import Control.Monad.Trans.Control
import Data.Char (isAlphaNum, isSpace)
import Data.Function (on)
import Data.List (isPrefixOf,nub,sortBy,sort)
import System.Console.ANSI (setTitle)
import System.IO (stdout)
import System.Console.ANSI (setTitle, hSupportsANSI)
import System.Console.Haskeline
import System.Directory ( doesFileExist
, getHomeDirectory
@ -151,6 +152,14 @@ setREPLTitle = do
lm <- getLoadedMod
io (setTitle (mkTitle lm))
-- | In certain environments like Emacs, we shouldn't set the terminal
-- title. Note: this does not imply we can't use color output. We can
-- use ANSI color sequences in places like Emacs, but not terminal
-- codes. Rather, the lack of color output would imply this option, not the
-- other way around.
shouldSetREPLTitle :: REPL Bool
shouldSetREPLTitle = io (hSupportsANSI stdout)
-- Completion ------------------------------------------------------------------
-- | Completion for cryptol commands.