diff --git a/home/Mallorea.nix b/home/Mallorea.nix index 7426128..0e875a7 100644 --- a/home/Mallorea.nix +++ b/home/Mallorea.nix @@ -47,8 +47,14 @@ let ''); }; }; -in - { + fzgo_paths = [ + "/home/server/.dotfiles" + "/home/server/Containers" + "/home/server/Workspace" + "/home/server/Storage/Thomas" + "/home/server/Storage/Shared" + ]; +in { home.username = "server"; home.homeDirectory = "/home/server"; nix = { @@ -63,7 +69,8 @@ in imports = [ (import ./utils/common.nix { inherit inputs config pkgs; }) - (import ./utils/services.nix { inherit pkgs; scripts = scripts; }) + (import ./utils/services.nix { inherit pkgs scripts; }) + (import ./utils/fzgo_links.nix { inherit config pkgs fzgo_paths; }) ./ssh ]; programs.ssh.matchBlocks."*".identityFile = "/home/server/.secrets/SSH/Mallorea/id_ed25519"; @@ -73,9 +80,4 @@ in LANG = "en_US.UTF-8"; XDG_RUNTIME_DIR = "/run/user/$(id -u)"; }; - - programs.zsh.initExtra = pkgs.lib.mkForce '' - LOCAL_SEARCH_DIRS=(~/.dotfiles/ ~/Containers ~/Workspace ~/Storage/Thomas ~/Storage/Shared) - REMOTE_SEARCH_DIRS=() - ''; } diff --git a/home/Riva.nix b/home/Riva.nix index d24aaf3..60230a9 100644 --- a/home/Riva.nix +++ b/home/Riva.nix @@ -1,9 +1,14 @@ { inputs, config, pkgs, ... }: - +let + fzgo_paths = [ + "/home/user/.dotfiles" + ]; +in { imports = [ (import ./utils/common.nix { inherit inputs config pkgs; }) (import ./utils/python.nix { inherit inputs config pkgs; }) + (import ./utils/fzgo_links.nix { inherit config pkgs fzgo_paths; }) ./ssh ]; diff --git a/home/utils/fzgo_links.nix b/home/utils/fzgo_links.nix new file mode 100644 index 0000000..2def8f6 --- /dev/null +++ b/home/utils/fzgo_links.nix @@ -0,0 +1,12 @@ +{ config, pkgs, fzgo_paths }: +let + mkLink = source_path: file_type: { + "${source_path}_${file_type}" = { + source = config.lib.file.mkOutOfStoreSymlink source_path; + target = "/home/user/.config/fzgo/entries/${file_type}/${pkgs.lib.strings.replaceStrings [ "/" ] [ "|" ] source_path}"; + }; + }; + mkLinks = source_path: (mkLink source_path "d") // (mkLink source_path "f"); +in { + home.file = pkgs.lib.mkMerge (map mkLinks fzgo_paths); +} diff --git a/home/zsh/default.nix b/home/zsh/default.nix index 495b77e..12e1f7d 100644 --- a/home/zsh/default.nix +++ b/home/zsh/default.nix @@ -19,8 +19,7 @@ PDF_VIEWER = "evince"; GPULAB_CERT = "${config.xdg.configHome}/../.secrets/GPULab/certificate.pem"; GPULAB_DEV = "False"; - GCC_COLORS = - "error = '01;31:warning = '01;35:note = '01;36:caret = '01;32:locus = '01:quote = '01'"; + GCC_COLORS = "error = '01;31:warning = '01;35:note = '01;36:caret = '01;32:locus = '01:quote = '01'"; ARCHFLAGS = "-arch x86_64"; TERM = "screen-256color"; PYTHONDONTWRITEBYTECODE = "1"; @@ -56,45 +55,29 @@ enableCompletion = true; syntaxHighlighting.enable = false; autosuggestion.enable = true; - - initExtra = '' - LOCAL_SEARCH_DIRS=(~/.dotfiles/) - REMOTE_SEARCH_DIRS=( - ~/Workspace/Trackbox/Branches/ - ~/Workspace/Trackbox/Repositories/ - ~/Workspace/Trackbox/Resources/ - ~/Workspace/Trackbox/Docker/ - ~/Workspace/University/PhD/Thesis/ - /home/server/Storage/Shared/ - /home/server/Storage/Thomas/ - ) - export LD_LIBRARY_PATH=${pkgs.sqlite.out}/lib:$LD_LIBRARY_PATH - ''; - + initExtra = ''export LD_LIBRARY_PATH=${pkgs.sqlite.out}/lib:$LD_LIBRARY_PATH''; initExtraFirst = '' - ZVM_VI_INSERT_ESCAPE_BINDKEY=jj - source ${./files/waypipe.zsh} - source ${pkgs.zsh-powerlevel10k}/share/zsh-powerlevel10k/powerlevel10k.zsh-theme - source ${./files/p10k.zsh} - source ${pkgs.zsh-vi-mode}/share/zsh-vi-mode/zsh-vi-mode.plugin.zsh - source ${./files/functions.zsh} - function zvm_after_init() { - source ${./files/term.zsh} - source ${./files/completion.zsh} - if [ -n "\$\{commands[fzf-share]\}" ]; then - source "$(fzf-share)/key-bindings.zsh" - source "$(fzf-share)/completion.zsh" - fi - source ${./files/keys.zsh} - eval "$(direnv hook zsh)" - export LD_LIBRARY_PATH=${pkgs.sqlite.out}/lib:$LD_LIBRARY_PATH - } - function run() { - NIXPKGS_ALLOW_UNFREE=1 nix run --impure "nixpkgs#$1" -- "''${@:2}" - } - function o() { - ${pkgs.handlr-regex}/bin/handlr open $@ < /dev/null &> /dev/null & disown - } + ZVM_VI_INSERT_ESCAPE_BINDKEY=jj + source ${./files/waypipe.zsh} + source ${pkgs.zsh-powerlevel10k}/share/zsh-powerlevel10k/powerlevel10k.zsh-theme + source ${./files/p10k.zsh} + source ${pkgs.zsh-vi-mode}/share/zsh-vi-mode/zsh-vi-mode.plugin.zsh + source ${./files/functions.zsh} + source ${./files/fzgo.zsh} + function zvm_after_init() { + source ${./files/term.zsh} + source ${./files/completion.zsh} + if [ -n "\$\{commands[fzf-share]\}" ]; then + source "$(fzf-share)/key-bindings.zsh" + source "$(fzf-share)/completion.zsh" + fi + source ${./files/keys.zsh} + eval "$(direnv hook zsh)" + export LD_LIBRARY_PATH=${pkgs.sqlite.out}/lib:$LD_LIBRARY_PATH + } + function o() { + ${pkgs.handlr-regex}/bin/handlr open $@ < /dev/null &> /dev/null & disown + } ''; shellAliases = { @@ -114,14 +97,10 @@ python = "python3"; vim = "nvim"; v = ''echo $(pwd) > /tmp/oil_dir && nvim -c ':lua require("oil").open()' && cd "$(cat /tmp/oil_dir 2> /dev/null || pwd)"''; - cpr = - "rsync --archive -hh --partial --info = stats1,progress2 --modify-window = 1"; - mvr = - "rsync --archive -hh --partial --info = stats1,progress2 --modify-window = 1 --remove-source-files"; - rg = - "rg -i --colors 'match:bg:yellow' --colors 'match:fg:black' --colors 'match:style:nobold' --colors 'path:fg:green' --colors 'path:style:bold' --colors 'line:fg:yellow' --colors 'line:style:bold'"; - ag = - "rg -i --colors 'match:bg:yellow' --colors 'match:fg:black' --colors 'match:style:nobold' --colors 'path:fg:green' --colors 'path:style:bold' --colors 'line:fg:yellow' --colors 'line:style:bold'"; + cpr = "rsync --archive -hh --partial --info = stats1,progress2 --modify-window = 1"; + mvr = "rsync --archive -hh --partial --info = stats1,progress2 --modify-window = 1 --remove-source-files"; + rg = "rg -i --colors 'match:bg:yellow' --colors 'match:fg:black' --colors 'match:style:nobold' --colors 'path:fg:green' --colors 'path:style:bold' --colors 'line:fg:yellow' --colors 'line:style:bold'"; + ag = "rg -i --colors 'match:bg:yellow' --colors 'match:fg:black' --colors 'match:style:nobold' --colors 'path:fg:green' --colors 'path:style:bold' --colors 'line:fg:yellow' --colors 'line:style:bold'"; reset = "tput reset"; }; history.size = 10000000; diff --git a/home/zsh/files/functions.zsh b/home/zsh/files/functions.zsh index 6ef1f27..1bc6094 100644 --- a/home/zsh/files/functions.zsh +++ b/home/zsh/files/functions.zsh @@ -1,89 +1,3 @@ -function cd_to() { - setopt localoptions pipefail no_aliases 2> /dev/null - local dir=$1 - if [[ -z "$dir" ]]; then - zle redisplay - return 0 - fi - zle push-line # Clear buffer. Auto-restored on next prompt. - cd $dir - zle accept-line - local ret=$? - unset dir # ensure this doesn't end up appearing in prompt expansion - zle reset-prompt -} - -function find_local() { - fd . -t $1 | fzgo -} - -function find_global() { - file_type=$1 - mkdir -p ~/.cache/fzgo_paths - FZGO_CACHE=~/.cache/fzgo_paths/$file_type - FZGO_RECENTS=~/.cache/fzgo_paths/recents_$file_type - if [ -f $FZGO_CACHE ]; then - if [ $(stat -c %Y $FZGO_CACHE) -lt $(date +%s -d '-1 day') ]; then - rm $FZGO_CACHE - fi - fi - if [ ! -f $FZGO_CACHE ]; then - PATHS="" - for i in "$LOCAL_SEARCH_DIRS[@]"; do - if [ -d "$i" ]; then - PATHS="$PATHS\n$i\n$(fd . $i -t $file_type)" - fi - done - for i in "$REMOTE_SEARCH_DIRS[@]"; do - if [ -d "$i" ]; then - PATHS="$PATHS\n$i\n$(ssh mallorea fd . \"$i\" -t $file_type -d 15)" - fi - done - echo "$PATHS" > $FZGO_CACHE - fi - touch $FZGO_RECENTS - file="$(cat $FZGO_RECENTS $FZGO_CACHE | awk '!x[$0]++' | fzgo)" - echo "$file" | cat - $FZGO_RECENTS | awk '!x[$0]++' > /tmp/fzgo_paths_recents_$file_type && mv /tmp/fzgo_paths_recents_$file_type $FZGO_RECENTS - echo "$file" -} - -function open_path() { - file_type=$1 - search_fn=$2 - selection="$($search_fn $file_type)" - if [ "$selection" = "" ]; then - zle reset-prompt - return - fi - if [ "$LBUFFER" = "" ]; then - if [ "$file_type" = "d" ]; then - cd_to "$selection" - else - selection=$(realpath "$selection") - parent=$(dirname "$selection") - cd_to "$parent" - nvim "$selection" - fi - else - emulate -L zsh - zle -I - LBUFFER="${LBUFFER}\"$selection\"" - zle reset-prompt - fi -} - -function open_global_path_d() { open_path d find_global } -function open_global_path_f() { open_path f find_global } -function open_local() { open_path f find_local } - -function register_key() { - zle -N $2 - bindkey "^$1" $2 - bindkey -M emacs "^$1" $2 - bindkey -M vicmd "^$1" $2 - bindkey -M viins "^$1" $2 -} - function push() { git add -A if [ -z "$*" ]; then @@ -93,3 +7,7 @@ function push() { fi git push } + +function run() { + NIXPKGS_ALLOW_UNFREE=1 nix run --impure "nixpkgs#$1" -- "''${@:2}" +} diff --git a/home/zsh/files/fzgo.zsh b/home/zsh/files/fzgo.zsh new file mode 100644 index 0000000..350248b --- /dev/null +++ b/home/zsh/files/fzgo.zsh @@ -0,0 +1,106 @@ +local function cd_to() { + setopt localoptions pipefail no_aliases 2> /dev/null + local dir=$1 + if [[ -z "$dir" ]]; then + zle redisplay + return 0 + fi + zle push-line # Clear buffer. Auto-restored on next prompt. + cd $dir + zle accept-line + local ret=$? + unset dir # ensure this doesn't end up appearing in prompt expansion + zle reset-prompt +} + +local function get_entries() { + file_type=$1 + if [ ! -d "$XDG_CONFIG_HOME/fzgo/entries" ]; then + return + fi + for entry in "$XDG_CONFIG_HOME/fzgo/entries/$file_type"/*; do + # If the file is a symlink to a directory, we want to list the directory + if [ -L "$entry" ]; then + target=$(realpath "$entry") + if [ -d "$target" ]; then + fd . "$target" -t "$file_type" + fi + else + cat "$entry" + fi + done +} + +function fzgo_update_cache() { + for file_type_dir in "$XDG_CONFIG_HOME/fzgo/entries"/*; do + mkdir -p "$XDG_CACHE_HOME/fzgo/entries" + for entry in "$file_type_dir"/*; do + file_type=$(basename "$file_type_dir") + mkdir -p "$XDG_CACHE_HOME/fzgo/entries/$file_type" + # If the file is a symlink to a directory, we want to list the directory + if [ -L "$entry" ]; then + target=$(realpath "$entry") + if [ -d "$target" ]; then + out_file="$(echo "$target" | sed 's\/\|\g')" + out_file="$XDG_CACHE_HOME/fzgo/entries/$file_type/$out_file" + mkdir -p "$(dirname \"$out_file\")" + fd . "$target" -t "$file_type" > "$out_file" + fi + fi + done + done +} + + +local function find_global() { + file_type=$1 + FZGO_RECENTS=$XDG_CACHE_HOME/fzgo/recent_paths_$file_type + mkdir -p $(dirname $FZGO_RECENTS) + touch $FZGO_RECENTS + FZGO_RECENT_ENTRIES=$(cat $FZGO_RECENTS) + FZGO_ENTRIES=$(get_entries $file_type) + file="$(echo $FZGO_RECENT_ENTRIES'\n'$FZGO_ENTRIES | awk '!x[$0]++' | fzgo)" + echo "$file" | cat - $FZGO_RECENTS | awk '!x[$0]++' > /tmp/fzgo_paths_recents_$file_type && mv /tmp/fzgo_paths_recents_$file_type $FZGO_RECENTS + echo "$file" +} + +local function find_local() { + fd . -t $1 | fzgo +} + +local function open_path() { + file_type=$1 + search_fn=$2 + selection="$($search_fn $file_type)" + if [ "$selection" = "" ]; then + zle reset-prompt + return + fi + if [ "$LBUFFER" = "" ]; then + if [ "$file_type" = "d" ]; then + cd_to "$selection" + else + selection=$(realpath "$selection") + parent=$(dirname "$selection") + cd_to "$parent" + $EDITOR "$selection" + fi + else + emulate -L zsh + zle -I + LBUFFER="${LBUFFER}\"$selection\"" + zle reset-prompt + fi +} + +function fzgo_global_d() { open_path d find_global } +function fzgo_global_f() { open_path f find_global } +function fzgo_local_f() { open_path f find_local } + +function fzgo_register_key() { + zle -N $2 + bindkey "^$1" $2 + bindkey -M emacs "^$1" $2 + bindkey -M vicmd "^$1" $2 + bindkey -M viins "^$1" $2 +} diff --git a/home/zsh/files/keys.zsh b/home/zsh/files/keys.zsh index b1828c5..931c5e5 100644 --- a/home/zsh/files/keys.zsh +++ b/home/zsh/files/keys.zsh @@ -39,6 +39,6 @@ bindkey -M vicmd "^R" fzf-history-widget bindkey -M viins "^R" fzf-history-widget bindkey -M emacs "^R" fzf-history-widget -register_key "T" open_global_path_d -register_key "E" open_global_path_f -register_key "W" open_local +fzgo_register_key "T" fzgo_global_d +fzgo_register_key "E" fzgo_global_f +fzgo_register_key "W" fzgo_local_f