in reply to Re^3: Fitting N Xterms On An X by Y screen
in thread Fitting N Xterms On An X by Y screen

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."