File: cursor.pl

package info (click to toggle)
xterm 406-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,372 kB
  • sloc: ansic: 87,522; perl: 9,847; sh: 5,528; makefile: 843; xml: 46; sed: 11
file content (184 lines) | stat: -rwxr-xr-x 4,502 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env perl
# $XTermId: cursor.pl,v 1.8 2007/12/03 00:56:29 tom Exp $
# -----------------------------------------------------------------------------
# Copyright 2007 by Thomas E. Dickey
#
#                         All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
# Read a file (or pipe from a program) and move the cursor around the screen
# in response to h,j,k,l commands so we can see the colors that affect the
# cursor.  Exit on 'q'.  Do forward/backward paging to \E[J markers
# in the data with n,p.  Ignore other characters.
#
# Use this rather than, say, a curses program since it is much easier to
# construct a particular screen using 'script' or echo commands than to
# guarantee the same screen with curses' optimization.

use strict;

use Getopt::Std;
use IO::Handle;

our ( $opt_x );
our ( $row_max, $col_max );
our $old_stty;
our $text_blob;
our $text_1st;
our $text_2nd;
our $text_chop = qw/^\x1b\[H\x1b\[J/;
our $text_mark = "\x1b[H\x1b[J";

sub get_screensize() {
	my @reply = `resize -u`;
	chomp @reply;
	for my $n (0..$#reply) {
		if ( $reply[$n] =~ /=/ ) {
			my $value = $reply[$n];
			$value =~ s/^.*=//;
			if ( $reply[$n] =~ /^COLUMNS.*/ ) {
				$col_max = $value;
			} else {
				$row_max = $value;
			}
		}
	}
}

sub end_cursor($) {
	close TTY;
	system "stty $old_stty";
	print $_[0];
	exit;
}

sub begin_cursor() {
	open TTY, "+</dev/tty" or end_cursor("Cannot open /dev/tty\n");
	autoflush TTY 1;
	$old_stty=`stty -g`;
	system "stty raw -echo min 0 time 1";
}

sub beep() {
	printf "\007";
}

sub get_char() {
	my $reply;
	do {
		$reply=<TTY>;
		# printf "get_char\r\n";
	} while (not defined $reply);
	return $reply;
}

sub move_to($$) {
	my $y = $_[0];
	my $x = $_[1];
	if ( $y < 0 ) {
		$y = 0;
	} elsif ( $x < 0 ) {
		$x = 0;
	} elsif ( $y >= $row_max ) {
		$y -= 1;
	} elsif ( $x >= $col_max ) {
		$x -= 1;
	} else {
		printf "\x1b[%d;%dH", $y + 1, $x + 1;
	}
	return ( $y, $x );
}

sub vxt_cursor() {
	my $ch;
	my $x = 0;
	my $y = 0;
	my @pages = split $text_chop, $text_blob;
	my $page = 1;

my_page:
	move_to ($y, $x);
	printf "%s", $text_mark . $pages[$page];
	move_to ($y, $x);
my_loop:
	for (;;) {
		$ch = get_char();
		if ( $ch eq "q") {
			last my_loop;
		} elsif ( $ch eq "h" ) {
			($y, $x) = move_to($y, $x - 1);
		} elsif ( $ch eq "j" ) {
			($y, $x) = move_to($y + 1, $x);
		} elsif ( $ch eq "k" ) {
			($y, $x) = move_to($y - 1, $x);
		} elsif ( $ch eq "l" ) {
			($y, $x) = move_to($y, $x + 1);
		} elsif ( $ch eq "n" ) {
			if ( $page < $#pages ) {
				$page += 1;
				goto my_page;
			} else {
				beep();
			}
		} elsif ( $ch eq "p" ) {
			if ( $page > 1 ) {
				$page -= 1;
				goto my_page;
			} else {
				beep();
			}
		} else {
			beep();
			# printf "got:%s\r\n", $ch;
		}
	}
}

sub load_text($) {
	my $source = $_[0];
	my $text;
	if ( defined($opt_x) ) {
		$text = `$source`;
	} else {
		$text = `cat $source`;
	}
	$text =~ s/\n/\r\n/g;
	if ( $text !~ $text_chop ) {
		$text = $text_mark . $text;
	}
	$text_blob = $text_blob . $text;
}

&getopts('x') || die();

while ( $#ARGV >= 0 ) {
	load_text ( shift @ARGV );
}

get_screensize();
begin_cursor();
vxt_cursor();
end_cursor("Done\n");