My build of nnn with minor changes
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.
 
 
 
 
 
 

149 lignes
3.1 KiB

  1. #!/usr/bin/env bash
  2. # Description: An almost fully POSIX compliant batch file renamer
  3. #
  4. # Note: nnn auto-detects and invokes this plugin if available
  5. #
  6. # Capabilities:
  7. # 1. Basic file rename
  8. # 2. Detects order change
  9. # 3. Can move files
  10. # 4. Can remove files
  11. # 5. Switch number pairs to swap filenames
  12. #
  13. # Shell: bash
  14. # Author: KlzXS
  15. EDITOR="${EDITOR:-vi}"
  16. TMPDIR="${TMPDIR:-/tmp}"
  17. INCLUDE_HIDDEN="${INCLUDE_HIDDEN:-0}"
  18. VERBOSE="${VERBOSE:-0}"
  19. selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
  20. exit_status=0
  21. dst_file=$(mktemp "$TMPDIR/.nnnXXXXXX")
  22. if [ -s "$selection" ]; then
  23. printf "Rename 'c'urrent / 's'election? "
  24. read -r resp
  25. if ! [ "$resp" = "c" ] && ! [ "$resp" = "s" ]; then
  26. exit 1
  27. fi
  28. fi
  29. if [ "$resp" = "s" ]; then
  30. arr=$(tr '\0' '\n' < "$selection")
  31. else
  32. if [ "$INCLUDE_HIDDEN" -eq 0 ]; then
  33. arr=$(find . ! -name . -prune ! -name ".*" -print | sort)
  34. else
  35. arr=$(find . ! -name . -prune -print | sort)
  36. fi
  37. fi
  38. printf "%s" "$arr" | awk '{print NR " " $0}' > "$dst_file"
  39. items=("~")
  40. while IFS='' read -r line; do
  41. items+=("$line");
  42. done < <(printf "%s\n" "$arr")
  43. $EDITOR "$dst_file"
  44. while read -r num name; do
  45. if [ -z "$name" ]; then
  46. if [ -z "$num" ]; then
  47. continue
  48. fi
  49. printf "%s: unable to parse line, aborting\n" "$0"
  50. exit 1
  51. fi
  52. # check if $num is an integer
  53. if [ ! "$num" -eq "$num" ] 2> /dev/null; then
  54. printf "%s: unable to parse line, aborting\n" "$0"
  55. exit 1
  56. fi
  57. src=${items[$num]}
  58. if [ -z "$src" ]; then
  59. printf "%s: unknown item number %s\n" "$0" "$num" > /dev/stderr
  60. continue
  61. elif [ "$name" != "$src" ]; then
  62. if [ -z "$name" ]; then
  63. continue
  64. fi
  65. if [ ! -e "$src" ] && [ ! -L "$src" ]; then
  66. printf "%s: %s does not exit\n" "$0" "$src" > /dev/stderr
  67. unset "items[$num]"
  68. continue
  69. fi
  70. # handle swaps
  71. if [ -e "$name" ] || [ -L "$name" ]; then
  72. tmp="$name~"
  73. c=0
  74. while [ -e "$tmp" ] || [ -L "$tmp" ]; do
  75. c=$((c+1))
  76. tmp="$tmp~$c"
  77. done
  78. if mv "$name" "$tmp"; then
  79. if [ "$VERBOSE" -ne 0 ]; then
  80. printf "'%s' -> '%s'\n" "$name" "$tmp"
  81. fi
  82. else
  83. printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
  84. exit_status=1
  85. fi
  86. for key in "${!items[@]}"; do
  87. if [ "${items[$key]}" = "$name" ]; then
  88. items[$key]="$tmp"
  89. fi
  90. done
  91. fi
  92. dir=$(dirname "$name")
  93. if [ ! -d "$dir" ] && ! mkdir -p "$dir"; then
  94. printf "%s: failed to create directory tree %s\n" "$0" "$dir" > /dev/stderr
  95. exit_status=1
  96. elif ! mv "$src" "$name"; then
  97. printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
  98. exit_status=1
  99. else
  100. if [ -d "$name" ]; then
  101. for key in "${!items[@]}"; do
  102. items[$key]=$(printf "%s" "${items[$key]}" | sed "s|^$src\(\$\|\/\)|$name\1|")
  103. done
  104. if [ "$VERBOSE" -ne 0 ]; then
  105. printf "'%s' => '%s'\n" "$src" "$name"
  106. fi
  107. else
  108. true
  109. if [ "$VERBOSE" -ne 0 ]; then
  110. printf "'%s' -> '%s'\n" "$src" "$name"
  111. fi
  112. fi
  113. fi
  114. fi
  115. unset "items[$num]"
  116. done <"$dst_file"
  117. unset "items[0]"
  118. for item in "${items[@]}"; do
  119. rm -ri "$item"
  120. done
  121. rm "$dst_file"
  122. exit $exit_status