Last active
January 22, 2021 21:01
-
-
Save nick133/4454fa85120bbd7d5b5dddb953d19563 to your computer and use it in GitHub Desktop.
Advanced finder plugin for awesome NNN file manager (command selection and history)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Description: Advanced file finder plugin for awesome NNN file manager | |
# | |
# Features: | |
# - Predefined commands selection menu | |
# - History of unique commands | |
# | |
# Requires: bash, fzf, [fd] | |
# | |
# Bugs: Filenames with spaces brakes grep. Please report if you find some more. | |
# | |
# Author: nick133 | |
history_file="$HOME/.config/nnn/history/find_commands.hst" | |
history_maxlines=12 | |
find_commands=( | |
'' '* Hint: Press Escape to cancel' '' | |
'[ History ]' | |
'grep -lZ "???" `fd --type f -HL ".*$"`' | |
'grep -lZF "main()" `fd -t f -HLg "*.c"`' | |
'fd -HL0g "*.*"' | |
'fd -HL0 --size +1M ".*$"' | |
'fd -HL0 --max-depth 1 ".*$"' | |
'fd -HL0 --change-newer-than "2021-01-12 10:00:00" ".*$"' | |
'fd -HL0 --change-older-than "2021-01-12 10:00:00" ".*$"' | |
) | |
fuzzy() { | |
title=$1 | |
shift | |
if [[ -n "$1" ]] | |
then | |
lines=$1 | |
shift | |
else | |
lines=3 | |
fi | |
fzf -i -e --layout=reverse --info=hidden \ | |
--bind home:page-up --bind end:page-down \ | |
--header="$title" --header-lines=$lines --prompt='' --pointer='>>' $@ | |
} | |
select_command=$(for cmd in "${find_commands[@]}" | |
do printf "$cmd\n" | |
done | fuzzy " <<< Find files / Regex search >>>") | |
mkdir -p "$(dirname $history_file)" | |
[[ -z "$select_command" ]] && exit 1 | |
if [[ "$select_command" == "[ History ]" ]] | |
then | |
select_command="$(sort -r $history_file | fuzzy [\ History\ ] 0)" | |
[[ -z "$select_command" ]] && exit 1 | |
fi | |
clear && echo | |
read -p "==> Edit command: " -re -i "$select_command" cmd | |
if [[ -n "$cmd" ]] | |
then | |
hist_lines=$(wc -l $history_file | awk '{ print $1 }') | |
if [[ "$hist_lines" -eq $history_maxlines ]] | |
then | |
tail -n $(($history_maxlines - 1)) $history_file > $history_file.new | |
mv -f $history_file.new $history_file | |
fi | |
echo $cmd >> $history_file | |
uniq $history_file > $history_file.new | |
mv -f $history_file.new $history_file | |
printf "%s" "+l" > "$NNN_PIPE" | |
eval "$cmd" > "$NNN_PIPE" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment