Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbcurse functions badly with piped STDIN #14

Open
CarlQLange opened this issue Feb 23, 2013 · 6 comments
Open

rbcurse functions badly with piped STDIN #14

CarlQLange opened this issue Feb 23, 2013 · 6 comments

Comments

@CarlQLange
Copy link

Hey, I'm having trouble with rbcurse and piped input. This is reproducible with (among others) your basiclistbox example.

$ ruby ./abasiclistbox.rb takes input correctly, but $ echo "some input" | ruby ./abasiclistbox.rb stops taking input and causes other glitches.

I've noticed that window.getch will always return 3, instead of the actual character code.

This might be a bug further down in ncurses (or, more likely, I just haven't done something right), unfortunately I'm not well-versed enough to dive into that rabbit hole.

Any help would be greatly appreciated!

@rkumar
Copy link
Owner

rkumar commented Feb 23, 2013

I am not clear what you are trying to do with piping in the input. Of course, the sample is not reading in the STDIN... I will have to try this out ... the sample should read in stdin and then pass it to the main program.

Yes, there are issues in ncurses itself when you move from cooked mode to raw mode and back. btw, I just tried the same thing with other programs. I did "echo some input | mc" and mc crashed. Then i tried with "vifm" and that came out saying "terminal error". Give me a little time. I'll check stackoverflow also.

@CarlQLange
Copy link
Author

Thanks for responding so fast!

I am not clear what you are trying to do with piping in the input.

I'm trying to write a very simple program that displays URLs from text that's piped in.

@rkumar
Copy link
Owner

rkumar commented Feb 23, 2013

In such a case, you need to put a read loop (just like we read the ARGS in
a command line program) here we would read STDIN and pass it as a list to
the main program which will use that for populating the list.

This link has examples of reading stdin in ruby.
http://stackoverflow.com/questions/273262/best-practices-with-stdin-in-ruby

Okay I just tried out some stuff frm that link.

alist = nil
if $stdin.tty?
else
alist = []
$stdin.each_line do |line|
alist.push line
end
end
if !alist
alist = File.open("data/brew.txt",'r').readlines
end
.
I added this to the abasiclist.rb and it does populate the list if i do
something like "ls --color=none | ruby abasiclist.rb" (note that i
switched off colors in LS so no ansi-codes went in).

It printed the list but did not take input from keyboard (I had to kill the
process). I think this is because when we are passing stdin to a program it
is not a TTY or terminal. I will have to pull out some of the older samples
and read in STDIN before ncurses is started.

At the same time, programs like less and most do use ncurses and take
stdin, so there must be some way of doing this. I am not sure where this
issue lies -- in ncurses or in rbcurse. I will check one by one and
eliminate the issue.

In fact i will first try this on the ffi-ncurses samples if i find one that
is close enough. If it fails there then likely this is something i will
have to take up with the ffi-ncurses maintainer.

re
rahul

On Sat, Feb 23, 2013 at 12:48 PM, Carl Lange [email protected]:

Thanks for responding so fast!

I am not clear what you are trying to do with piping in the input.

I'm trying to write a very simple program that displays URLs from text
that's piped in.


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-13986449.

rahul

@CarlQLange
Copy link
Author

@rkumar
Copy link
Owner

rkumar commented Feb 23, 2013

I actually found the same thing in an example called "viewer.rb" in
ffi-ncurses:

We need to open /dev/tty so we can read from STDIN without borking

ncurses

term = CLib.fopen("/dev/tty", "rb+")

screen = newterm(nil, term, term)

old_screen = set_term(screen)

some helper methods for working with stdlib FILE pointers

module CLib

extend FFI::Library

ffi_lib FFI::Library::LIBC

typedef :pointer, :FILEP

FILE* open, close and eof

attach_function :fopen, [:string, :string], :FILEP

attach_function :fclose, [:FILEP], :int

attach_function :feof, [:FILEP], :int

attach_function :fflush, [:FILEP], :int

attach_function :fputs, [:string, :FILEP], :int

end

On Sat, Feb 23, 2013 at 3:07 PM, Carl Lange [email protected]:

I suspect this may help?
https://github.com/seanohalpin/ffi-ncurses/blob/master/examples/newterm.rb


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-13987575.

r
"Don't
let yourself get attached to anything you are not willing to walk out on in
30 seconds flat if you feel the heat around the corner."

@rkumar
Copy link
Owner

rkumar commented Feb 23, 2013

On Sat, Feb 23, 2013 at 3:07 PM, Carl Lange [email protected]:

I suspect this may help?
https://github.com/seanohalpin/ffi-ncurses/blob/master/examples/newterm.rb


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-13987575.

I have finally had some success. I used an older sample that does not use
APP. IT's called testmessagebox.rb -- this is perhaps the first sample I
wrote.

https://gist.github.com/rkumar/5019688

After start_ncurses, P put the lines about opening tty. Then i read from
stdin.
This works but for one thing, that the tty is now black and white, so i
presume the colors set in start_ncurses will have to be done a second time.

However, when i tried the same thing in abasiclist.rb, the screen still
freezes.

https://gist.github.com/rkumar/5019688

rahul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants