Piping println() through an external pager program (more, less)

J

Jon Gómez

Thomas Kellerer wrote:
[...]
I tried to retrieve the screen size from within a shell script because
interactively $LINES and $COLUMNS are available (echo $LINES shows the
correct value) but when trying to retrieve them using System.getenv() it
returns null.
[...]

I checked on my system: LINES and COLUMNS are shell variables, and they
are not part of the environment. However, TERM presumably is, if it is
set. Martin Gregorie brought up the possibility of looking into
terminfo and termcap stuff, in the context of a library he thought might
help do that for you. I checked, and the library doesn't, but the idea
is still valid.

Jon.
 
J

Jon Gómez

Thomas Kellerer wrote:
[...]
I tried to retrieve the screen size from within a shell script because
interactively $LINES and $COLUMNS are available (echo $LINES shows the
correct value) but when trying to retrieve them using System.getenv() it
returns null.
[...]

I checked on my system: LINES and COLUMNS are shell variables, and they
are not part of the environment. However, TERM presumably is, if it is
set. Martin Gregorie brought up the possibility of looking into
terminfo and termcap stuff, in the context of a library he thought might
help do that for you. I checked, and the library doesn't, but the idea
is still valid.

Jon.
 
J

Jon Gómez

I kept trying to send a message and my newsgroup server kept timing out.
I didn't realize each one had gone through. I didn't even think to
check. My mistake. I tried to issue a cancel on the duplicate messages.

Sorry,

Jon.
 
L

Lew

Are you sure PuTTY does not accept X11 forwarding?

I often use PuTTY to access some servers where I have shell accounts.
The configuration dialog has an option "Enable X11 forwarding". With it
checked, and the Cygwin X11 server running on my laptop, if I type e.g.
"xterm &" in the PuTTY window, an xterm, running on the server, displays
on my laptop.

For that matter, with cygwin on the Windows machine one can use the
cygwin command-line 'ssh' program with X11 forwarding.

I'm still not clear as to what the OP was discussing in the comment,
I'm also interested in how their program is made portable between
Swing and non-GUI interfaces:

That sounds like a good piece of engineering and I'd love some
pointers.
 
T

Thomas Kellerer

Martin Gregorie wrote on 17.03.2009 18:35:
PuTTY is a commonly used Windows client that won't accept X11 forwarding
because it is text only. If the OP's workplace uses it, that's a pretty
good reason for not using a GUI.
Putty *does* support X11 forwarding (actually any forwarding - I have
successfully used it to forward database connections efore) without problems.

It's the customer's server which will refuse to do it (and which I cannnot
control). Don't ask me for details. It just doesn't work and has nothing to do
with my initial problem ;)
To the OP - can you use VNC? The VNC client runs on many OSes including
Windows and does accept X11 port forwarding.
No, we are limited to ssh and putty,

But this is getting way off-topic for this list now.

Thanks for all the answers
Thomas
 
T

Thomas Kellerer

Lew wrote on 17.03.2009 21:34:
That looks great, but there's nothing I found at that link that
answers my question about how you get non-GUI and GUI interfaces to
the same program.

Well you can start either the Swing GUI or a commandline version (it's a
different main class. If you want more details you can either check the source
code or contact me offline (see the support email on that site)

Regards
Thomas
 
M

Martin Gregorie

Are you sure PuTTY does not accept X11 forwarding?

I often use PuTTY to access some servers where I have shell accounts.
The configuration dialog has an option "Enable X11 forwarding". With it
checked, and the Cygwin X11 server running on my laptop, if I type e.g.
"xterm &" in the PuTTY window, an xterm, running on the server, displays
on my laptop.
I wouldn't do it last time I tried, but admittedly that was a long time
ago and on a W95 box.
 
M

Martin Gregorie

I checked on my system: LINES and COLUMNS are shell variables, and they
are not part of the environment.
I've only seen them set up by session initialisers that also load the
termcap definition into the TERMCAP shell variable to speed up curses
initialisation: before scanning /etc/termcap compliant versions of curses
check for the LINES, COLUMNS and TERMCAP variables. IIRC the ncurses
variant does this. I don't know where this behavior originated: my
somewhat tweaked Microware OS-9 v2.4 system, which runs on a 20 MHz 68020
uses it. It needs to - the OS-9 filing system is very robust but slow
since its unbuffered.

However, TERM presumably is, if it is
set. Martin Gregorie brought up the possibility of looking into
terminfo and termcap stuff, in the context of a library he thought might
help do that for you. I checked, and the library doesn't, but the idea
is still valid.
System.out and System.err are Printstreams, so they can output byte
arrays. Given that, I wonder why JCurses found it necessary to use native
code at all, let alone conio, which is DOS-specific. I missed that MS
lock-in or I would not have suggested using JCurses but, as Jon says, I
think the idea is OK. Parsing a termcap definition is pretty easy. I
haven't tried to pull a compiled terminfo definition apart.

TERM is always set for a *nix or *nix clone and unlikely to be set on a
Windows box, but that's easily enough fixed, especially as I seem to
remember that the DOS box defaults to 80 x 24. Of course you'd also have
to install a termcap file or terminfo database somewhere. You'd only need
a definition for the default ANSI terminal that's been part of DOS/
Windows since at least DOS 4. There's no convention about where to put
the termcap or terminfo databases under Windows but C:\etc sounds as good
a place as any provided the TERMINFO environment variable is honoured if
you go the terminfo way.
 
L

Lew

Jon said:
I looked at the source for version 0.9.5. It has native code for
accessing the console using "curses" and "conio" in the directory
/jcurses/src/native/.

I'm getting a chuckle out of seeing "curses" and "conio" in the same phrase.
 
A

Arne Vajhøj

Lew said:
I'm getting a chuckle out of seeing "curses" and "conio" in the same
phrase.

I am very skeptical about mixing curses and conio.

Well - actually I am skeptical about using conio. Period. Assuming
that it is not for a system running DOS, but since Java does not run
on DOS, then that seems as a good assumption.

Arne
 
J

Jon Gómez

Lew said:
I'm getting a chuckle out of seeing "curses" and "conio" in the same
phrase.

My Argentine father did not raise me with the language, and I had to
pick it up the hard way. However, I do appreciate a certain amount of
the irony of placing them together in the same sentence... :)

