Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support non-controlling terminals #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions System/Console/Haskeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ module System.Console.Haskeline(
defaultBehavior,
useFileHandle,
useFile,
#ifndef MINGW
useTermHandles,
#endif
preferTerm,
-- * User interaction functions
-- ** Reading user input
Expand Down
22 changes: 17 additions & 5 deletions System/Console/Haskeline/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,39 @@ defaultRunTerm = (liftIO (hGetEcho stdin) >>= guard >> stdinTTY)
terminalRunTerm :: IO RunTerm
terminalRunTerm = directTTY `orElse` fileHandleRunTerm stdin

#ifndef MINGW
useTermHandlesRunTerm :: Maybe String -> Handle -> Handle -> IO RunTerm
useTermHandlesRunTerm termtype input output =
explicitTTY termtype input output `orElse` fileHandleRunTerm input
#endif

stdinTTY :: MaybeT IO RunTerm
#ifdef MINGW
stdinTTY = win32TermStdin
#else
stdinTTY = stdinTTYHandles >>= runDraw
stdinTTY = stdinTTYHandles >>= runDraw Nothing
#endif

directTTY :: MaybeT IO RunTerm
#ifdef MINGW
directTTY = win32Term
#else
directTTY = ttyHandles >>= runDraw
directTTY = ttyHandles >>= runDraw Nothing
#endif

#ifndef MINGW
explicitTTY :: Maybe String -> Handle -> Handle -> MaybeT IO RunTerm
explicitTTY termtype input output =
explicitTTYHandles input output >>= runDraw termtype
#endif


#ifndef MINGW
runDraw :: Handles -> MaybeT IO RunTerm
runDraw :: Maybe String -> Handles -> MaybeT IO RunTerm
#ifndef TERMINFO
runDraw = runDumbTerm
runDraw _termtype = runDumbTerm
#else
runDraw h = runTerminfoDraw h `mplus` runDumbTerm h
runDraw termtype h = runTerminfoDraw termtype h `mplus` runDumbTerm h
#endif
#endif

Expand Down
10 changes: 10 additions & 0 deletions System/Console/Haskeline/Backend/Posix.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module System.Console.Haskeline.Backend.Posix (
mapLines,
stdinTTYHandles,
ttyHandles,
explicitTTYHandles,
posixRunTerm,
fileRunTerm
) where
Expand Down Expand Up @@ -286,6 +287,15 @@ openTerm :: IOMode -> MaybeT IO ExternalHandle
openTerm mode = handle (\(_::IOException) -> mzero)
$ liftIO $ openInCodingMode "/dev/tty" mode

explicitTTYHandles :: Handle -> Handle -> MaybeT IO Handles
explicitTTYHandles h_in h_out = do
isInTerm <- liftIO $ hIsTerminalDevice h_in
guard isInTerm
return Handles
{ hIn = externalHandle h_in
, hOut = externalHandle h_out
, closeHandles = return ()
}

posixRunTerm ::
Handles
Expand Down
6 changes: 3 additions & 3 deletions System/Console/Haskeline/Backend/Terminfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ evalDraw term actions = EvalTerm eval liftE
. unDraw


runTerminfoDraw :: Handles -> MaybeT IO RunTerm
runTerminfoDraw h = do
mterm <- liftIO $ Exception.try setupTermFromEnv
runTerminfoDraw :: Maybe String -> Handles -> MaybeT IO RunTerm
runTerminfoDraw termtype h = do
mterm <- liftIO $ Exception.try $ maybe setupTermFromEnv setupTerm termtype
case mterm of
Left (_::SetupTermError) -> mzero
Right term -> do
Expand Down
10 changes: 10 additions & 0 deletions System/Console/Haskeline/InputT.hs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ useFile file = Behavior $ do
preferTerm :: Behavior
preferTerm = Behavior terminalRunTerm

#ifndef MINGW
-- | Use terminal-style interaction on the given input and output handles. The terminal
-- type may also be explicitly specified.
--
-- This behavior is for dealing with terminals other than the controlling terminal.
-- The caller is responsible for closing handles after use. Not available on Windows.
useTermHandles :: Maybe String -> Handle -> Handle -> Behavior
useTermHandles termtype input output =
Behavior $ useTermHandlesRunTerm termtype input output
#endif

-- | Read 'Prefs' from @~/.haskeline.@ If there is an error reading the file,
-- the 'defaultPrefs' will be returned.
Expand Down