[Bps-public-commit] r16078 - in Prophet/branches/dispatcher: lib/Prophet

sartak at bestpractical.com sartak at bestpractical.com
Thu Sep 25 18:48:06 EDT 2008


Author: sartak
Date: Thu Sep 25 18:48:06 2008
New Revision: 16078

Modified:
   Prophet/branches/dispatcher/   (props changed)
   Prophet/branches/dispatcher/lib/Prophet/CLI.pm

Log:
 r72494 at onn:  sartak | 2008-09-25 18:22:50 -0400
 Move stringify_props into CLI


Modified: Prophet/branches/dispatcher/lib/Prophet/CLI.pm
==============================================================================
--- Prophet/branches/dispatcher/lib/Prophet/CLI.pm	(original)
+++ Prophet/branches/dispatcher/lib/Prophet/CLI.pm	Thu Sep 25 18:48:06 2008
@@ -258,6 +258,88 @@
     );
 }
 
+=head2 stringify_props
+
+Returns a stringified form of the properties suitable for displaying directly
+to the user. Also includes luid and uuid.
+
+You may define a "color_prop" method which transforms a property name and value
+(by adding color).
+
+You may also define a "color_prop_foo" method which transforms values of
+property "foo" (by adding color).
+
+=cut
+
+sub stringify_props {
+    my $self = shift;
+    my %args = validate(@_, {
+        record  => { ISA => 'Prophet::Record'},
+        batch   => 1,
+        verbose => 1,
+    });
+
+    my $record = $args{'record'};
+    my $props = $record->get_props;
+
+    my $colorize = $args{'batch'} ? 0 : 1;
+
+    # which props are we going to display?
+    my @show_props;
+    if ($record->can('props_to_show')) {
+        @show_props = $record->props_to_show(\%args);
+
+        # if they ask for verbosity, then display all the other fields
+        # after the fields that our subclass wants to show
+        if ($args{verbose}) {
+            my %already_shown = map { $_ => 1 } @show_props;
+            push @show_props, grep { !$already_shown{$_} }
+                              sort keys %$props;
+        }
+    }
+    else {
+        @show_props = ('id', sort keys %$props);
+    }
+
+    # kind of ugly but it simplifies the code
+    $props->{id} = $record->luid ." (" . $record->uuid . ")";
+
+    my $max_length = 0;
+    my @fields;
+
+    for my $field (@show_props) {
+        my $value = $props->{$field};
+
+        # don't bother displaying unset fields
+        next if !defined($value);
+
+        # color if we can (and should)
+        my ($colorized_field, $colorized_value);
+        if ($colorize) {
+            ($colorized_field,$colorized_value) = $record->colorize($field => $value);
+
+    }
+        push @fields, [$field, ($colorized_field|| $field), ($colorized_value ||$value)];
+
+        # don't check length($field) here, since coloring will increase the
+        # length but we only care about display length
+        $max_length = length($field)
+            if length($field) > $max_length;
+    }
+
+    $max_length = 0 if $args{batch};
+
+    # this code is kind of ugly. we need to format based on uncolored length
+    return join '',
+           map {
+               my ($field, $colorized_field, $colorized_value) = @$_;
+               $colorized_field .= ':';
+               $colorized_field .= ' ' x ($max_length - length($field));
+               "$colorized_field $colorized_value\n"
+           }
+           @fields;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 



More information about the Bps-public-commit mailing list