My scripts for startup, dmenu, and the command line
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. updatedirs() {
  36. cache_name="$1"
  37. if [ -z "$1" ]; then cache_name=home; fi
  38. path="$(printf $cache_info | grep "^$cache_name" - | cut -d: -f2)"
  39. if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
  40. list "$1" | sed -n 's:\(.*\)/.*$:\1:p' | sort -u >$XDG_CACHE_HOME/mru/"$cache_name.dirs"
  41. }
  42. # This remove duplicates by deleting files in recent from the main file
  43. rm_recent() {
  44. file=$XDG_CACHE_HOME/mru/$1
  45. if [ -z "$1" ]; then file=$XDG_CACHE_HOME/mru/home; fi
  46. while read line
  47. do
  48. l=$(printf "$line" | cut -f3)
  49. if [ ! -z $l ]; then sed -i "\|.*\t.*\t$l|d" $file; fi
  50. done <$file.recent
  51. }
  52. output() {
  53. if [ -z "$1" ]; then
  54. file1="$XDG_CACHE_HOME/mru/home.recent"; file2="$XDG_CACHE_HOME/mru/home"
  55. else
  56. file1="$XDG_CACHE_HOME/mru/$1.recent"; file2="$XDG_CACHE_HOME/mru/$1"
  57. fi
  58. cat $file1; printf "\n"; cat $file2
  59. }
  60. list() {
  61. output "$1" | cut -f3
  62. }
  63. listdirs() {
  64. cache_name="$1"
  65. if [ -z "$1" ]; then cache_name=home; fi
  66. path="$(printf $cache_info | grep "^$cache_name" - | cut -d: -f2)"
  67. if [ -z "$path" ]; then printf "invalid cache name: $1\n" 1>&2; exit; fi
  68. cat $XDG_CACHE_HOME/mru/"$cache_name.dirs"
  69. }
  70. insert() {
  71. if [ -z "$1" ]; then printf "No path given\n" 1>&2; exit; fi
  72. cache_name=$2
  73. if [ -z "$2" ]; then cache_name=home; fi
  74. cache_path=$(get_cache_path "$cache_name")
  75. file_path=$(realpath "$1")
  76. if [ -e "$file_path" ]; then
  77. sed -i -e "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name.recent
  78. if [ -s $XDG_CACHE_HOME/mru/$cache_name.recent ]; then
  79. echo "sed starts$(date '+%Y-%m-%d%t%T')\t$file_path\n"
  80. sed -i "1i$(date '+%Y-%m-%d%t%T')\t$file_path" $XDG_CACHE_HOME/mru/$cache_name.recent
  81. echo 'sed ends'
  82. else
  83. printf "$(date '+%Y-%m-%d%t%T')\t$file_path" >$XDG_CACHE_HOME/mru/$cache_name.recent
  84. fi
  85. fi
  86. if [ ! -z "$file_path" ]; then
  87. echo "second sed starts"
  88. sed -i "0,\|.*\t.*\t$file_path|d" $XDG_CACHE_HOME/mru/$cache_name
  89. fi
  90. }
  91. case "$1" in
  92. init) init "$2";;
  93. update) update $2;;
  94. updatedirs) updatedirs "$2";;
  95. output) output "$2";;
  96. list) list "$2";;
  97. listdirs) listdirs "$2";;
  98. insert) insert "$2" "$3";;
  99. esac