Skip to content

Instantly share code, notes, and snippets.

@yteraoka
Created May 22, 2013 13:09
Show Gist options
  • Save yteraoka/5627419 to your computer and use it in GitHub Desktop.
Save yteraoka/5627419 to your computer and use it in GitHub Desktop.
CR (Carriage Return) で Cinnamon が期待通りに動作しない件
register_read_type line => sub {
my ($self, $cb, $eol) = @_;
if (@_ < 3) {
# this is more than twice as fast as the generic code below
sub {
$_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return;
$cb->($_[0], "$1", "$2");
1
}
} else {
$eol = quotemeta $eol unless ref $eol;
$eol = qr|^(.*?)($eol)|s;
sub {
$_[0]{rbuf} =~ s/$eol// or return;
$cb->($_[0], "$1", "$2");
1
}
}
};
sub start_async_read {
my ($self) = @_;
my $handle_container = $self->{handle_container} or return;
my $cv = AnyEvent->condvar;
my $handles = [];
for my $name (keys %$handle_container) {
my $info = $handle_container->{$name};
$cv->begin;
my $handle; $handle = AnyEvent::Handle->new(
fh => $info->{fh},
on_read => sub {
$handle->push_read(line => sub {
my $line = $_[1];
push @{$info->{output_lines}}, $line;
log info => sprintf "[%s :: %s] %s",
$self->{host}, $name, $line;
});
},
on_eof => sub {
$cv->end;
},
on_error => sub {
my $msg = $_[2];
log error => sprintf "[%s :: %s] %s", $self->{host}, $name, $msg
unless $! == POSIX::EPIPE;
$cv->end;
},
);
push @$handles, $handle;
}
$cv->recv;
for my $h (@$handles) {
$h->destroy;
}
}
for my $i (0 .. 10) {
my $percent = sprintf "\r%3d%%", $i * 10;
syswrite(STDOUT, $percent, length($percent));
sleep 1;
}
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment