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.

100 lines
2.4 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. file=XDG_CACHE_HOME/mru/$1
  10. create_new "$1"
  11. mv "$file.new" "$file"
  12. touch $XDG_CACHE_HOME/mru/$file.recent
  13. }
  14. get_cache_path() {
  15. if [ -z "$1" ]; then printf "no arg to get_cache_path\n" 1>&2; exit; fi
  16. path="$(printf $cache_info | grep "^$1" - | cut -d: -f2)"
  17. if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
  18. printf "$path"
  19. }
  20. create_new() {
  21. file=$XDG_CACHE_HOME/mru/"$1"
  22. path=$(get_cache_path "$1")
  23. find $path -type f -not \( -path '*/.*/*' -o -path '*node_modules/*' -o \
  24. -path '*Backups/my-plugins*' -o -path '*.sw[po]' \) -printf \
  25. '%TY-%Tm-%Td\t%TT\t%p\n' | sort -r > $file'.new'
  26. }
  27. update() {
  28. file=$XDG_CACHE_HOME/mru/$1
  29. name="$1"
  30. if [ -z "$1" ]; then name='home'; fi
  31. create_new "$name"
  32. mv "$file.new" "$file"
  33. rm_recent "$1"
  34. }
  35. # This remove duplicates by deleting files in recent from the main file
  36. rm_recent() {
  37. file=$XDG_CACHE_HOME/mru/$1
  38. if [ -z "$1" ]; then file=$XDG_CACHE_HOME/mru/home; fi
  39. while read line
  40. do
  41. l=$(printf "$line" | cut -f3)
  42. if [ ! -z $l ]; then sed -i "\|.*\t.*\t$l|d" $file; fi
  43. done <$file.recent
  44. }
  45. output() {
  46. if [ -z "$1" ]; then
  47. file1="$XDG_CACHE_HOME/mru/home.recent"; file2="$XDG_CACHE_HOME/mru/home"
  48. else
  49. file1="$XDG_CACHE_HOME/mru/$1.recent"; file2="$XDG_CACHE_HOME/mru/$1"
  50. fi
  51. cat $file1; printf "\n"; cat $file2
  52. }
  53. list() {
  54. output "$1" | cut -f3
  55. }
  56. insert() {
  57. if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
  58. cache_name=$2
  59. if [ -z "$2" ]; then cache_name=home; fi
  60. cache_path=$(get_cache_path "$cache_name")
  61. file_path=$(realpath "$1")
  62. if [ -e "$file_path" ]; then
  63. sed -i -e "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name.recent
  64. if [ -s $XDG_CACHE_HOME/mru/$cache_name.recent ]; then
  65. echo "sed starts$(date '+%Y-%m-%d%t%T')\t$file_path\n"
  66. sed -i "1i$(date '+%Y-%m-%d%t%T')\t$file_path" $XDG_CACHE_HOME/mru/$cache_name.recent
  67. echo 'sed ends'
  68. else
  69. printf "$(date '+%Y-%m-%d%t%T')\t$file_path" >$XDG_CACHE_HOME/mru/$cache_name.recent
  70. fi
  71. fi
  72. if [ ! -z $file_path ]; then
  73. sed -i "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name
  74. fi
  75. }
  76. case "$1" in
  77. init) init "$2";;
  78. update) update $2;;
  79. output) output "$2";;
  80. list) list "$2";;
  81. insert) insert "$2" "$3";;
  82. esac