Ignore:
Timestamp:
Oct 28, 2009, 10:00:15 AM (12 years ago)
Author:
Karl Ramm <kcr@1ts.org>
Branches:
master, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
48d130b
Parents:
7ca5d3e
git-author:
Karl Ramm <kcr@1ts.org> (10/25/09 12:11:13)
git-committer:
Karl Ramm <kcr@1ts.org> (10/28/09 10:00:15)
Message:
make control characters human-readable

Random control characters that make it through the formatting code
should not be passed silently to the user's terminal...
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl/Style/Default.pm

    r0b2afba rad0dedd  
    2424    }
    2525    $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m);
     26    $fmt = $self->humanize($fmt);
    2627    return $fmt;
    2728}
     
    8687    if ( $m->is_personal ) {
    8788        my $personal_context = $m->personal_context;
    88         $personal_context = ' [' . $personal_context . ']' if $personal_context;
     89        $personal_context = ' [' . $self->humanize($personal_context, 1) . ']' if $personal_context;
    8990
    9091        if ( $m->direction eq "out" ) {
     
    9495        }
    9596    } else {
    96         $header = $m->context;
     97        $header = $self->humanize($m->context, 1);
    9798        if(defined $m->subcontext) {
    98             $header .= ' / ' . $m->subcontext;
     99            $header .= ' / ' . $self->humanize($m->subcontext, 1);
    99100        }
    100101        $header .= ' / @b{' . $m->pretty_sender . '}';
     
    102103
    103104    if($m->opcode) {
    104         $header .= " [" . $m->opcode . "]";
     105        $header .= " [" . $self->humanize($m->opcode, 1) . "]";
    105106    }
    106107    $header .= "  " . $self->format_time($m);
     
    138139}
    139140
     141=head3 humanize STRING [one_line]
     142
     143Method that takes a STRING with control characters and makes it human
     144readable in such a way as to not do anything funky with the terminal.
     145If one_line is true, be more conservative about what we treat as
     146control character.
     147
     148=cut
     149
     150sub humanize
     151{
     152  my $self = shift;
     153  my $s = shift;
     154  my $oneline = shift;
     155  sub _humanize_char
     156  {
     157    my $c = ord(shift);
     158
     159    if ($c < ord(' ')) {
     160      return ('^' . chr($c + ord('@')));
     161    } elsif ($c == 255) {
     162      return ('^?');
     163    } else {
     164      return (sprintf('\\x{%x}', $c));
     165    }
     166  }
     167  my $colorize = (BarnOwl::getvar('colorztext') eq 'on')
     168    ? '@color(cyan)' : '';
     169
     170  my $chars = $oneline ? qr/[[:cntrl:]]/ : qr/[^[:print:]]|[\r\cK\f]/;
     171
     172  $s =~ s/($chars)/
     173    "\@b($colorize" . _humanize_char($1) . ')'/eg;
     174
     175  return $s;
     176}
     177
     178=head3 humanize_short STRING
     179
     180As above, but always be conservative, and replace with a '?' instead
     181of something mmore elaborate.
     182
     183=cut
     184
     185sub humanize_short
     186{
     187  my $self = shift;
     188  my $s = shift;
     189
     190  $s =~ s/[[:cntrl:]]/?/g;
     191
     192  return $s;
     193}
    140194
    1411951;
Note: See TracChangeset for help on using the changeset viewer.