File tree Expand file tree Collapse file tree 5 files changed +41
-8
lines changed Expand file tree Collapse file tree 5 files changed +41
-8
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ module System.Console.Haskeline(
39
39
defaultBehavior ,
40
40
useFileHandle ,
41
41
useFile ,
42
+ useTermHandles ,
42
43
preferTerm ,
43
44
-- * User interaction functions
44
45
-- ** Reading user input
Original file line number Diff line number Diff line change @@ -23,27 +23,39 @@ defaultRunTerm = (liftIO (hGetEcho stdin) >>= guard >> stdinTTY)
23
23
terminalRunTerm :: IO RunTerm
24
24
terminalRunTerm = directTTY `orElse` fileHandleRunTerm stdin
25
25
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
+
26
32
stdinTTY :: MaybeT IO RunTerm
27
33
#ifdef MINGW
28
34
stdinTTY = win32TermStdin
29
35
#else
30
- stdinTTY = stdinTTYHandles >>= runDraw
36
+ stdinTTY = stdinTTYHandles >>= runDraw Nothing
31
37
#endif
32
38
33
39
directTTY :: MaybeT IO RunTerm
34
40
#ifdef MINGW
35
41
directTTY = win32Term
36
42
#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
38
50
#endif
39
51
40
52
41
53
#ifndef MINGW
42
- runDraw :: Handles -> MaybeT IO RunTerm
54
+ runDraw :: Maybe String -> Handles -> MaybeT IO RunTerm
43
55
#ifndef TERMINFO
44
- runDraw = runDumbTerm
56
+ runDraw _termtype = runDumbTerm
45
57
#else
46
- runDraw h = runTerminfoDraw h `mplus` runDumbTerm h
58
+ runDraw termtype h = runTerminfoDraw termtype h `mplus` runDumbTerm h
47
59
#endif
48
60
#endif
49
61
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ module System.Console.Haskeline.Backend.Posix (
9
9
mapLines ,
10
10
stdinTTYHandles ,
11
11
ttyHandles ,
12
+ explicitTTYHandles ,
12
13
posixRunTerm ,
13
14
fileRunTerm
14
15
) where
@@ -276,6 +277,15 @@ openTerm :: IOMode -> MaybeT IO ExternalHandle
276
277
openTerm mode = handle (\ (_:: IOException ) -> mzero)
277
278
$ liftIO $ openInCodingMode " /dev/tty" mode
278
279
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
+ }
279
289
280
290
posixRunTerm ::
281
291
Handles
Original file line number Diff line number Diff line change @@ -125,9 +125,9 @@ evalDraw term actions = EvalTerm eval liftE
125
125
. unDraw
126
126
127
127
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
131
131
case mterm of
132
132
Left (_:: SetupTermError ) -> mzero
133
133
Right term -> do
Original file line number Diff line number Diff line change @@ -216,6 +216,16 @@ useFile file = Behavior $ do
216
216
preferTerm :: Behavior
217
217
preferTerm = Behavior terminalRunTerm
218
218
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
219
229
220
230
-- | Read 'Prefs' from @~/.haskeline.@ If there is an error reading the file,
221
231
-- the 'defaultPrefs' will be returned.
You can’t perform that action at this time.
0 commit comments