forked from penma/guckets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myterm.pm
74 lines (60 loc) · 1.39 KB
/
myterm.pm
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
package myterm;
use strict;
use warnings;
use Term::ReadLine;
use Term::Size;
use POSIX;
sub set {
my $termios = new POSIX::Termios;
$termios->getattr(0);
$termios->setlflag($termios->getlflag | $_[0]);
$termios->setattr(0, POSIX::TCSANOW);
}
sub unset {
my $termios = new POSIX::Termios;
$termios->getattr(0);
$termios->setlflag($termios->getlflag & ~$_[0]);
$termios->setattr(0, POSIX::TCSANOW);
}
sub readkey {
my ($stream, $current);
sysread(STDIN, $current, 1) or return undef;
$stream .= $current;
# translate some crap into something that sucks less
$stream =~ y/\cM/\cJ/;
# fully resolve an escape sequence
if ($current eq "\e") {
sysread(STDIN, $current, 1);
$current =~ y/O/\[/;
$stream .= $current;
if ($current eq "[") { # application/cursor keys
sysread(STDIN, $current, 1);
$stream .= $current;
if ($current !~ /[ABCD]/) { # probably one of home delete..
sysread(STDIN, $current, 1);
$stream .= $current;
# don't bother sanitizing further
}
}
}
return $stream;
}
sub readline {
my $term = Term::ReadLine->new('Screenedit');
term_set(POSIX::ECHO);
my $line = $term->readline($_[0]);
term_unset(POSIX::ECHO);
return $line;
}
sub dimensions {
my ($w, $h) = Term::Size::chars *STDOUT{IO};
($w, $h) = (80, 24) if (!defined($w) or $w == 0);
return ($w, $h);
}
sub width {
return (dimensions())[0];
}
sub height {
return (dimensions())[1];
}
1;