diff options
-rw-r--r-- | .SRCINFO | 18 | ||||
-rw-r--r-- | PKGBUILD | 20 | ||||
-rwxr-xr-x | fu.pl | 85 |
3 files changed, 123 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..2c9ef9770855 --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,18 @@ +# Generated by mksrcinfo v8 +# Tue Dec 22 14:32:59 UTC 2015 +pkgbase = fu-perl + pkgdesc = A simple perl script to query commandlinefu + pkgver = 0.01 + pkgrel = 1 + url = http://syohex.hatenablog.com/entry/20110123/1295778589 + arch = any + license = PerlArtistic + depends = perl>=5.10.0 + depends = perl-json-xs + depends = perl-mouse + options = !emptydirs + source = fu.pl + sha256sums = 198788111374a3ecdf5d856c4a0ba358da41ad6a3c3e61ab5af345a7d1851970 + +pkgname = fu-perl + diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..baea43408d14 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,20 @@ +# Maintainer: Christos Nouskas <nous at archlinux.us> + +_author=syohex +pkgname=fu-perl +pkgver=0.01 +pkgrel=1 +pkgdesc="A simple perl script to query commandlinefu" +arch=('any') +url="http://syohex.hatenablog.com/entry/20110123/1295778589" +license=('PerlArtistic') +depends=('perl>=5.10.0' 'perl-json-xs' 'perl-mouse') +options=(!emptydirs) +source=("fu.pl") +sha256sums=('198788111374a3ecdf5d856c4a0ba358da41ad6a3c3e61ab5af345a7d1851970') + +package() { + mkdir -p "$pkgdir/usr/bin/" + install -vm755 fu.pl "$pkgdir/usr/bin/" + ln -s "$pkgdir/usr/bin/fu.pl" "$pkgdir/usr/bin/fu" +} diff --git a/fu.pl b/fu.pl new file mode 100755 index 000000000000..afcccfafbb7f --- /dev/null +++ b/fu.pl @@ -0,0 +1,85 @@ +#!/usr/bin/env perl +package App::commandlinefu; +use Mouse; +use LWP::UserAgent; + +our $VERSION = '0.01'; + +has ua => ( + accessor => 'ua', + isa => "LWP::UserAgent", + lazy_build => 1, +); + +sub _build_ua { + LWP::UserAgent->new( + agent => __PACKAGE__ . $VERSION, + timeout => 10, + ); +} + +__PACKAGE__->meta->make_immutable; + +no Mouse; + +use Carp; +use Encode; +use JSON::XS; +use MIME::Base64; +use Term::ANSIColor qw(:constants); + +sub run { + my ($self, $query) = @_; + _validate($query); + + my $url = $self->_api_url($query); + + my $res = $self->ua->get($url); + unless ($res->is_success) { + Carp::croak("Can't download $url ", $res->status_line); + } + + my $content = decode_utf8($res->content); + + my $command_infos_ref = $self->_parse_response(\$content); + for my $command_info (@{$command_infos_ref}) { + print BRIGHT_GREEN "# "; + print encode_utf8($command_info->{summary}); + print encode_utf8(" [votes=" . encode_utf8($command_info->{votes}) . "]\n"); + print RESET; + + my $colored_query = YELLOW . $query . RESET; + $command_info->{command} =~ s/$query/$colored_query/; + print encode_utf8($command_info->{command}), "\n\n"; + } +} + +sub _validate { + my $query = shift; + + unless (defined $query) { + Carp::croak("Usage $0 search_command\n"); + } +} + +sub _parse_response { + my ($self, $content_ref) = @_; + return decode_json(${$content_ref}); +} + +sub _api_url { + my ($self, $query) = @_; + my $base = 'http://www.commandlinefu.com/commands/matching'; + my $base64 = encode_base64($query, ''); + + return "${base}/${query}/${base64}/json"; +} + +package main; +use strict; +use warnings; + +unless (caller) { + my $app = App::commandlinefu->new(); + $app->run( @ARGV ); +} |