File tree 5 files changed +43
-8
lines changed
5 files changed +43
-8
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,9 @@ module System.Console.Haskeline(
39
39
defaultBehavior ,
40
40
useFileHandle ,
41
41
useFile ,
42
+ #ifndef MINGW
43
+ useTermHandles ,
44
+ #endif
42
45
preferTerm ,
43
46
-- * User interaction functions
44
47
-- ** 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 @@ -11,6 +11,7 @@ module System.Console.Haskeline.Backend.Posix (
11
11
mapLines ,
12
12
stdinTTYHandles ,
13
13
ttyHandles ,
14
+ explicitTTYHandles ,
14
15
posixRunTerm ,
15
16
fileRunTerm
16
17
) where
@@ -286,6 +287,15 @@ openTerm :: IOMode -> MaybeT IO ExternalHandle
286
287
openTerm mode = handle (\ (_:: IOException ) -> mzero)
287
288
$ liftIO $ openInCodingMode " /dev/tty" mode
288
289
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
+ }
289
299
290
300
posixRunTerm ::
291
301
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 @$XDG_CONFIG_HOME/haskeline/haskeline@ if present
221
231
-- ortherwise @~/.haskeline.@ If there is an error reading the file,
You can’t perform that action at this time.
0 commit comments