remove newline from end of ssh key
[pxelator] / lib / PXElator / html.pm
index 857f923..799df37 100644 (file)
@@ -1,12 +1,27 @@
 package html;
 
+use Data::Dump qw/dump/;
+
 sub table {
        my $cols = shift;
+       my $th;
+
+       if ( $cols < 0 ) {
+               $cols = abs($cols);
+               $th .= qq|<th>| . shift(@_) . qq|</td>| foreach ( 1 .. $cols );
+               $th .= qq|</tr>\n<tr>|;
+       }
+
        my @td = map { "<td>$_</td>" } @_;
-       my $html = qq{<table>\n<tr>};
+       my $html = qq{<table>\n<tr>$th};
+       my $row = 0;
+
        foreach ( 0 .. $#td ) {
                        $html .= $td[$_];
-                       $html .= qq{</tr>\n<tr>} if $_ % $cols == 1;
+                       if ( ( $_ + 1 ) % $cols == 0 ) {
+                               $zebra = $row++ % 2 == 0 ? qq{ style="background: #eee"} : '';
+                               $html .= qq{</tr>\n<tr$zebra>};
+                       };
        }
        $html .= qq{</tr>\n</table>};
 }
@@ -24,20 +39,70 @@ sub tabs {
        
 
 sub tt {
-       qq|<tt>| . join(' ', @_) . qq|</tt>|;
+       qq|<tt>| . join(' ', @_) . qq|</tt>| if @_;
 }
 
 sub select {
        my $name = shift;
-       my $checked_option = shift;
+       my $selected_option = shift;
+       unshift @_, '' unless $selected_option;
        return join("\n"        
                , qq|<select type=select name=$name>|
-               , join("\n", map { my $checked = $_ eq $checked_option ? 'checked' : ''; qq|<option name=$_ $checked>$_</option>| } @_ )
+               , join("\n", map { my $selected = $_ eq $selected_option ? 'selected' : ''; qq|<option name=$_ $selected>$_</option>| } @_ )
                , qq|</select>|
                , qq|</form>|
        );
 }
 
+sub pre {
+       qq|<pre>| . join(' ', @_) . qq|</pre>| if @_;
+}
+
+sub pre_dump {
+       my $data = shift;
+       my $dump = dump( $data );
+       $dump =~ s{"([^"]+)"(\s*=>)}{$1  $2}gs;
+       $dump =~ s{"([^"]*)"}{"<b>$1</b>"}gs;
+       qq|<pre>$dump</pre>|;
+}
+
+sub conf {
+       my ($ip,$conf,$format) = @_;
+       my @editable = splice(@_,3);
+
+       warn "# conf ",dump( $ip, $conf, $format, [ @editable ] );
+
+       $format ||= 'inline';
+
+       my @opts = map {
+               my $name = $_;
+               my $html = $conf->{$name};
+
+               if ( $format eq 'edit' && grep { m/^$name$/ } @editable ) {
+                       $size = length($html);
+                       ( $name, qq|<input name=$name value="$html" size=$size>| )
+               } else {
+                       if ( $name eq 'amt' ) {
+                               $html = qq|<a title="$html" href=http://$ip:16992/logon.htm>logon</a>|;
+                       } elsif ( $name eq 'ssh' ) {
+                               $html =~ s{\s(\S{16}).+(\S{16})\s}{ $1..$2 };
+                               chomp($html);
+                       }
+                       $html = qq|<pre style="display: inline">$html</pre>|
+                       unless
+                       $html =~ s{\b(\S+)\t(\S+)\t(\S+)\b}{<b title="$1/$2">$3</b> }gs;
+               
+                       if ( $format =~ /edit|table/ ) {
+                               ( $name, $html );
+                       } else {
+                               qq|<em>$name</em> $html<br>|
+                       }
+               }
+       } keys %$conf;
+
+       $format eq 'inline' ? join("\n", @opts) : @opts;
+}
+
 warn "loaded";
 
 1;