Created
February 22, 2019 07:39
-
-
Save xtetsuji/6132344663b4e960fc79f69dba33cc9d to your computer and use it in GitHub Desktop.
Clipboard contained markdown dumper for backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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