there's no reason for window size computation to be linked to the
authorespie <espie@openbsd.org>
Thu, 23 Jun 2016 16:11:23 +0000 (16:11 +0000)
committerespie <espie@openbsd.org>
Thu, 23 Jun 2016 16:11:23 +0000 (16:11 +0000)
ProgressMeter, so make it available from state.
- computation is lazy, so no runtime difference;
- don't bother setting a SIG{WINCH} on non-tty;
- progressmeter already has access to state;
- do an empty window_size_changed in base state, override it for
addcreatedelete to inform the progressmeter.

to be reused in dpb...

usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm
usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm
usr.sbin/pkg_add/OpenBSD/ProgressMeter/Term.pm
usr.sbin/pkg_add/OpenBSD/State.pm

index e61677d..f6af4b5 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: AddCreateDelete.pm,v 1.37 2016/06/15 15:40:13 espie Exp $
+# $OpenBSD: AddCreateDelete.pm,v 1.38 2016/06/23 16:11:23 espie Exp $
 #
 # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org>
 #
@@ -99,6 +99,11 @@ sub is_interactive
        return shift->{interactive}->is_interactive;
 }
 
+sub window_size_changed
+{
+       shift->{progressmeter}->compute_playfield;
+}
+
 sub confirm
 {
        my $self = shift;
index 17ffdc6..8cb613b 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: ProgressMeter.pm,v 1.48 2016/06/15 15:31:09 espie Exp $
+# $OpenBSD: ProgressMeter.pm,v 1.49 2016/06/23 16:11:23 espie Exp $
 #
 # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
 #
@@ -80,6 +80,10 @@ sub for_list
        $self->next;
 }
 
+sub compute_playfield
+{
+}
+
 # stub class when no actual progressmeter that still prints out.
 package OpenBSD::ProgressMeter::Stub;
 our @ISA = qw(OpenBSD::ProgressMeter);
index ef72c7f..2d32716 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: Term.pm,v 1.36 2016/06/15 15:31:09 espie Exp $
+# $OpenBSD: Term.pm,v 1.37 2016/06/23 16:11:23 espie Exp $
 #
 # Copyright (c) 2004-2007 Marc Espie <espie@openbsd.org>
 #
@@ -86,10 +86,16 @@ use POSIX;
 use Term::Cap;
 use Term::ReadKey;
 
+sub width
+{
+       my $self = shift;
+       return $self->{state}->width;
+}
+
 sub forked
 {
        my $self = shift;
-       $self->{lastdisplay} = ' 'x($self->{width}-1);
+       $self->{lastdisplay} = ' 'x($self->width-1);
 }
 
 sub init
@@ -98,7 +104,6 @@ sub init
        my $oldfh = select(STDOUT);
        $| = 1;
        select($oldfh);
-       $self->find_window_size;
        $self->{lastdisplay} = '';
        $self->{continued} = 0;
        $self->{work} = 0;
@@ -120,22 +125,10 @@ sub init
        }
 }
 
-sub find_window_size
-{
-       my $self = shift;
-
-       my @l = Term::ReadKey::GetTermSizeGWINSZ(\*STDOUT);
-       if (@l != 4) {
-               $self->{width} = 80;
-       } else {
-               $self->{width} = $l[0];
-       }
-}
-
 sub compute_playfield
 {
        my $self = shift;
-       $self->{playfield} = $self->{width} - length($self->{header}) - 7;
+       $self->{playfield} = $self->width - length($self->{header}) - 7;
        # we can print to 80 columns
        if ($self->{glitch} && $self->{state}->config->istrue("fullwidth")) {
                $self->{playfield} += 1;
@@ -150,10 +143,6 @@ sub set_header
        my ($self, $header) = @_;
        $self->{header} = $header;
        $self->compute_playfield;
-       $SIG{'WINCH'} = sub {
-               $self->find_window_size;
-               $self->compute_playfield;
-       };
        $SIG{'CONT'} = sub {
                $self->{continued} = 1;
        };
@@ -186,11 +175,11 @@ sub _show
                $d.="|$extra";
                $prefix++;
        }
-       if ($self->{width} > length($d)) {
+       if ($self->width > length($d)) {
                if ($self->{cleareol}) {
                        $d .= $self->{cleareol};
                } else {
-                       $d .= ' 'x($self->{width} - length($d) - 1);
+                       $d .= ' 'x($self->width - length($d) - 1);
                }
        }
 
index 6d93213..56080e9 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: State.pm,v 1.35 2016/06/23 12:44:10 espie Exp $
+# $OpenBSD: State.pm,v 1.36 2016/06/23 16:11:23 espie Exp $
 #
 # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org>
 #
@@ -363,6 +363,46 @@ sub defines
        return $self->{subst}->value($k);
 }
 
+sub width
+{
+       my $self = shift;
+       if (!defined $self->{width}) {
+               $self->find_window_size;
+       }
+       return $self->{width};
+}
+
+sub height
+{
+       my $self = shift;
+       if (!defined $self->{height}) {
+               $self->find_window_size;
+       }
+       return $self->{height};
+}
+               
+sub find_window_size
+{
+       my $self = shift;
+       require Term::ReadKey;
+       my @l = Term::ReadKey::GetTermSizeGWINSZ(\*STDOUT);
+       if (@l != 4) {
+               $self->{width} = 80;
+               $self->{height} = 24;
+       } else {
+               $self->{width} = $l[0];
+               $self->{height} = $l[1];
+               $SIG{'WINCH'} = sub {
+                       $self->find_window_size;
+                       $self->window_size_changed;
+               };
+       }
+}
+
+sub window_size_changed
+{
+}
+
 OpenBSD::Auto::cache(signer_list,
        sub {
                my $self = shift;