[c5c522c] | 1 | #!/usr/local/bin/perl -w |
---|
| 2 | |
---|
| 3 | use IO::Socket; |
---|
| 4 | use Fcntl; |
---|
| 5 | |
---|
| 6 | if($#ARGV < 0) |
---|
| 7 | { |
---|
| 8 | print "Usage: tail.pl file\n"; |
---|
| 9 | exit; |
---|
| 10 | } |
---|
| 11 | $filename = shift @ARGV; |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | # Connect to the server... |
---|
| 15 | $remote = IO::Socket::INET->new( |
---|
| 16 | Proto => "tcp", |
---|
| 17 | PeerAddr => "localhost", |
---|
| 18 | PeerPort => "13666", |
---|
| 19 | ) |
---|
| 20 | || die "Cannot connect to LCDproc port\n"; |
---|
| 21 | |
---|
| 22 | # Make sure our messages get there right away |
---|
| 23 | $remote->autoflush(1); |
---|
| 24 | |
---|
| 25 | sleep 1; # Give server plenty of time to notice us... |
---|
| 26 | |
---|
| 27 | print $remote "hello\n"; |
---|
| 28 | my $lcdconnect = <$remote>; |
---|
| 29 | print $lcdconnect; |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | # Turn off blocking mode... |
---|
| 33 | fcntl($remote, F_SETFL, O_NONBLOCK); |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | # Set up some screen widgets... |
---|
| 37 | print $remote "client_set name {Tail ($filename)}\n"; |
---|
| 38 | print $remote "screen_add tail\n"; |
---|
| 39 | print $remote "screen_set tail name {Tail $filename}\n"; |
---|
| 40 | print $remote "widget_add tail title title\n"; |
---|
| 41 | print $remote "widget_set tail title {Tail: $filename}\n"; |
---|
| 42 | print $remote "widget_add tail 1 string\n"; |
---|
| 43 | print $remote "widget_add tail 2 string\n"; |
---|
| 44 | print $remote "widget_add tail 3 string\n"; |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | # These define the visible part of the file... |
---|
| 48 | $top = 3; # How far from the end of the file should we start? |
---|
| 49 | $lines = 3; # How many lines to display? |
---|
| 50 | $left = 1; # Left/right scrolling position, |
---|
| 51 | $width = 20; # and number of characters per line to show |
---|
| 52 | |
---|
| 53 | $slow = 1; # Should we pause after the current frame? |
---|
| 54 | |
---|
| 55 | # Forever, we should do stuff... |
---|
| 56 | while(1) |
---|
| 57 | { |
---|
| 58 | # Handle input... (spew it to the console) |
---|
| 59 | # Also, certain keys scroll the display |
---|
| 60 | while(defined($input = <$remote>)) { |
---|
| 61 | print $input; |
---|
| 62 | |
---|
| 63 | $slow = -10; |
---|
| 64 | # Make sure we handle each line... |
---|
| 65 | @lines = split(/\n/, $input); |
---|
| 66 | foreach $line (@lines) |
---|
| 67 | { |
---|
| 68 | if($line =~ /^key (\S)/) # Keypresses are useful. |
---|
| 69 | { |
---|
| 70 | $key = $1; |
---|
| 71 | if($key eq "G") { $top++; } |
---|
| 72 | if($key eq "H") |
---|
| 73 | { |
---|
| 74 | if($top > $lines) {$top--; } |
---|
| 75 | } |
---|
| 76 | if($key eq "F") { $left++; } |
---|
| 77 | if($key eq "E") |
---|
| 78 | { |
---|
| 79 | if($left > 1) {$left--; } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | # Now, show what the file contains, if anything... |
---|
| 86 | if( -f $filename ) |
---|
| 87 | { |
---|
| 88 | # Grab some text. |
---|
| 89 | $right = $left + $width - 1; |
---|
| 90 | $text = `tail -$top $filename | head -$lines | cut -c$left-$right`; |
---|
| 91 | @lines = split(/\n/, $text); |
---|
| 92 | # Now, display that text... |
---|
| 93 | for($i=0; $i<$lines; $i++) |
---|
| 94 | { |
---|
| 95 | $j = $i + 1; |
---|
| 96 | $k = $i + 2; |
---|
| 97 | if($#lines < $i) { $line = " "; } # Avoid blank lines |
---|
| 98 | else { $line = $lines[$i]; } |
---|
| 99 | if(!($line =~ /\S/)) { $line = " ";} |
---|
| 100 | print $remote "widget_set tail $j 1 $k {$line }\n"; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | else # If the file is unreadable, show an error |
---|
| 104 | { |
---|
| 105 | print $remote "widget_set tail one 1 2 {$filename}\n"; |
---|
| 106 | print $remote "widget_set tail two 1 3 {doesn't exist.}\n"; |
---|
| 107 | print $remote "widget_set tail three 1 4 { }\n"; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | # And wait a little while before we show stuff again. |
---|
| 111 | if($slow > 0) {sleep 1; $slow++;} |
---|
| 112 | elsif($slow > 4) {sleep 2; $slow++;} |
---|
| 113 | elsif($slow > 64) {sleep 4; } |
---|
| 114 | else {$slow++;} |
---|
| 115 | # The "slow" thing just lets us have a better response time |
---|
| 116 | # while the user is pressing keys... But while the user |
---|
| 117 | # is inactive, it gradually decreases update frequency. |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | close ($remote) || die "close: $!"; |
---|
| 121 | exit; |
---|