Skip to content

Mod IDs and argument completions #68

Description

@BinToss

I've been prototyping mod ID completions for Fish, lately, and thought I'd share the system I settled on.

The main points are how and where to store the mod IDs.

  1. rustique install <MOD_ID>... and rustique info <MOD_ID>... should use IDs extracted from the search database. This will be the "global" cache stored alongside Rustique's config file.
  2. delete, update, and backup commands should use a "local" cache built from a mod_dir. I parse the rustique-sync.json for the IDs, but Rustique can write the IDs from memory to a file after a sync. This is the "local" cache.

Caching the mod IDs to files is optional. It depends on the performance cost of parsing the mod ID sources and ease of supporting each shell. I opted for caching to newline-separated text files as a preemptive optimization.

function rustique_update_modIDs -d 'Run this after `rustique sync [-s|-g]` or after any `rustique install/delete` operation.'
    # extract the value of mods_dir from rustique's `config list` output.
    set -f mod_dir $(
        pcregrep -oe '(?<=mod_dir = ")(.+)(?="$)' $HOME/.config/rustique/config.toml
    )

    set globalModsIdsTxt "$(path normalize "$HOME/.config/rustique/.modIDs.txt")"
    set localModsIdsTxt "$(path normalize "$mod_dir/.modIDs.txt")"
    for idsTxt in "$globalModsIdsTxt" "$localModsIdsTxt"
        not [ -e "$idsTxt" ]
        and echo "" >$idsTxt
    end

    echo "Updating $globalModsIdsTxt; $localModsIdsTxt"
    #  redirecting to $localModsIdsTxt is an "Invalid redirection target" error. So, tee to the file and redirect to null.
    jq -r '.mods.[].modidstrs[]' $HOME/.config/rustique/mod-search.json |
        tee $globalModsIdsTxt >/dev/null
    jq -r '.RustiqueSync | keys[]' $mod_dir/rustique-sync.json |
        tee $localModsIdsTxt >/dev/null

    echo "Updating fish completions"
    source $HOME/.config/fish/completions/rustique_modIDs.fish
end
# region modIDs

set -l mod_dir $(
        pcregrep -oe '(?<=mod_dir = ")(.+)(?="$)' $HOME/.config/rustique/config.toml
    )

set rustiqueGlobalModIDsTxt $(path normalize -- $HOME/.config/rustique/.modIDs.txt)
set -l rustiqueLocalModIDsTxt $(path normalize -- "$mod_dir/.modIDs.txt")

for idsTxt in "$rustiqueGlobalModIDsTxt" "$rustiqueLocalModIDsTxt"
    not [ -e "$idsTxt" ]
    and echo "" >$idsTxt
end

# @fish-lsp-disable 4005
complete -c rustique -n "__fish_rustique_using_subcommand install" -f -a "$(cat $rustiqueGlobalModIDsTxt)"
complete -c rustique -n "__fish_rustique_using_subcommand update" -f -a "$(cat $rustiqueLocalModIDsTxt)"
complete -c rustique -n "__fish_rustique_using_subcommand delete" -s m -l mod-id -r -a "$(cat $rustiqueLocalModIDsTxt)"
complete -c rustique -n "__fish_rustique_using_subcommand info" -f -a "$(cat $rustiqueGlobalModIDsTxt)"

# @fish-lsp-enable 4005

set -e mod_dir
set -e rustiqueLocalModIDsTxt

# endregion modIDs

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