display_userlist.pl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/perl
  2. use strict;
  3. use Term::ReadKey;
  4. use Term::ANSIColor;
  5. use IO::Socket;
  6. my $path = "$ENV{HOME}/.irssi/sock";
  7. my $umap = '@%+ ';
  8. my $offset = 0;
  9. my $lastoutput = "";
  10. my %modes = (
  11. '@' => 'green',
  12. '%' => 'blue',
  13. '+' => 'yellow',
  14. ' ' => '',
  15. );
  16. $|++;
  17. my($width, $height) = GetTerminalSize;
  18. $height -= 2;
  19. $SIG{WINCH} = sub {
  20. ($width, $height) = GetTerminalSize;
  21. $height -= 2;
  22. };
  23. ReadMode 4;
  24. END { ReadMode 0; }
  25. my $socket = IO::Socket::UNIX->new($path) or die $!;
  26. #for(;;) {
  27. print $socket "userlist\n";
  28. my($channel, $mode, $userlist) = split / /, <$socket>, 3;
  29. exit unless defined $channel;
  30. chomp $userlist;
  31. my $output = "\n" . substr("$channel($mode)", 0, $width);
  32. my @users;
  33. for(sort { # Adapted from CGI:IRC
  34. return lc $a->[1] cmp lc $b->[1] if $a->[0] eq $b->[0];
  35. return index($umap, $a->[0]) <=> index($umap, $b->[0]);
  36. } map(/^[@%+]/ ? [substr($_, 0, 1), substr($_, 1)] : [" ", $_],
  37. split(/ /, $userlist))) {
  38. if(1 + length($_->[1] > $width)) {
  39. $_->[1] = substr($_->[1], 0, $width - 1);
  40. }
  41. push @users, color($modes{$_->[0]}) . $_->[0] . color('reset') . $_->[1];
  42. }
  43. $output .= "\n$_" for @users[$offset .. ($offset + $height)];
  44. if($output ne $lastoutput) {
  45. $lastoutput = $output;
  46. print $output;
  47. }
  48. my $key = ReadKey(1);
  49. if($key eq "\003" or $key eq 'q') { exit; }
  50. if($key eq '' and ReadKey(-1) eq '[') {
  51. $key = ReadKey(-1);
  52. if($key eq 'A') {#up
  53. $offset -= 1;
  54. }elsif($key eq 'B') {#down
  55. $offset += 1;
  56. }elsif($key eq '5') {# Pgup
  57. $offset -= $height;
  58. }elsif($key eq '6') {# Pgdn
  59. $offset += $height;
  60. }
  61. $offset = $#users - $height if $offset > $#users - $height;
  62. $offset = 0 if $offset < 0;
  63. }elsif($key eq ' ') {# spacebar
  64. # force refresh
  65. $lastoutput = "";
  66. }elsif($key eq 'r') { #re-exec, useful for testing
  67. exec("perl", $0);
  68. }
  69. #}