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.

89 lines
2.7 KiB

  1. #!/bin/sh
  2. #To add more caches, add a newline to this string and follow the existing name:path format
  3. cache_info="home:$HOME"
  4. HOME=/home/immanuel
  5. XDG_CACHE_HOME="$HOME/.cache"
  6. init() {
  7. mkdir -p $XDG_CACHE_HOME/mru
  8. file=$XDG_CACHE_HOME/mru/"$1"
  9. if [ -z "$1" ]; then file="$file""home"; fi
  10. path=$(printf $cache_info | grep "^$1" - | cut -d: -f2)
  11. if [ -z "$path" -o ! -e "$path" ]; then
  12. printf "path:$path from cache:$file does not exist\n" 1>&2; exit;
  13. fi
  14. find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
  15. -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
  16. '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new'
  17. mv "$file.new" "$file"
  18. }
  19. get_cache_path() {
  20. if [ -z "$1" ]; then printf "no arg to get_cache_path\n" 1>&2; exit; fi
  21. path="$(printf $cache_info | grep "^$1" - | cut -d: -f2)"
  22. if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
  23. printf "$path"
  24. }
  25. update() {
  26. clean
  27. file=$XDG_CACHE_HOME/mru/"$1"
  28. if [ -z "$1" ]; then file="$file""home"; fi
  29. path=$(printf $cache_info | grep "^$1" - | cut -d: -f2)
  30. if [ -z "$path" -o ! -e "$path" ]; then
  31. printf "path:$path from cache:$file does not exist\n"; exit;
  32. fi
  33. #init a temporary file. The traverse the original, if a file exists in a
  34. # but not in b, delete it from a, if it exists in a and b delete it from b.
  35. # append the rest of b to a.
  36. # This should check if each file found is already there. If not, add it to it's position based on modified time
  37. # find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
  38. # -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
  39. # '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file
  40. }
  41. output() {
  42. if [ -z "$1" ]; then
  43. cat $XDG_CACHE_HOME/mru/home; else cat "$XDG_CACHE_HOME/mru/$1";
  44. fi
  45. }
  46. list() {
  47. output "$1" | cut -f3
  48. }
  49. insert() {
  50. if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
  51. cache_name=${1+'home'}
  52. cache_path=$(get_cache_path "$cache_name")
  53. file_path=$(realpath "$1")
  54. if [ -e "$file_path" ]; then
  55. # sed "0,\|.*\t.*\t$file_path|s|||" $XDG_CACHE_HOME/mru/$cache_name
  56. 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 \
  57. >$XDG_CACHE_HOME/mru/$cache_name.insert$$
  58. mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
  59. else
  60. sed "0,\|.*\t.*\t$file_path|s|||" <$XDG_CACHE_HOME/mru/$cache_name \
  61. >$XDG_CACHE_HOME/mru/$cache_name.insert$$
  62. mv $XDG_CACHE_HOME/mru/$cache_name.insert$$ $XDG_CACHE_HOME/mru/$cache_name
  63. fi
  64. }
  65. clean() {
  66. rm $XDG_CACHE_HOME/mru/*.{insert,new}*
  67. }
  68. case "$1" in
  69. init) init "$2";;
  70. update) update $2;;
  71. output) output "$2";;
  72. list) list "$2";;
  73. insert) insert "$2" "$3";;
  74. clean) clean;;
  75. esac