Day 11: Tab-completion galore (App::{PM,Pl,Prog,Dzil,Git}Utils)

About the series: perlancar's 2014 Advent Calendar: Introduction to a selection of 24 modules which I published in 2014. Table of contents.

Ever since I got interested in doing shell tab completion with Perl, I've been trying to add tab completion to various programs or, in some cases, (re)creating utilities with tab completion capability. Granted, many of the utilities mentioned here are pretty trivial, but that's okay since tab completion is the main point. Here are some of them:

First of all, if you want to try these utilities out and you use bash, you might want to install and setup bash-completion-prog first. Just do a cpanm App::BashCompletionProg and put . ~/.bash-completion-prog in your ~/.bashrc. After that, installing the other utilities cpanm -n App::PMUtils App::PlUtils App::ProgUtils App::GitUtils App::DzilUtils will automatically add completion entries to ~/.bash-completion-prog so the next shell you start will automatically enable tab completion for all the included utilities.

On to the utilities. From App::PMUtils: we have pmpath, a tab completion aware cousin of mpath. Suppose you want to know where Text::ANSI::Util is on the filesystem, you can do:

% pmpath tex[tab]
% pmpath Text/

% pmpath Text/an[tab]
% pmpath Text/ANSI[tab][tab]
Text/ANSI/ Text/ANSITable Text/ANSITable/

% pmpath Text/ANSI/u[tab]
% pmpath Text/ANSI/Util

% pmpath Text/ANSI/Util

Yup, as you can see above, the module completion is designed to be easy to type. It's case-insensitive and accept / (slash) as well as the normal :: (double-colon). When you use double colon, you must quote the module name because by default bash treats : as a word breaking character (I've ranted about this before, BTW).

Aside from returning path, there are also pmless (use it as a shortcut for less `pmpath Foo/Bar`), pmedit (shortcut for $EDITOR `pmpath Foo/Bar`), pmman (you know the drill), pmuninst, pmversion (the tab completion aware cousin of mversion, pmcost (to check which modules are loaded by a module), pmcore (check whether a module is a core module), and several others.

From App::ProgUtils: we have utilities like progcat, progless, progedit, progman. These are all shortcuts for $action `which $prog`, so for example progcat pmpath is equivalent to cat `which pmpath`. Handy if you want to view/edit/man a program in PATH. The completion these utilities provide is of course programs located in PATH.

From App::PlUtils: we have plsub, pluse, plcost, pllex. These utilities extract various components (like subroutine definition, use/require statement) and is usually used rarely unless you are analyzing some Perl scripts. The completion they provide are normal file completion, but most of these utilities also let you specify a Perl module instead, in which case you specify --module and Perl module completion will kick in.

From App::DzilUtils: if you use Dist::Zilla, these self-evidently-named utilities will conveniently provide you with some dzil-related information: list-dzil-bundles, list-dzil-bundle-contents, list-dzil-plugins, list-dzil-plugin-roles. The nice things about these utilities is that you don't have to type the RSI-inducing Dist::Zilla::Plugin:: or Dist::Zilla::PluginBundle:: prefixes. For example, if you want to know which roles the plugin assumes:

% list-dzil-plugin-roles ru[tab]
% list-dzil-plugin-roles Run

Or if you want to know the contents of bundle Dist::Zilla::PluginBundle::Author::PERLANCAR:

% list-dzil-bundle-contents au[tab]
% list-dzil-bundle-contents Author/
% list-dzil-bundle-contents Author/perl[tab]
% list-dzil-bundle-contents Author/PERLANCAR

From App::GitUtils: currently there is only a single utility included here called gu. It is meant to be fast to type. It has a few functionalities right now. One example, if you want to run git post-commit hook, instead of typing:

% .git/hooks/post-commit

You can type (with tab completion):

% gu po[tab]
% gu post-commit

The advantage of using gu is, you don't have to be in the topmost directory of the git repository. gu will search upwards until it finds a .git directory.

1 Comment

Leave a comment

About perlancar

user-pic #perl #indonesia