Author: bdonlan
Date: 2006-03-11 20:34:10 -0500 (Sat, 11 Mar 2006)
New Revision: 973
Added:
trunk/perl/term-visual/
trunk/perl/term-visual/lib/
trunk/perl/term-visual/lib/Term/
trunk/perl/term-visual/lib/Term/Visual.pm
trunk/perl/term-visual/lib/Term/Visual/
trunk/perl/term-visual/lib/Term/Visual/Window.pm
trunk/perl/term-visual/test.pl
Log:
Commit early stages of perl term-visual reimplementation
Added: trunk/perl/term-visual/lib/Term/Visual/Window.pm
===================================================================
--- trunk/perl/term-visual/lib/Term/Visual/Window.pm 2006-03-05 04:47:40 UTC
(rev 972)
+++ trunk/perl/term-visual/lib/Term/Visual/Window.pm 2006-03-12 01:34:10 UTC
(rev 973)
@@ -0,0 +1,92 @@
+# vim: set ts=4 sw=4 expandtab si ai sta tw=100:
+# This module is copyrighted, see end of file for details.
+package Term::Visual::Window;
+use strict;
+use warnings;
+use Carp;
+
+our $VERSION = 0.01;
+
+use Curses;
+
+sub _new {
+ my ($class, $idx) = @_;
+ $class = ref $class || $class || croak;
+
+ my $self = {
+ idx => $idx,
+ };
+ bless $self, $class;
+ $self->_winch;
+ return $self;
+}
+
+sub _winch {
+ $_[0]->{window} = newwin((LINES) - 2, COLS, 0, 0);
+ $_[0]->{window}->scrollok(1);
+}
+
+sub println {
+ my ($self, $line) = @_;
+ $self->{window}->scroll;
+ $self->{window}->addstr((LINES) - 3, 0, $line);
+}
+
+sub _render {
+ $_[0]->{window}->noutrefresh;
+}
+
+sub destroy {
+ Term::Visual::_destroy_window($_[0]);
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Term::Visual::Window - description
+
+=head1 SYNOPSIS
+
+ use Term::Visual::Window
+ # Small code example.
+
+=head1 DESCRIPTION
+
+FIXME
+
+=head1 METHODS
+
+Term::Visual::Window implements the following methods:
+
+=head1 BUGS
+
+Describe how to report bugs, and list any known bugs.
+
+=head1 AUTHOR
+
+<<AUTHOR>>, E<lt><<EMAIL>>E<gt>
+
+=head1 SEE ALSO
+
+L<perl(1)>, and any other related modules / manpages / websites.
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (C) 2006 by <<AUTHOR>>. All Rights Reserved.
+
+This module is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This module is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this module; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
Property changes on: trunk/perl/term-visual/lib/Term/Visual/Window.pm
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/perl/term-visual/lib/Term/Visual.pm
===================================================================
--- trunk/perl/term-visual/lib/Term/Visual.pm 2006-03-05 04:47:40 UTC (rev
972)
+++ trunk/perl/term-visual/lib/Term/Visual.pm 2006-03-12 01:34:10 UTC (rev
973)
@@ -0,0 +1,240 @@
+# vim: set ts=4 sw=4 expandtab si ai sta tw=100:
+# This module is copyrighted, see end of file for details.
+package Term::Visual;
+use strict;
+use warnings;
+use Carp;
+use Curses;
+use Data::Dumper;
+
+use Term::Visual::Window;
+
+our $VERSION = 0.01;
+
+require Exporter;
+our @ISA = 'Exporter';
+
+our @EXPORT_OK = qw( new_window term_commit term_keystroke
+ edit_insert_at edit_adjust_curs edit_delete_range);
+our %EXPORT_TAGS = (all => [EMAIL PROTECTED]);
+
+our $edit_win;
+our $status_win;
+
+sub init {
+ initscr; cbreak; noecho; nonl;
+ intrflush(stdscr, 0);
+ keypad(stdscr, 1);
+
+ select_window(new_window());
+ on_winch();
+}
+
+### WINDOW MANAGEMENT
+our @windows;
+our $current_window;
+
+sub new_window {
+ my $win = Term::Visual::Window->_new(scalar @windows);
+ push @windows, $win;
+
+ return $win;
+}
+
+sub select_window {
+ my $win = shift;
+ $current_window = $win;
+}
+
+sub get_window_index {
+ my $idx = shift;
+ return $windows[$idx];
+}
+
+sub _destroy_window {
+ my $win = shift;
+ splice @windows, $win->{idx}, 1;
+ foreach (0..$#windows) {
+ $windows[$_]{idx} = $_;
+ }
+ if ($current_window == $win) {
+ $current_window = $windows[$win->{idx} - 1];
+ }
+}
+
+sub on_winch() {
+ my $y_ext = LINES;
+ my $x_ext = COLS;
+ $edit_win->delwin if defined $edit_win;
+ $edit_win = newwin(1, $x_ext, $y_ext - 1, 0);
+
+ $status_win->delwin if defined $status_win;
+ $status_win = newwin(1, $x_ext, $y_ext - 2, 0);
+
+ set_status('');
+ update_edit();
+ confess unless $current_window;
+ $current_window->_winch if defined $current_window;
+}
+
+our $status_line = '';
+
+sub set_status {
+ my $str = shift;
+ $status_line = $str;
+}
+
+sub update_status {
+ $status_win->erase;
+ $status_win->addstr(0, 0, $status_line);
+}
+
+our ($edit_string, $edit_curs_pos, $edit_disp_pos) = ('', 0, 0);
+
+sub update_edit {
+ # basic sanity checks on edit buffer size
+ $edit_curs_pos = int $edit_curs_pos;
+ $edit_disp_pos = int $edit_disp_pos;
+ if ($edit_curs_pos > length $edit_string) {
+ $edit_curs_pos = length $edit_string;
+ }
+ if ($edit_curs_pos < 0) {
+ $edit_curs_pos = 0;
+ }
+ if ($edit_disp_pos > length $edit_string
+ || $edit_disp_pos > $edit_curs_pos
+ || $edit_curs_pos - $edit_disp_pos > COLS
+ )
+ {
+ $edit_disp_pos = $edit_curs_pos - int((COLS) / 2);
+ $edit_disp_pos = 0 if $edit_disp_pos < 0;
+ }
+ $edit_win->erase;
+ $edit_win->addstr(0, 0, substr($edit_string, $edit_disp_pos, (COLS) - 1));
+ $edit_win->move(0, $edit_curs_pos - $edit_disp_pos);
+}
+
+sub edit_adjust_curs {
+ my ($offset) = @_;
+ $edit_curs_pos += $offset;
+ update_edit;
+}
+
+sub edit_delete_range {
+ my ($start, $length) = @_;
+ substr $edit_string, $start, $length, '';
+ update_edit;
+}
+
+sub edit_insert_at {
+ my ($pos, $string) = @_;
+ if ($edit_curs_pos > $pos) {
+ $edit_curs_pos += length $string;
+ }
+ substr $edit_string, $pos, 0, $string;
+ update_edit;
+}
+
+my %ks = (
+ KEY_LEFT, sub { edit_adjust_curs(-1); },
+ KEY_RIGHT, sub { edit_adjust_curs(1); },
+ KEY_BACKSPACE, sub {
+ edit_delete_range($edit_curs_pos - 1, 1);
+ edit_adjust_curs(-1);
+ },
+ KEY_DC, sub {
+ edit_delete_range($edit_curs_pos, 1);
+ },
+ KEY_HOME, sub {
+ $edit_curs_pos = 0;
+ update_edit;
+ },
+ KEY_END, sub {
+ $edit_curs_pos = length $edit_string;
+ update_edit;
+ },
+ "\r", sub {
+ $current_window->println($edit_string);
+ $edit_string = '';
+ update_edit;
+ },
+ '^L', sub {
+ stdscr->erase();
+ doupdate();
+
+
+);
+
+sub _default_key {
+ my ($key) = @_;
+ edit_insert_at($edit_curs_pos, $key);
+ edit_adjust_curs(1);
+}
+
+sub term_keystroke {
+ my $key = shift;
+ my $handler = \&_default_key;
+ set_status(Dumper $key);
+ if ($ks{$key}) {
+ $handler = $ks{$key};
+ }
+ $handler->($key);
+}
+
+sub term_commit {
+ $current_window->_render;
+ $status_win->noutrefresh;
+ $edit_win->noutrefresh;
+ doupdate;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Term::Visual - description
+
+=head1 SYNOPSIS
+
+ use Term::Visual
+ # Small code example.
+
+=head1 DESCRIPTION
+
+FIXME
+
+=head1 METHODS
+
+Term::Visual implements the following methods:
+
+=head1 BUGS
+
+Describe how to report bugs, and list any known bugs.
+
+=head1 AUTHOR
+
+<<AUTHOR>>, E<lt><<EMAIL>>E<gt>
+
+=head1 SEE ALSO
+
+L<perl(1)>, and any other related modules / manpages / websites.
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (C) 2006 by <<AUTHOR>>. All Rights Reserved.
+
+This module is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This module is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this module; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
Property changes on: trunk/perl/term-visual/lib/Term/Visual.pm
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/perl/term-visual/test.pl
===================================================================
--- trunk/perl/term-visual/test.pl 2006-03-05 04:47:40 UTC (rev 972)
+++ trunk/perl/term-visual/test.pl 2006-03-12 01:34:10 UTC (rev 973)
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Term::Visual qw(:all);
+use Curses;
+
+$SIG{INT} = sub { endwin; exit 0; };
+$SIG{__DIE__} = sub { endwin; die @_; };
+
+Term::Visual::init();
+
+$SIG{WINCH} = \&Term::Visual::on_winch;
+Term::Visual::set_status("=== This be a status bar, yo");
+
+while (1) {
+ term_commit;
+ term_keystroke(getch());
+}
+
+endwin;
Property changes on: trunk/perl/term-visual/test.pl
___________________________________________________________________
Name: svn:eol-style
+ native
_______________________________________________
Haver-commits mailing list
[email protected]
https://mail.gna.org/listinfo/haver-commits