LoginSignup
10
10

More than 5 years have passed since last update.

iTerm2の新機能を使ってターミナルにポプテピピックの4コマをランダムに表示する

Last updated at Posted at 2016-03-25

iterm2が画像をサポートするようになりました。
iterm2の新機能については、以下の記事が素晴らしくまとまっています。

iTerm2 3.0の新機能 - Qiita

今回はせっかくなので、node.jsでターミナルに画像を表示するコマンドラインツールを書いてみました。
popute-imgまたはppt-imgでポプテピピックの4コマをランダムに表示します。

img.gif

ポプテピピック、どれもパンチが効いててすごい。

コードはgithubで公開しています。
github: akameco/popute-random-img

インストール

npmからインストールできます。

$ npm install -g popute-random-img

使い方

以下のコマンドを提供します。

$ popute-img

または

$ ppt-img

実装

コマンドラインツール

node.jsによるコマンドラインツールの作り方はこちらを参照してください。
node.jsによるコマンドラインツール - Qiita

スクレイピング

httpライブラリはgotを使っています。
node.jsのhttpライブラリというとrequestがメジャーですが、個人的には軽量でPromiseベースのgotが好みです。

const got = require('got');
got('http://mangalifewin.takeshobo.co.jp/rensai/popute2/').then(res => {
    console.log(res.body);
}).catch(e => console.log(e));

それをjQueryライクに要素を取得できるcheerioでパースします。

got(url).then(res => {
    const $ = cheerio.load(res.body);
    return $('.bookR a').map((i, el) => {
        return $(el).attr('href');
    }).get();
}).catch(err => {
    console.log(err);
});

iTerm2に画像を表示

term-imgを使いました。
sindresorhus/term-img: Display images in your terminal

画像のパスかbufferを引数に取ります。

const termImg = require('term-img');
termImg('test.jpg');

まとめ

今回は完全にお遊びですが、キャプチャをターミナルから確認したり、ログの可視化などに使えそうです。
テストがすべてpassしたらlgtmな画像を表示してもいいかもしれませんね。

akameco/popute-random-img: ポプテピピックの画像をランダムで取得

参考

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10