Skip to content

Ability to serve as generic frecency database #1188

@purevg

Description

@purevg

zoxide already have the capability to be a generic frecency database for other purposes. It would be very nice to be able to disable the directory check being done in add.rs file through an environment variable or an option.

I'm already able to use it as a generic frecency list if I use the following wrapper script which disables this check by use of a ld_preload trick.

With this script, I'm able to use it like the following example to have a frecency list for my executables and its working fine:

( frecency test query -l | cut -d/ -f2-; dmenu_path ) | fzf | sed 's,^,/,' | tee /dev/stderr | xargs -r frecency test add

It can be used the same way with all kind of list like emoji insertion history, profile selection history etc. It would just be nicer to not have to use a ld_preload and instead just rely on a environment variable.

The wrapper script:

#!/bin/sh -eu

ensure_ld_preload_lib_present()
{
    local dir="$HOME/.cache/frecency"
    if [ ! -e "$dir/fake_dir.so" ]; then
        mkdir -p "$dir"
        cat > "$dir/fake_dir.c" <<"EOF"
// gcc -shared -fPIC -o fake_dir.so fake_dir.c
#define _GNU_SOURCE
#include <sys/stat.h>

int statx(int dirfd, const char *restrict pathname, int flags,
          unsigned int mask, struct statx *restrict statxbuf)
{
    *statxbuf = *(&(typeof(*statxbuf)){ 0 });
    statxbuf->stx_mode = S_IFDIR | 0755;
    return 0;
}
EOF
        gcc -shared -fPIC -o "$dir/fake_dir.so" "$dir/fake_dir.c"
    fi
}

frecency()
{
    local topic="$1"; shift
    local db="$HOME/.local/share/zoxide-$topic"
    mkdir -p "$db"

    _ZO_DATA_DIR="$db" \
        LD_PRELOAD="$HOME/.cache/frecency/fake_dir.so" \
        zoxide "$@"
}

ensure_ld_preload_lib_present
frecency "$@"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions