This seems to a fairly reasonable job for upto 26 windows(1) and most reasonable combinations of screen width and height (supplied on the command line). The code could definitely do with refactoring, but it is always going to be a bit messy as there are so many corner and edge cases. Literally.

Don't specifiy a width greater than you own terminal-1 otherwise it just looks a mess.

#! perl -sw use 5.010; use strict; use Data::Dump qw[ pp ]; my( $w, $h ) = @ARGV; sub display { my @screen = map ' ' x $w . '|', 0 .. $h; my $char = 'A'; for my $term ( @_ ) { substr( $screen[ $_ ], $term->[ 0 ], $term->[ 1 ] ) = $char x $term->[ 1 ] for $term->[ 2 ] .. $term->[ 2 ] + $term->[ 3 ]; ++$char; } # system 'cls'; say for @screen; } for my $n ( 1 .. 26 ) { my @xterms; my $xn = int( $n ** 0.5 ); my $yn = int( $n / $xn ); my $rem = $n - ($xn * $yn); my $deltaX= int( $w / $xn ); my $deltaY = int( $h / ( $yn + ( $rem ? 1 : 0 ) ) ); my( $tlx, $tly ) = ( 0, 0 ); for my $t ( 1 .. $n ) { push @xterms, [ $tlx, $deltaX, $tly, $deltaY ]; $tlx += $deltaX; if( ( $tlx + $deltaX ) > $w ) { $xterms[ -1 ][ 1 ] = $w - $xterms[ -1 ][ 0 ]; $tlx = 0; $tly += $deltaY; my $xLeft = $n - $t; if( $xLeft and $xLeft <= $xn ) { $deltaX = int( $w / $xLeft ); $deltaY = $h - $tly; } } } display( @xterms ); <STDIN>; } __END__ C:\test>xterms 20 9 AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAABBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAABBBBBBBBBB| AAAAAAAAAAAAAAAAAAAA| AAAAAAAAAABBBBBBBBBB| BBBBBBBBBBBBBBBBBBBB| AAAAAAAAAABBBBBBBBBB| BBBBBBBBBBBBBBBBBBBB| CCCCCCCCCCDDDDDDDDDD| BBBBBBBBBBBBBBBBBBBB| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCCCCCCCCCCC| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCCCCCCCCCCC| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCCCCCCCCCCC| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCCCCCCCCCCC| CCCCCCCCCCDDDDDDDDDD| AAAAAAAAAABBBBBBBBBB| AAAAAAAAAABBBBBBBBBB| AAAAAAAAAABBBBBBBBBB| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCDDDDDDDDDD| CCCCCCCCCCDDDDDDDDDD| EEEEEEEEEEEEEEEEEEEE| EEEEEEEEEEEEEEEEEEEE| EEEEEEEEEEEEEEEEEEEE| EEEEEEEEEEEEEEEEEEEE|

(1) The demo gets way more complex once you move to more that a single character for each screen.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^4: Fitting N Xterms On An X by Y screen by BrowserUk
in thread Fitting N Xterms On An X by Y screen by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.