config/users/pbsds/home/modules/jump.nix

80 lines
2.3 KiB
Nix
Raw Normal View History

2022-10-17 22:40:48 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.jump;
enabled = cfg.enableBash || cfg.enableZsh;
in {
meta.maintainers = [ hm.maintainers.pbsds ];
options.programs.jump = {
# Jumping around with symbolic links
# Based on http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
enableBash = mkEnableOption "jump - Quickly Navigate your Filesystem";
enableZsh = mkEnableOption "jump - Quickly Navigate your Filesystem";
marksPath = mkOption {
type = types.str;
default = "$HOME/.marks";
description = ''
Where the jump marks are stored
'';
};
};
config = mkIf enabled {
#home.packages = [ cfg.package ];
home.sessionVariables = { _JUMP_MARKPATH = cfg.marksPath; };
programs = let
rcScript = ''
2024-04-29 00:00:19 +02:00
function _mark_choose() {
${lib.getExe pkgs.fd} . "$_JUMP_MARKPATH" --type l -X ${lib.getExe pkgs.gum} choose --ordered {/}
}
function jump() {
local target="''${1:-$(_mark_choose)}"
if test -n "$target"; then
2024-02-01 00:58:20 +01:00
pushd . > /dev/null
2024-04-29 00:00:19 +02:00
cd -P "$_JUMP_MARKPATH/$target" 2>/dev/null || echo "No such mark: $target"
2024-02-01 00:58:20 +01:00
fi
2022-10-17 22:40:48 +02:00
}
2024-04-29 00:00:19 +02:00
function mark() {
2024-02-01 00:58:20 +01:00
mkdir -p "$_JUMP_MARKPATH" &&
test ! -L "$_JUMP_MARKPATH/$1" \
&& ln -s "$(pwd)" "$_JUMP_MARKPATH/$1" \
|| echo "mark already exists!"
2022-10-17 22:40:48 +02:00
}
2024-04-29 00:00:19 +02:00
function unmark() {
local target="''${1:-$(_mark_choose)}"
if test -n "$target"; then
rm -i "$_JUMP_MARKPATH/$target"
fi
2022-10-17 22:40:48 +02:00
}
2024-04-29 00:00:19 +02:00
function marks() {
2024-02-01 00:58:20 +01:00
#ls -l "$_JUMP_MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/\t-/g' && echo
command ls --color=always -l "$_JUMP_MARKPATH" | tr -s ' ' | cut -d' ' -f9- | sed -e 's/ -> /§/g' | column -t -s '§' -o ' -> '
2022-10-17 22:40:48 +02:00
}
_complete_jump_marks() {
local curw=''${COMP_WORDS[COMP_CWORD]}
2024-02-01 00:58:20 +01:00
local wordlist=$(find "$_JUMP_MARKPATH" -type l -printf "%f\n")
2024-08-13 16:26:58 +02:00
COMPREPLY=($(compgen -W ''\''${wordlist[@]}' -- "$curw"))
2022-10-17 22:40:48 +02:00
return 0
}
complete -F _complete_jump_marks jump unmark
'';
in {
# TODO: fish
bash.initExtra = mkIf cfg.enableBash rcScript;
zsh.initExtra = mkIf cfg.enableZsh rcScript;
};
};
}