My scripts for startup, dmenu, and the command line
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.9 KiB

  1. #!/bin/sh
  2. #To add more caches, add a newline to this string and follow the existing name:path format
  3. #Edit create_new to change find behaviour
  4. cache_info="home:$HOME"
  5. HOME=/home/immanuel
  6. XDG_CACHE_HOME="$HOME/.cache"
  7. init() {
  8. mkdir -p $XDG_CACHE_HOME/mru
  9. create_new "$1"
  10. mv "$file.new" "$file"
  11. }
  12. get_cache_path() {
  13. if [ -z "$1" ]; then printf "no arg to get_cache_path\n" 1>&2; exit; fi
  14. path="$(printf $cache_info | grep "^$1" - | cut -d: -f2)"
  15. if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
  16. printf "$path"
  17. }
  18. create_new() {
  19. file=$XDG_CACHE_HOME/mru/"$1"
  20. path=$(get_cache_path "$1")
  21. find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
  22. -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
  23. '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new'
  24. }
  25. update() {
  26. clean
  27. name="$1"
  28. if [ -z "$1" ]; then
  29. name='home'
  30. fi
  31. create_new "$name"
  32. }
  33. output() {
  34. if [ -z "$1" ]; then
  35. cat $XDG_CACHE_HOME/mru/home; else cat "$XDG_CACHE_HOME/mru/$1";
  36. fi
  37. }
  38. list() {
  39. output "$1" | cut -f3
  40. }
  41. insert() {
  42. if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
  43. cache_name=${1+'home'}
  44. cache_path=$(get_cache_path "$cache_name")
  45. file_path=$(realpath "$1")
  46. if [ -e "$file_path" ]; then
  47. # sed "0,\|.*\t.*\t$file_path|s|||" $XDG_CACHE_HOME/mru/$cache_name
  48. sed -e "0,\|.*\t.*\t$file_path|s|||" -e "1s;^;$(date '+%Y-%m-%d%t%T')\t$file_path\n;" <$XDG_CACHE_HOME/mru/$cache_name \
  49. >$XDG_CACHE_HOME/mru/$cache_name.insert$$
  50. mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
  51. else
  52. sed "0,\|.*\t.*\t$file_path|s|||" <$XDG_CACHE_HOME/mru/$cache_name \
  53. >$XDG_CACHE_HOME/mru/$cache_name.insert$$
  54. mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
  55. fi
  56. }
  57. clean() {
  58. rm $XDG_CACHE_HOME/mru/*.{insert,new}*
  59. }
  60. case "$1" in
  61. init) init "$2";;
  62. update) update $2;;
  63. output) output "$2";;
  64. list) list "$2";;
  65. insert) insert "$2" "$3";;
  66. clean) clean;;
  67. esac