add bash-only version

This commit is contained in:
jan 2017-01-25 17:20:27 -08:00
parent d96cba52df
commit f2790b54ca
2 changed files with 80 additions and 96 deletions

View File

@ -1,96 +0,0 @@
#!/usr/bin/env python3
# Mark/Recall script. Uses fuzzywuzzy for fuzzy matching.
import sys
import os
from collections import OrderedDict
DATA_DIR = os.getenv('XDG_DATA_DIR')
if DATA_DIR is None:
DATA_DIR = os.path.expanduser('~/.local/share')
MARK_FILE = DATA_DIR + '/.mark_recall'
os.makedirs(DATA_DIR)
with open(MARK_FILE, 'r+') as f:
pass
def mark(dir=None):
if dir is None:
dir = os.getcwd()
with open(MARK_FILE, 'r+') as f:
path_list = [dir] + f.readlines()
path_list = list(OrderedDict.fromkeys(path_list))
f.writelines(path_list)
def recall(n=0):
with open(MARK_FILE, 'r') as f:
path_list = f.readlines()
os.chdir(path_list[n])
def bamf(fuzzy_path):
choices = get_fuzzy(fuzzy_path, 1)
os.chdir(choices[0])
def list(n=10):
paths = get_list(n)
[print(p) for p in paths]
def get_fuzzy(fuzzy_path, n=1):
import fuzzywuzzy
import fuzzywuzzy.process
with open(MARK_FILE, 'r') as f:
path_list = f.readlines()
choices = fuzzywuzzy.process.extract(fuzzy_path, path_list, limit=n
scorer=fuzzywuzzy.token_sort_ratio)
return [c[0] for c in choices]
def get_list(n=10):
path_list = []
with open(MARK_FILE, 'r') as f:
for i, line in enumerate(f):
path_list.append(line)
if n-1 == i:
break
return path_list
if __name__ == '__main__':
args = list(sys.argv)
cmd = args.pop(0)
if cmd == 'mark':
if args:
mark(args.pop(0))
else:
mark()
elif cmd == 'recall':
if args:
recall(args.pop(0))
else:
recall()
elif cmd == 'bamf':
if args:
bamf(args.join(' '))
else:
recall()
elif cmd == 'marklist:
if args:
list(args.pop(0))
else:
list()

80
mark_recall.sh Normal file
View File

@ -0,0 +1,80 @@
### Mark and recall
#
# Jan Petykiewicz 2017-01-26
#
# Functions:
# mark : Mark a path for later recall.
# If no directory is given as an argument, marks the current working directory.
# mlist : List marked directories.
# Entries are unique and sorted most-to-least recent by mark time.
# recall: Return to a previously-marked directory.
# Pass a number to specify an entry in mlist; default is 1 (most recently marked).
# bamf: Return to a marked directory using a fuzzy search, e.g.
# ```bamf proj``` may match /home/username/projects/
#
# Use of bamf requires python3 and fuzzywuzzy.
#
if [ -z $XDG_DATA_DIR ]; then
dir=$HOME/.local/share
else
dir=$XDG_DATA_DIR
fi
mkdir -p $dir
MARK_RECALL_FILE=$dir/.mark_recall
touch $MARK_RECALL_FILE
MARK_RECALL_MEMORY=100
mark() {
# If no arg given, add current dir
if [ "$#" -lt 1 ]; then
dir="$PWD"
else
dir="$(cd "$1" && pwd)"
fi
# cat inside echo to avoid overwriting before we read
new_list="$dir
$(cat $MARK_RECALL_FILE)"
# Remove duplicates, keep a limited number, drop empty lines, and write to file
echo "$new_list" \
| awk '!a[$0]++' - \
| head -n $MARK_RECALL_MEMORY \
| sed '/^[[:space:]]*$/d' \
> $MARK_RECALL_FILE
}
recall() {
if [ "$#" -lt 1 ]; then
cd $(head -n 1 $MARK_RECALL_FILE)
else
cd $(sed "${1}q;d" $MARK_RECALL_FILE)
fi
}
mlist() {
cat -n $MARK_RECALL_FILE
}
bamf() {
if [ "$#" -lt 1 ]; then
recall
else
dir=$(python3 -W ignore -c "
import sys
from fuzzywuzzy import process, fuzz
with open('$MARK_RECALL_FILE') as f:
path_list = f.readlines()
print(process.extractOne(' '.join(sys.argv[1:]), path_list,
scorer=fuzz.token_sort_ratio)[0][:-1])
" "$@")
cd $dir
fi
}