Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Last active June 28, 2017 05:45
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/0680ed18b6d6d018ce8fda8134152fab to your computer and use it in GitHub Desktop.
Save xtetsuji/0680ed18b6d6d018ce8fda8134152fab to your computer and use it in GitHub Desktop.
BS code detecter for Mac Slack (and/or Chromium?)
#!/usr/bin/env perl
# bs-code-detect.pl [--daemon] [--replace [--pasteback]] [--silent]
use strict;
use warnings;
use Term::ANSIColor qw(colored);
use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help);
use Mac::Pasteboard ();
BEGIN {
local $@;
eval { require Mac::Pasteboard; };
use constant HAVE_MAC_PASTEBOARD => length $@ ? 0 : 1;
}
use constant DAEMON_INTERVAL => 3;
use constant PASTEBOARD_FLAVOR => "public.utf8-plain-text";
GetOptions(
\my %opt, "replace", "daemon", "silent", "pasteback"
);
my $warning_color = "red bold on_bright_black";
my $warning_symbol = "BS";
my $warning_0x08 = colored([$warning_color], $warning_symbol);
my $info_color = "green on_bright_black";
if ( $opt{daemon} ) {
while (1) {
pasteboard_process();
} continue {
sleep DAEMON_INTERVAL;
}
} else {
pasteboard_process();
}
sub pasteboard_process {
my $marked_content =
my $cleaned_content =
my $original_content = pbpaste() or return;
my $replaced_count = $marked_content =~ s{\x08}{$warning_0x08}g;
print ">>> $marked_content\n" unless $opt{silent};
if ( $replaced_count ) {
print ">>> $marked_content\n" if $opt{silent};
printf "%s %d個発見しました\n",
colored([$warning_color], "warning:"), $replaced_count;
if ( $opt{replace} ) {
$cleaned_content =~ s{\x08}{}g;
pbcopy($cleaned_content);
print colored([$info_color], "info:") . " BS を削除した文字列をクリップボードに書き戻しました\n";
if ( $opt{pasteback} ) {
# keystroke("v", {using => "command down"});
press_cmd_v();
print colored([$info_color], "info:") . " cmd-v でペースト操作を行いました\n";
}
}
}
}
# JXA で Application("System Events").keystroke(...) してキー入力をおこなう
# cmd-v でペーストのキーボードショートカットがしたいだけ
sub keystroke {
my $key = shift;
my $param = shift;
open my $pipe, '|-', "osascript", "-l", "JavaScript";
print {$pipe} qq{var se = Application("System Events");\n};
if ( $param && ref $param eq 'HASH' ) {
# 面倒なのでパラメータは using しか対応していない
print {$pipe} qq{se.keystroke("${key}", {using: "$param->{using}"});\n};
} else {
print {$pipe} qq{se.keystroke("${key}");\n};
}
close $pipe;
}
# keystroke の実行が重そうなので、かなりコストを意識した Cmd-v するだけの限定版
sub press_cmd_v {
open my $pipe, '|-', "osascript", "-l", "JavaScript";
print {$pipe} <<END_JXA;
var se = Application("System Events");
se.keystroke("v", {using: "command down"});
END_JXA
close $pipe;
}
sub pbpaste {
if ( HAVE_MAC_PASTEBOARD ) {
# flavor is public.utf8-plain-text
# pbpaste returns +($data, $flag) if it is given flavor string.
return +(Mac::Pasteboard::pbpaste(PASTEBOARD_FLAVOR))[0];
} else {
return qx{pbpaste};
}
}
sub pbcopy {
my $string = shift;
if ( HAVE_MAC_PASTEBOARD ) {
return Mac::Pasteboard::pbcopy($string, PASTEBOARD_FLAVOR);
} else {
open my $pipe, '|-', 'pbcopy';
print {$pipe} $string;
close $pipe;
}
}
=pod
=encoding utf-8
=head1 NAME
bs-code-detect.pl
=head1 SYNOPSIS
bs-code-detect.pl [--daemon] [--replace [--pasteback]] [--silent]
=head1 OPTIONS
=head2 --daemon
デーモン動作を行う。既定値では 3秒(DAEMON_INTERVAL の定数値)ごとに
クリップボードの中を監視する
=head2 --replace
クリップボードの中に BS 制御文字を見つけたら、空文字で置換をしたものをクリップボードに書き戻す。
=head2 --pasteback
C<--replace> 指定でさらに C<--pasteback> 指定がある場合は、BS 制御文字を見つけたら、
クリップボードに書き戻すだけでなく、その場で cmd-v を打ってペーストを行おうとする。
=head2 --silent
通常は毎回行うクリップボードの中身の表示を抑制する。C<--daemon> での動作時は
都度表示されることがうっとおしい場合もあり、これを付けておくとよい。
=head1 AUTHOR
OGATA Tetsuji E<lt>tetsuji.ogata@gmail.comE<gt>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2017, OGATA Tetsuji.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
=head1 SEE ALSO
L<man 1 pbpaste>, L<man 1 pbcopy>.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment