15.4. Determining Terminal or Window Size15.4.1. ProblemYou need to know the size of the terminal or window. For instance, you want to format text so that it doesn't pass the righthand boundary of the screen. 15.4.2. SolutionEither use the ioctl described in Recipe 12.17, or else use the CPAN module Term::ReadKey: use Term::ReadKey; ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize( ); 15.4.3. DiscussionGetTerminalSize returns four elements: the width and height in characters and the width and height in pixels. If the operation is unsupported for the output device (for instance, if output has been redirected to a file), it returns an empty list. Here's how you'd graph the contents of @values, assuming no value is less than 0: use Term::ReadKey; ($width) = GetTerminalSize( ); die "You must have at least 10 characters" unless $width >= 10; $max = 0; foreach (@values) { $max = $_ if $max < $_; } $ratio = ($width-10)/$max; # chars per unit foreach (@values) { printf("%8.1f %s\n", $_, "*" x ($ratio*$_)); } 15.4.4. See AlsoThe documentation for the Term::ReadKey module from CPAN; Recipe 12.17 Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|