Jon.
 
R

Roedy Green

I tried several combinations of using Process.getInputStream() and
Process.getOutputStream() (and mingling with System.out and System.in) but
either I can't see the output or the output is not paged.

There are three approaches you could use:

1. spawn a BAT file with a command processor that does pipe
redirection. Not WORA.

2. capture the input. You need several threads to pull this off. Then
send page the output and write it to a file, or exec your page and
feed the output to it. You then have two execs running at once. You
have threads coming your ears. You have a loop reading the output of
one and writing the input of the other.

3, make your page splitting code a Java class and run it as part of
your mother program.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"America ships tons of sugar cookies to Denmark, and Denmark ships tons of sugar cookies to America. Wouldn’t it be more efficient just to swap recipes?"
~ Michael Pollan
 
A

Andreas Leitgeb

Thomas Kellerer said:
It's the customer's server which will refuse to do it (and which I cannnot
control). Don't ask me for details.

They may just simply have disallowed it per /etc/ssh/sshd_config.
It just doesn't work and has nothing to do with my initial problem ;)

Apropos that: have you seen my followup (to the firstpost) suggesting
to use "stty" or "less" in a shell with redirections from/to /dev/tty ?
 
T

Thomas Kellerer

Andreas Leitgeb, 18.03.2009 08:29:
Apropos that: have you seen my followup (to the firstpost) suggesting
to use "stty" or "less" in a shell with redirections from/to /dev/tty ?

Yes I have seen that, and I though I already tried that, but after looking at your example again it was a different situation. I tried spawning "sh -c stty" to retrieve the screensize but that didn't work for some reason. But I will try your solution for piping. That might also work on Windows as well.

But after all I think I'll simply pass the current screensize to my program during startup (from the shell script) and then do the paging "manually" in the program. Not as flexible because I will not know if a line wrapped in the terminal or not, potentially giving a wrong paging.

Regards
Thomas
 
N

Nigel Wade

Patricia said:
Are you sure PuTTY does not accept X11 forwarding?

It certainly does. It doesn't have any native X/Windows server built in, but it
can establish a tunnel to an already running X server.
I often use PuTTY to access some servers where I have shell accounts.
The configuration dialog has an option "Enable X11 forwarding". With it
checked, and the Cygwin X11 server running on my laptop, if I type e.g.
"xterm &" in the PuTTY window, an xterm, running on the server, displays
on my laptop.

Yep, that's how it works. It does rely on the server end allowing X11
port-forwarding. Some systems are configured to prevent this.
 
M

Martin Gregorie

It certainly does. It doesn't have any native X/Windows server built in,
but it can establish a tunnel to an already running X server.
OK, thats why I thought it didn't forward X11 - I have never had an X-
server running on a machine where I was using the PuTTY client.
 
N

Nigel Wade

Martin said:
OK, thats why I thought it didn't forward X11 - I have never had an X-
server running on a machine where I was using the PuTTY client.

It's sort of a requirement...

Otherwise what is the X client going to connect to?
 
A

Andreas Leitgeb

Thomas Kellerer said:
Andreas Leitgeb, 18.03.2009 08:29:
Yes I have seen that, and I though I already tried that, but after looking
at your example again it was a different situation. I tried spawning
"sh -c stty" to retrieve the screensize but that didn't work for some
reason. But I will try your solution for piping. That might also work
on Windows as well.

The essential piece is redirecting input/output from/to "/dev/tty",
which is simpler from the shell, than from Java. I wouldn't expect this
to work on windows, unless you have some "POSIX for windows" subsystem,
that knows to deal with "/dev/tty" on windows.
But after all I think I'll simply pass the current screensize to my program
during startup (from the shell script) and then do the paging "manually" in
the program. Not as flexible because I will not know if a line wrapped in
the terminal or not, potentially giving a wrong paging.

This reminds me of the variables "line" and "pagesize" that exist in
oracle's sqlplus (don't know much about others) and already govern the
output formatting: line wrapping and frequency of headings-repetitions.
If you go the way that the user has to specify these values, I'd set
them to reasonable defaults (80x24), and allow the user to change these
variables during the session.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top