Comments (8)
@Kaligule @simonmichael @jtdaugherty
I'm super late to the party here but I found a way past this for anyone running into this who is willing to use customMain
instead of simpleMain
. customMain
allows you to override the underlying VTY configuration and give it the handle of the controlling terminal even when input originally comes from a pipe.
Reading stdin using getContents
will put the handle into a semi closed state. Just read it, then close the handle. After this, using System.Posix.Terminal
, you can make a call to getControllingTerminalName
then open that descriptor using openFd
in the System.Posix.IO.ByteString
module, and then pass that descriptor to inputFd
on the VTY value passed to customMain
.
Here is a crude example that requires the unix
, brick
, vty
, bytestring
packages:
You can test this with your shell: $ echo 12345 | YourApp
module Main where
import Brick
import Brick.BChan
import Control.Monad
import qualified Data.ByteString.Char8 as C
import Graphics.Vty
import Graphics.Vty.Config
import System.IO
import System.Posix.IO.ByteString
import System.Posix.Terminal
import System.Posix.Types
data AppState = AppState String
type ResourceName = ()
main :: IO ()
main = do
stdin' <- getContents
print stdin' -- no lazy plz
_ <- hClose stdin
terminalName <- getControllingTerminalName
terminalFd <- openFd (C.pack terminalName) ReadOnly Nothing (OpenFileFlags False False False False False)
channel <- newBChan 1000
initialVty <- buildVty terminalFd
_ <- customMain initialVty (buildVty terminalFd) (Just channel) app (AppState stdin')
return ()
where
buildVty :: Fd -> IO Vty
buildVty terminalFd' = do
vtyCfg <- standardIOConfig
v <- mkVty $ vtyCfg { inputFd = Just terminalFd' }
when (supportsMode (outputIface v) Mouse) $ setMode (outputIface v) Mouse True
when (supportsMode (outputIface v) BracketedPaste) $ setMode (outputIface v) BracketedPaste True
return v
app :: App AppState e ResourceName
app = App draw neverShowCursor handleEvent startEvent attributeMap
draw :: AppState -> [Widget ResourceName]
draw (AppState stdin) = [ str stdin ]
handleEvent :: AppState -> BrickEvent ResourceName e -> EventM ResourceName (Next AppState)
handleEvent s e = continue s
startEvent :: AppState -> EventM ResourceName AppState
startEvent = return
attributeMap :: AppState -> AttrMap
attributeMap = const $ attrMap Graphics.Vty.defAttr []
from brick.
I think the issue here is that invoking a program with standard input from the terminal is not the same as invoking a program with a pipe redirection. In the former case, stdin
is a file descriptor from the terminal that supports different ioctl
system calls (such as the ones needed by vty
), while the file descriptor opened by the shell for the pipe redirection is not a terminal device and will not support the same ioctl
operations. In the redirection case you get the error about an unsupported ioctl
as a result.
from brick.
Here is something that sheds a little light on what I'm talking about:
http://unix.stackexchange.com/questions/68712/how-to-detect-if-a-program-is-not-run-from-a-terminal
from brick.
Here's another:
from brick.
So, to sum up: this isn't a vty
or brick
problem; it's purely a consequence of using a pipe where none should be used because vty
(and therefore brick
) programs expect stdin
to be a terminal.
from brick.
I see that this sort of thing could happen in a nonpure world of bash. Thanks for pointing that out. I am going to search for a workaround.
from brick.
@Kaligule did you find a workaround ? We'd like to make this work:
$ cat some.file | hledger-ui -f- # 1. take initial input from stdin
# 2. continue in normal interactive mode
I guess some shuffling of handles could do it.
from brick.
@simonmichael I never found a way to make it work, sorry.
from brick.
Related Issues (20)
- Wrap repeating code in a function HOT 18
- handleFormEvent doesn't update form field validation state? HOT 22
- row ... now exceeds region width HOT 8
- Is there any way to wait for an input? For example, wait for confirmation. HOT 8
- There should be something like `fmapState`. HOT 10
- Consider adding `zoomOut` for saving lines when dealing with sum types. HOT 3
- Cannot install `brick` via `cabal update && cabal install brick` on NixOS HOT 9
- Add version of customMain that doesn't require a Vty handle HOT 11
- Documentation Unavailable on Hackage HOT 4
- Can you add brick-trading-journal to featured projects on README? HOT 1
- Mouse-based selection stops working after using the mouse in an app. HOT 17
- Visbility request for FormFieldState instead of FormField HOT 7
- Segmentation fault when eagerly pressing a keyboard key HOT 3
- test compilation failure with brick-2.1.1 in Nixpkgs HOT 3
- How can I get SIGTSTP (ctrl-z) to suspend the process? HOT 6
- Thread blocked indefinitely in an STM transaction HOT 35
- Provide absolute coordinates on brick mouse event HOT 5
- Progress bar usage HOT 6
- How to use dialogSelection with appHandleEvent function? HOT 7
- Pop-up overlay dialog, is it possible? HOT 23
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brick.