[Haskell-cafe] reading from a unix pipe and then from the user

Aramís Concepción Durán aramis at systemli.org
Sat Oct 26 08:27:30 UTC 2019


Hello,

I'm trying to write a program that first reads input from
a pipe and then reads keyboard input from the user (like
fzf, the fuzzy finder).

Under different circumstances I could open a handle to
`/dev/tty` and use the `hGet...` functions from `System.IO`
to get the user's keyboard input, but I'm constructing the
text user interface with the brick library, which doesn't
seem to give my any way to sneak in alternative handles
through its application entry point functions.

So, what I'm looking for is a function which takes an
`IO ()` function like the usual `main` and a `Handle`
and turns it into an `IO ()` function that reads from the
provided handle instead of `stdin`.

Does that make sense?  I might be looking for something
that doesn't exist.  Or I'm lacking the language to get
results from a search engine.

To illustrate the problem further, here is a condensed
brick application:

    import qualified Brick.Main     as Main
    import qualified Brick.Types    as Types
    import qualified Graphics.Vty   as Vty

    import Brick.AttrMap        ( attrMap )
    import Brick.Widgets.Core   ( str )

    type Event = Types.EventM () ( Types.Next () )

    main =
        let
            app =
                Main.App 
                    { Main.appChooseCursor = Main.neverShowCursor
                    , Main.appHandleEvent = \ _ _ -> Main.halt () :: Event
                    , Main.appStartEvent = pure
                    , Main.appAttrMap = \ _ -> attrMap Vty.currentAttr []
                    , Main.appDraw = \ _ -> [ str Hit any key to exit. ]
                    }
        in
            Main.defaultMain app ()

How do I get this `main` function to read from a custom
handle intead of stdin?


More information about the Haskell-Cafe mailing list