My scripts for startup, dmenu, and the command line
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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