LinkedIn Sourceforge Twitter

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

Fuzzy search within the OpenBSD packages

Posted on 2020-05-24 22:24:00 from Vincent in OpenBSD

fzf is an addicting tool for what concerns search. The overall process is very simple, despite very quick. Moreover the tool is well presented. This post will describe how to integrate fzf with OpenBSD packages.


Introduction

Searching keyword in packages description or package name is not the most powerful feature we have in OpenBSD. The FAQ proposes to search a file or a keyword in the package's name (by doing a grep in the ports tree). Or to perform SQL queries for advances search.
For simple search in package's name, we have pkg_info -Q to our help.

When I saw what others are doing with fzf, like apt-fzf, I had the idea to perform the same with OpenBSD.

Pre-requisites

You first have to install some packages:

doas pkg_add fzf sqlports

fzf is the fuzzy search tool.

sqlports is an SQLite DB consolidating all information concerning available packages.

Fuzzy search combined with pkg_add

Basically the idea is to provide to fzf the result of a SQLite query. Then the output of fzf will be send to pkg_add.

In order to have some package's details in the right panel, we will have an extra script for that only purpose.

Thus, if you count well, we will have 3 small scripts: pkg_search, fzpkg_add and pkg_descr

pkg_search

This script will search in SQLite DB build by sqlports all packages containing the keyword provided as parameter either in the package's name or in the package's comment or in the package's description.

sqlite3 /usr/local/share/sqlports "SELECT DISTINCT PathId, FULLPKGNAME,COMMENT FROM Ports WHERE FULLPKGNAME LIKE "%$1%" or COMMENT LIKE "%$1%" or DESCR_CONTENTS LIKE "%$1%" ORDER BY FULLPKGNAME;"

fzpkg_add

The core script is the one fiding fzf and analysing the outputs of fzf in order to send them to pkg_add.

pkgs=$(pkg_search $1 | fzf-tmux -m --tac +s --preview "pkg_descr {1}" --bind "ctrl-d:toggle-preview")
set -A params
IFS='
'
for line in $pkgs
do
  pkg=$(echo $line | cut -d"|" -f2)
  params[${#params}]="$pkg"
done

[ -n "$params" ] && (echo "pkg_add ${params[@]}" ; pkg_add ${params[@]})

The exchange of information between the scripts is always based on the packages's PathId. This is a unique identifier inside sqlports for each packages.

As you can also see, the right panel can be hidden by hitting Ctrl-d.

To keep an sort based on those PathID, which follows more or less the package's name, we ask fzf to not sort data.

The script is able to manage multiple selections. All selected packages will be sent to the command pkg_add

pkg_descr

Finally a small script displaying package's details.

pkgid=$(echo $1 | cut -d "|" -f1)
echo "Category:"
sqlite3 /usr/local/share/sqlports "select DISTINCT FullPkgPath from Categories where PathId="$pkgid";"
echo " "

res=$(sqlite3 /usr/local/share/sqlports "select Value from Flavors where PathId="$pkgid";"| tr '\n' ', ')
[ -n "$res" ] && echo "Falvors:\n${res%%,}\n"

echo "Description:"
sqlite3 /usr/local/share/sqlports "SELECT Value FROM Descr WHERE PathId = "$pkgid";"

In this script we look for most relevant details of a package: his category, if he has flavors and a package's description.

Download source files

You can download all those scripts from my fzpkg_add repository

Demo

A small video showing how it works:(better in full screen)

Conclusions

All in all those small scripts around sqlports and fzf are really useful if you want to search for packages with having /usr/ports installed and without querying openports.se.

This match perfectly my own needs. Do it is useful for other OpenBSD users ?
Should develop further those scripts ?



1, 0
displayed: 2912



What is the second letter of the word Moon?