Simple search in source code with Tomtit and Sparrow
Alexey Melezhik

Alexey Melezhik @melezhik

About: Sparrow - devops and automation tooling with Raku

Location:
Woodlands, TX
Joined:
Oct 8, 2017

Simple search in source code with Tomtit and Sparrow

Publish Date: Jan 23
5 0

Recently, I've released a simple Sparrow plugin called find to search text in source code, which is based on find and grep core linux utilities.

The rest of the document shows how to perform search in terminal by using Tomtit task runner.


Installation

To install the plugin, we need to use Tomtit task runner with profiles - preset of Raku scenarios that grouped by topics:

#!bash

cd /to/you/source/code
tom --init # initialize Tomtit for the first time
tom --profile code # install code profile scenarios
Enter fullscreen mode Exit fullscreen mode

Now once we've installed code profile scenarios (for now it's just a single one), we have an access to search scenario which is just a Raku wrapper to do search with find plugin:

#!bash

tom --cat search
Enter fullscreen mode Exit fullscreen mode
#!raku

my $ext = prompt("ext (go): ");

$ext = "go" unless $ext;

my $search1 = prompt("search1: ");

my $search2 = prompt("search2: ");

my $exclude = prompt("exclude: ");

say "find [$search1] [$search2] !$exclude in $ext";

task-run "find $search1 $search2 in $ext", "find", %(
  :$ext,
  :$search1,
  search2 => $search2 || "",
  exclude => $exclude || "",
);
Enter fullscreen mode Exit fullscreen mode

The logic is following:

‘search1’ filters results out by first string and then ‘search2’ filter is applied for final results, ‘ext’ defines files extension to search.

Customization

Stub code shipped with Tomtit profile is good enough, we only need to change it a bit, to make default choice for file extenstion suitable for Raku project:

#!bash

export EDITOR=nano
tom --edit search
Enter fullscreen mode Exit fullscreen mode
#!raku

my $ext = prompt("ext (rakumod): ");

$ext = "rakumod" unless $ext;

# ... the rest of the code is the same

Enter fullscreen mode Exit fullscreen mode

Search

Now let's search files in my Tomtit repository. Say, I want to find all exported functions.

#!bash

tom search
Enter fullscreen mode Exit fullscreen mode
load configuration from /home/melezhik/projects/Tomtit/.tom/env/config.raku
ext (rakumod): 
search1: sub
search2: is export
exclude: 
find [sub] [is export] ! in rakumod
23:09:06 :: find [sub] [is export] [!] in *.rakumod
23:09:06 :: ===
23:09:06 :: find . -name *.rakumod -exec grep --color -H 'sub' {} \; | grep 'is export'
23:09:06 :: case2
23:09:06 :: ./lib/Tomtit/Completion.rakumod:sub complete () is export {
23:09:06 :: ./lib/Tomtit.rakumod:our sub check-if-init ( $dir ) is export {
23:09:06 :: ./lib/Tomtit.rakumod:our sub init ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:our sub load-conf () is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub tomtit-usage () is export  {
23:09:06 :: ./lib/Tomtit.rakumod:sub tomtit-help () is export  {
23:09:06 :: ./lib/Tomtit.rakumod:sub tomtit-clean ($dir) is export { 
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-last ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-run ($dir,$scenario,%args?) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-remove ($dir,$scenario) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-cat ($dir,$scenario,%args?) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-edit ($dir,$scenario) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub environment-edit ($dir,$env) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub environment-list ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub environment-set ($dir,$env) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub environment-show ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub environment-cat ($dir,$env,%args?) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-doc ($dir,$scenario) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-list ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub scenario-list-print ($dir) is export {
23:09:06 :: ./lib/Tomtit.rakumod:multi sub profile-list  is export {
23:09:06 :: ./lib/Tomtit.rakumod:multi sub profile-list($dir,$profile is copy)  is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub profile-install ($dir, $profile is copy, %args?) is export {
23:09:06 :: ./lib/Tomtit.rakumod:sub completion-install () is export {
Enter fullscreen mode Exit fullscreen mode

That is it. Follow plugin documentation to get familiar with plugin parameters.

Thanks for reading

Comments 0 total

    Add comment