humble has asked for the wisdom of the Perl Monks concerning the following question:

As i have an interactive PERL app., working in text mode (in a terminal window), and it outputs new messages in the place of previous ones (it first clears the space of the current line, then prints new message) - i need to set the size (in char.s) of the line (as the terminal window size changes some times). So, i set it manually, and therefore the app. clears the line right. Now, question is, how i can automate the seeting of the line size (in char.s): how the app. itself can measure current line size (in char.s)? - For without the exact size set, the line will be chenged to the next line - thus wasting one line every time new message comes OR it will not clear all the previous message either. Thank you for your time/help.
  • Comment on To find out console line size in char.s .

Replies are listed 'Best First'.
Re: To find out console line size in char.s .
by robby_dobby (Hermit) on Mar 13, 2014 at 05:40 UTC
    Hello humble,

    I'm not sure I understand your question. Can you show us some code demonstrating what you are trying to do? Without that, I have a few guesses to get at what you need:

    • If you have Term::ReadKey installed, you can use GetTerminalSize to hint at the terminal width and scale down your line length to some fraction of it. Here's the reference to that sub in the module.
    • For a nearly useless answer: how about my $len = scalar length $oldmsg? :-)
Re: To find out console line size in char.s .
by johngg (Canon) on Mar 13, 2014 at 09:27 UTC

    Expanding on robby_dobby's answer, if the terminal is resized it should generate a $SIG{ WINCH } so install a handler to recalculate terminal size on receipt of that signal.

    $SIG{ WINCH } = sub { # get new terminal size here };

    I hope this is helpful.

    Cheers,

    JohnGG

      Yes, but note that this may not be very portable:

      use Config; my @names = split ' ', $Config{sig_name}; print grep /WINCH/, @names;

      This does not print anything on Windows, but gives me results on cygwin and Linux(Slackware). To OP: As always, read the documentation (man 7 signal). This SO post may also shed some light on SIGWINCH for Windows OSes.

Re: To find out console line size in char.s .
by Anonymous Monk on Mar 13, 2014 at 12:08 UTC
    Isn't there some "clear to end-of-line" escape sequence?