Skip to content

Commit 9b0a955

Browse files
committed
Add useTermHandles behavior for explicitly providing term handles and term type
1 parent 071ef5a commit 9b0a955

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

System/Console/Haskeline.hs

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ module System.Console.Haskeline(
3939
defaultBehavior,
4040
useFileHandle,
4141
useFile,
42+
#ifndef MINGW
43+
useTermHandles,
44+
#endif
4245
preferTerm,
4346
-- * User interaction functions
4447
-- ** Reading user input

System/Console/Haskeline/Backend.hs

+17-5
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,39 @@ defaultRunTerm = (liftIO (hGetEcho stdin) >>= guard >> stdinTTY)
2323
terminalRunTerm :: IO RunTerm
2424
terminalRunTerm = directTTY `orElse` fileHandleRunTerm stdin
2525

26+
#ifndef MINGW
27+
useTermHandlesRunTerm :: Maybe String -> Handle -> Handle -> IO RunTerm
28+
useTermHandlesRunTerm termtype input output =
29+
explicitTTY termtype input output `orElse` fileHandleRunTerm input
30+
#endif
31+
2632
stdinTTY :: MaybeT IO RunTerm
2733
#ifdef MINGW
2834
stdinTTY = win32TermStdin
2935
#else
30-
stdinTTY = stdinTTYHandles >>= runDraw
36+
stdinTTY = stdinTTYHandles >>= runDraw Nothing
3137
#endif
3238

3339
directTTY :: MaybeT IO RunTerm
3440
#ifdef MINGW
3541
directTTY = win32Term
3642
#else
37-
directTTY = ttyHandles >>= runDraw
43+
directTTY = ttyHandles >>= runDraw Nothing
44+
#endif
45+
46+
#ifndef MINGW
47+
explicitTTY :: Maybe String -> Handle -> Handle -> MaybeT IO RunTerm
48+
explicitTTY termtype input output =
49+
explicitTTYHandles input output >>= runDraw termtype
3850
#endif
3951

4052

4153
#ifndef MINGW
42-
runDraw :: Handles -> MaybeT IO RunTerm
54+
runDraw :: Maybe String -> Handles -> MaybeT IO RunTerm
4355
#ifndef TERMINFO
44-
runDraw = runDumbTerm
56+
runDraw _termtype = runDumbTerm
4557
#else
46-
runDraw h = runTerminfoDraw h `mplus` runDumbTerm h
58+
runDraw termtype h = runTerminfoDraw termtype h `mplus` runDumbTerm h
4759
#endif
4860
#endif
4961

System/Console/Haskeline/Backend/Posix.hsc

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module System.Console.Haskeline.Backend.Posix (
1111
mapLines,
1212
stdinTTYHandles,
1313
ttyHandles,
14+
explicitTTYHandles,
1415
posixRunTerm,
1516
fileRunTerm
1617
) where
@@ -286,6 +287,15 @@ openTerm :: IOMode -> MaybeT IO ExternalHandle
286287
openTerm mode = handle (\(_::IOException) -> mzero)
287288
$ liftIO $ openInCodingMode "/dev/tty" mode
288289

290+
explicitTTYHandles :: Handle -> Handle -> MaybeT IO Handles
291+
explicitTTYHandles h_in h_out = do
292+
isInTerm <- liftIO $ hIsTerminalDevice h_in
293+
guard isInTerm
294+
return Handles
295+
{ hIn = externalHandle h_in
296+
, hOut = externalHandle h_out
297+
, closeHandles = return ()
298+
}
289299

290300
posixRunTerm ::
291301
Handles

System/Console/Haskeline/Backend/Terminfo.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ evalDraw term actions = EvalTerm eval liftE
125125
. unDraw
126126

127127

128-
runTerminfoDraw :: Handles -> MaybeT IO RunTerm
129-
runTerminfoDraw h = do
130-
mterm <- liftIO $ Exception.try setupTermFromEnv
128+
runTerminfoDraw :: Maybe String -> Handles -> MaybeT IO RunTerm
129+
runTerminfoDraw termtype h = do
130+
mterm <- liftIO $ Exception.try $ maybe setupTermFromEnv setupTerm termtype
131131
case mterm of
132132
Left (_::SetupTermError) -> mzero
133133
Right term -> do

System/Console/Haskeline/InputT.hs

+10
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ useFile file = Behavior $ do
216216
preferTerm :: Behavior
217217
preferTerm = Behavior terminalRunTerm
218218

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

220230
-- | Read 'Prefs' from @$XDG_CONFIG_HOME/haskeline/haskeline@ if present
221231
-- ortherwise @~/.haskeline.@ If there is an error reading the file,

0 commit comments

Comments
 (0)