Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created February 22, 2019 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/6132344663b4e960fc79f69dba33cc9d to your computer and use it in GitHub Desktop.
Save xtetsuji/6132344663b4e960fc79f69dba33cc9d to your computer and use it in GitHub Desktop.
Clipboard contained markdown dumper for backup
#!/usr/bin/env perl
# backup-clipboard.pl
# 実行するとクリップボードを監視して、対象とする Markdown だと思ったら
# 雑に /tmp 以下にダンプする
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Mac::Pasteboard;
use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help);
use Pod::Usage qw(pod2usage);
use constant WATCH_INTERVAL => 5;
GetOptions(
\my %opt,
"regexp|e=s"
);
my $cv = AnyEvent->condvar;
my $pb_watcher = AnyEvent::Mac::Pasteboard->new(
interval => WATCH_INTERVAL,
on_change => sub {
my $pb_content = shift;
# 全体コピーした時に冒頭にリストかヘディングがあるという見立て
if ( $pb_content !~ /\A(?:-|#)\s+/ ) {
# 対象とするデータではないので見逃す
return;
}
my $now = time;
my $buffer_file = "/tmp/backup-clipboard.$now.$$.txt";
my $res = write_file($buffer_file, \$pb_content);
if ( $res ) {
print "change write to $buffer_file\n";
} else {
print "fail write ($!)\n";
}
},
on_error => sub {
die "error\n";
},
);
print "waiting...\n";
$cv->recv;
sub write_file {
my $buffer_file = shift;
my $buffer_ref = shift;
open my $fh, '>', $buffer_file or return;
print {$fh} $$buffer_ref;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment