#!/usr/bin/perl use strict; use Curses; use Term::ReadKey; use JSONConnection; #use Net::IRC3::Client::Connection; our $HEIGHT; our $WIDTH; our $MAINWIN; our $winbuffer = []; our $stdiowatcher; our $inputbuffer; our $js; our $window_resize = 0; $SIG{WINCH} = sub { my ($wc, $hc, $wpx, $hpx) = GetTerminalSize (); unshift @$winbuffer, "SIZE: $wc $hc $wpx $hpx"; resizeterm ($hc, $wc); $MAINWIN->resize ($hc, $wc); $MAINWIN->erase; refresh (); }; sub color2attr { my $c = $_[0]; my $attr; if ($c <= 63) { $attr = COLOR_PAIR($c); } elsif ($c <= 127) { $c -= 64; $attr = COLOR_PAIR($c) | A_BOLD; } elsif ($c <= 191) { $c -= 128; $attr = COLOR_PAIR($c) | A_UNDERLINE; } else { $c -= 192; $attr = COLOR_PAIR($c) | A_UNDERLINE | A_BOLD; } $attr } sub refresh { my ($onlyinput) = @_; unless ($onlyinput) { $MAINWIN->erase; my (@outbuf) = @$winbuffer; my $line = $LINES - 2; while ($line >= 0) { my $lx = shift @outbuf; if ((ref $lx) eq "ARRAY") { my @lcont = @$lx; my $li = 0; while (@lcont) { my ($color, $str) = (shift @lcont, shift @lcont); my $sl = (length $str) > $COLS ? $COLS : length $str; $MAINWIN->attron (color2attr($color)); $MAINWIN->addnstr ($line, $li, (substr $str, 0, $sl), $sl); $li += $sl; $MAINWIN->attroff (color2attr($color)); } } else { my $sl = length $lx > $COLS ? $COLS : length $lx; $MAINWIN->addnstr ($line, 0, (substr $lx, 0, $sl), $sl); } $line--; } } $MAINWIN->move ($LINES - 1, 0); $MAINWIN->clrtoeol (); $MAINWIN->addstr ($LINES - 1, 0, $inputbuffer); $MAINWIN->move ($LINES - 1, 0); $MAINWIN->refresh; } sub printline { unshift @$winbuffer, $_[0]; refresh (); } sub handle_input { my $c = $MAINWIN->getch; if ($c == KEY_BACKSPACE) { substr $inputbuffer, -1, 1, ''; } elsif ($c eq "\n") { $js->send_data ({type => 'public', con => "localhost:6667", channel => '#blob', message => $inputbuffer }); $inputbuffer = ""; } else { if ($c & 0xFF) { printline ("SPECIAL[$c]"); } else { $inputbuffer .= $c; } } refresh (); } sub init_curses { my $win = $MAINWIN = Curses->new; cbreak; noecho; clear; $win->keypad (1); if (has_colors ()) { start_color (); use_default_colors (); my $colors = ""; my (@ansi_tab) = qw/0 4 2 6 1 5 3 7/; for (my $i = 1; $i < $COLOR_PAIRS; $i++) { # $colors .= "$i=". ($ansi_tab[$i & 7]).":".($ansi_tab[$i >> 3])."|"; init_pair ($i, $ansi_tab[$i & 7], $i <= 7 ? -1 : $ansi_tab[$i >> 3]); } # init_color (63, 0, 0); # init_pair (63, 0, -1); printline ("COLOR S $COLOR_PAIRS $colors"); } $win->attron (A_NORMAL); # $stdscr->getmaxyx ($HEIGHT, $WIDTH); # my ($y, $x); # $win->getmaxyx ($y, $x); # $win->clear; # $win->move (0, 0); # $win->clrtobot; # $win->refresh; # printline ("SIZE[$HEIGHT $WIDTH | $y $x]"); $stdiowatcher = AnyEvent->io (fh => \*STDIN, poll => "r", cb => sub { handle_input; }); } my $c = AnyEvent->condvar; my $d = 0; my $wt; sub timer { $wt = AnyEvent->timer (after => 3, cb => sub { printline ("TEST $d"); $d++; timer (); }); } timer (); $js = JSONClientConnection->new (packet_cb => sub { my ($js, $data) = @_; require JSON::Syck; printline (JSON::Syck::Dump ($data)); }); $js->connect (localhost => 1236); init_curses (); printline ("TEST!!!"); { my $l; for (my $i = 0; $i <= 256; $i++) { push @$l, $i, (sprintf "[%3d]", $i); if (($i + 1) % 8 == 0) { printline ($l); $l = []; } } printline ($l); } printline ("TESTE!!!"); $c->wait; endwin;