Skip to content

Commit 0c72e7b

Browse files
committed
Add useTermHandles behavior for explicitly providing term handles and term type
1 parent 7d8d1ab commit 0c72e7b

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

System/Console/Haskeline.hs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 17 additions & 5 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module System.Console.Haskeline.Backend.Posix (
99
mapLines,
1010
stdinTTYHandles,
1111
ttyHandles,
12+
explicitTTYHandles,
1213
posixRunTerm,
1314
fileRunTerm
1415
) where
@@ -276,6 +277,15 @@ openTerm :: IOMode -> MaybeT IO ExternalHandle
276277
openTerm mode = handle (\(_::IOException) -> mzero)
277278
$ liftIO $ openInCodingMode "/dev/tty" mode
278279

280+
explicitTTYHandles :: Handle -> Handle -> MaybeT IO Handles
281+
explicitTTYHandles h_in h_out = do
282+
isInTerm <- liftIO $ hIsTerminalDevice h_in
283+
guard isInTerm
284+
return Handles
285+
{ hIn = externalHandle h_in
286+
, hOut = externalHandle h_out
287+
, closeHandles = return ()
288+
}
279289

280290
posixRunTerm ::
281291
Handles

System/Console/Haskeline/Backend/Terminfo.hs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 10 additions & 0 deletions
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 @~/.haskeline.@ If there is an error reading the file,
221231
-- the 'defaultPrefs' will be returned.

0 commit comments

Comments
 (0)