resize.pl revision d522f475
1#!/usr/bin/perl
2# $XFree86: xc/programs/xterm/vttests/resize.pl,v 1.1 2004/03/04 02:21:58 dickey Exp $
3#
4# -- Thomas Dickey (2004/3/3)
5# resize.sh rewritten into Perl for comparison.
6# See also Term::ReadKey.
7
8use IO::Handle;
9
10sub write_tty {
11	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
12	autoflush TTY 1;
13	print TTY @_;
14	close TTY;
15}
16
17sub get_reply {
18	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
19	autoflush TTY 1;
20	$old=`stty -g`;
21	system "stty raw -echo min 0 time 5";
22
23	print TTY @_;
24	my $reply=<TTY>;
25	close TTY;
26	system "stty $old";
27	return $reply;
28}
29
30sub csi_field {
31	my $first = @_[0];
32	my $second = @_[1];
33	$first =~ s/^[^0-9]+//;
34	while ( --$second > 0 ) {
35		$first =~ s/^[\d]+//;
36		$first =~ s/^[^\d]+//;
37	}
38	$first =~ s/[^\d]+.*$//;
39	return $first;
40}
41
42$original=get_reply("\x1b[18t");
43if ( $original =~ /\x1b\[8;\d+;\d+t/ ) {
44	$high=csi_field($original,2);
45	$wide=csi_field($original,3);
46	printf "parsed terminal size $high,$wide\n";
47} else {
48	die "Cannot get terminal size via escape sequence\n";
49}
50#
51$maximize=get_reply("\x1b[19t");
52if ( $maximize =~ /\x1b\[9;\d+;\d+t/ ) {
53	$maxhigh=csi_field($maximize,2);
54	$maxwide=csi_field($maximize,3);
55	$maxhigh != 0 or $maxhigh = $high * 2;
56	$maxwide != 0 or $maxwide = $wide * 2;
57	printf "parsed terminal maxsize $maxhigh,$maxwide\n";
58} else {
59	die "Cannot get terminal size via escape sequence\n";
60}
61
62sub catch_zap {
63	$zapped++;
64}
65$SIG{INT} = \&catch_zap;
66$SIG{QUIT} = \&catch_zap;
67$SIG{KILL} = \&catch_zap;
68$SIG{HUP} = \&catch_zap;
69$SIG{TERM} = \&catch_zap;
70
71$w=$wide;
72$h=$high;
73$a=1;
74$zapped=0;
75while ( $zapped == 0 )
76{
77#	sleep 1
78	printf "resizing to $h by $w\n";
79	write_tty("\x1b[8;$h;$w" . "t");
80	if ( $a == 1 ) {
81		if ( $w == $maxwide ) {
82			$h += $a;
83			if ( $h = $maxhigh ) {
84				$a = -1;
85			}
86		} else {
87			$w += $a;
88		}
89	} else {
90		if ( $w == $wide ) {
91			$h += $a;
92			if ( $h = $high ) {
93				$a=1;
94			}
95		} else {
96			$w += $a;
97		}
98	}
99}
100write_tty($original);
101