Much stronger scripts, with shellcheck and () for function body
[iomenu.git] / io-files
blobef8844ade3ed86e51f00d5a9f3b405dc7413e512
1 # Prompt a file to open in PAGER, with an history.  In less(1), 'v' to edit.
4 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
7 get_path()
9         {
10                 printf '#\n# Recent files\n'
11                 [ -f "$CACHE/iomenu/files" ] && tac "$CACHE/iomenu/files"
13                 printf '#\n# Current directory\n'
14                 find "$PWD" -maxdepth 1 -type f
16                 printf '#\n# All files\n'
17                 find "$HOME" -type f ! -path '*/.cache/*' ! -path '*/.git/*'
19         } | sed "s|$HOME|~|" | iomenu -l 256 -s '#' | sed "s|~|$HOME|" |
20                 tee -a "$CACHE/iomenu/files"
23 update_history()
25         sort "$CACHE/iomenu/files" | uniq -d | while IFS='' read -r f
26         do
27                 printf '%s\n' "$(grep -Fxv "$f" "$CACHE/iomenu/files")" "$f" \
28                         > "$CACHE/iomenu/files"
29         done
31         printf %s "$(tail "$CACHE/iomenu/files")" > "$CACHE/iomenu/files"
35 main()
37         mkdir -p "$CACHE/iomenu"
39         file="$(get_path)"
41         update_history
43         [ -f "$file" ] && exec $EDITOR "$file"
47 main "$@"