[1046] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r1031 - trunk/owl

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:12:28 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Wed, 30 Apr 2008 13:29:46 -0400 (EDT)

Author: nelhage
Date: 2008-04-30 13:29:45 -0400 (Wed, 30 Apr 2008)
New Revision: 1031

Modified:
   trunk/owl/perlwrap.pm
Log:
Refactor default style code somewhat to be more easily extensible


Modified: trunk/owl/perlwrap.pm
===================================================================
--- trunk/owl/perlwrap.pm	2008-04-29 23:20:21 UTC (rev 1030)
+++ trunk/owl/perlwrap.pm	2008-04-30 17:29:45 UTC (rev 1031)
@@ -773,19 +773,28 @@
 sub format_message($)
 {
     my $self = shift;
-    my $m = shift;
+    my $m    = shift;
+    my $fmt;
 
     if ( $m->is_loginout) {
-        return $self->format_login($m);
+        $fmt = $self->format_login($m);
     } elsif($m->is_ping && $m->is_personal) {
-        return $self->format_ping($m);
+        $fmt = $self->format_ping($m);
     } elsif($m->is_admin) {
-        return $self->format_admin($m);
+        $fmt = $self->format_admin($m);
     } else {
-        return $self->format_chat($m);
+        $fmt = $self->format_chat($m);
     }
+    $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m);
+    return $fmt;
 }
 
+sub should_bold {
+    my $self = shift;
+    my $m = shift;
+    return $m->is_personal && $m->direction eq "in";
+}
+
 sub description {"Default style";}
 
 BarnOwl::create_style("default", "BarnOwl::Style::Default");
@@ -820,12 +829,19 @@
 sub format_admin {
     my $self = shift;
     my $m = shift;
-    return "\@bold(OWL ADMIN)\n" . indentBody($m);
+    return "\@bold(OWL ADMIN)\n" . $self->indent_body($m);
 }
 
 sub format_chat($) {
     my $self = shift;
     my $m = shift;
+    my $header = $self->chat_header($m);
+    return $header . "\n". $self->indent_body($m);
+}
+
+sub chat_header {
+    my $self = shift;
+    my $m = shift;
     my $header;
     if ( $m->is_personal ) {
         if ( $m->direction eq "out" ) {
@@ -845,19 +861,21 @@
         $header .= " [" . $m->opcode . "]";
     }
     $header .= "  " . time_hhmm($m);
+    $header .= $self->format_sender($m);
+    return $header;
+}
+
+sub format_sender {
+    my $self = shift;
+    my $m = shift;
     my $sender = $m->long_sender;
     $sender =~ s/\n.*$//s;
-    $header .= " " x (4 - ((length $header) % 4));
-    $header .= "(" . $sender . '@color[default]' . ")";
-    my $message = $header . "\n". indentBody($m);
-    if($m->is_personal && $m->direction eq "in") {
-        $message = BarnOwl::Style::boldify($message);
-    }
-    return $message;
+    return "  (" . $sender . '@color[default]' . ")";
 }
 
-sub indentBody($)
+sub indent_body($)
 {
+    my $self = shift;
     my $m = shift;
 
     my $body = $m->body;
@@ -873,7 +891,6 @@
 }
 
 package BarnOwl::Style::Basic;
-
 our @ISA=qw(BarnOwl::Style::Default);
 
 sub description {"Compatability alias for the default style";}
@@ -881,28 +898,11 @@
 BarnOwl::create_style("basic", "BarnOwl::Style::Basic");
 
 package BarnOwl::Style::OneLine;
-################################################################################
-# Branching point for various formatting functions in this style.
-################################################################################
+# Inherit format_message to dispatch
+our @ISA = qw(BarnOwl::Style::Default);
+
 use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s ';
-sub format_message($) {
-  my $self = shift;
-  my $m = shift;
 
-  if ( $m->is_loginout ) {
-    return $self->format_login($m);
-  }
-  elsif ( $m->is_ping) {
-    return $self->format_ping($m);
-  }
-  elsif ( $m->is_admin || $m->is_loopback) {
-    return $self->format_local($m);
-  }
-  else {
-    return $self->format_chat($m);
-  }
-}
-
 sub description {"Formats for one-line-per-message"}
 
 BarnOwl::create_style("oneline", "BarnOwl::Style::OneLine");
@@ -947,37 +947,35 @@
   my $line;
   if ($m->is_personal) {
     $line= sprintf(BASE_FORMAT,
-		   $dirsym,
-		   $m->type,
-		   '',
-		   ($dir eq 'out'
-		      ? $m->pretty_recipient
-		      : $m->pretty_sender));
+                   $dirsym,
+                   $m->type,
+                   '',
+                   ($dir eq 'out'
+                    ? $m->pretty_recipient
+                    : $m->pretty_sender));
   }
   else {
     $line = sprintf(BASE_FORMAT,
-		    $dirsym,
-		    $m->context,
-		    $m->subcontext,
-		    ($dir eq 'out'
-		       ? $m->pretty_recipient
-		       : $m->pretty_sender));
+                    $dirsym,
+                    $m->context,
+                    $m->subcontext,
+                    ($dir eq 'out'
+                     ? $m->pretty_recipient
+                     : $m->pretty_sender));
   }
 
   my $body = $m->{body};
   $body =~ tr/\n/ /;
   $line .= $body;
-  $line = BarnOwl::Style::boldify($line) if ($m->is_personal && lc($m->direction) eq 'in');
   return $line;
 }
 
-# Format locally generated messages
-sub format_local($)
+# Format owl admin messages
+sub format_admin($)
 {
   my $self = shift;
   my $m = shift;
-  my $type = uc($m->{type});
-  my $line = sprintf(BASE_FORMAT, '<', $type, '', '');
+  my $line = sprintf(BASE_FORMAT, '<', 'ADMIN', '', '');
   my $body = $m->{body};
   $body =~ tr/\n/ /;
   return $line.$body;


home help back first fref pref prev next nref lref last post