Skip to main content

Improving prove with Preview Windows

·241 words·2 mins·
perl Programming testing tab completion fzf prove fd bat
❤️ It's great to see you here! I'm currently available on evenings and weekends for consulting and freelance work. Let's chat about how I can help you achieve your goals.

Last month, I talked about how to add tab completion to a CLI program, using fzf. Next we talked about a more generic solution which adds a preview window to a file search.

Today we will look at how to combine the knowledge from these two posts. What we want to do is have our custom completion for the prove CLI, but with a preview window. The solution looks like this:

_fzf_complete_prove() {
  _fzf_complete  --bind='ctrl-/:toggle-preview' --preview 'bat --style=numbers --color=always --line-range :50 {}' --reverse --multi --prompt="prove> " -- "$@" < <(
      fd -e t
  )
}

_fzf_complete_prove_post() {
    awk '{print $1}'
}

[ -n "$BASH" ] && complete -F _fzf_complete_prove -o default -o bashdefault prove

If you don’t have bat installed, you can use cat for your file previews instead. You’ll just need a different incantation in _fzf_complete_prove():

_fzf_complete_prove() {
  _fzf_complete  --bind='ctrl-/:toggle-preview' --preview 'cat {}' --reverse --multi --prompt="prove> " -- "$@" < <(
      fd -e t
  )
}

If you don’t have fd installed, swap it out with find or whatever your preferred tool is. Maybe something like find t | grep "\.t$". If you’re an ack user, you could try something like ack -f t | grep "\.t$".

For reference, this is where my fzf completions currently live in my dot-files.

That’s it! Now, you’ll get the same behaviour as we saw in our previous screencast, but you can now get it via prove **<tab>.

See the previous screencast for reference:

asciicast


Related

Adding Tab Completion to Your Favourite CLI Programs
·544 words·3 mins
perl Programming testing tab completion fzf prove fd
Adding a Preview Window to Your Tab Completion
·304 words·2 mins
perl Programming testing tab completion fzf prove fd bat
Introducing LWP::ConsoleLogger::Everywhere
·157 words·1 min
perl LWP::ConsoleLogger LWP::ConsoleLogger::Everywhere