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

151 řádky
3.2 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. # Whitespace is used as delimiter for read.
  6. # The plugin doesn't support filenames with leading or trailing whitespace
  7. #
  8. # Capabilities:
  9. # 1. Basic file rename
  10. # 2. Detects order change
  11. # 3. Can move files
  12. # 4. Can remove files
  13. # 5. Switch number pairs to swap filenames
  14. #
  15. # Shell: bash
  16. # Author: KlzXS
  17. EDITOR="${EDITOR:-vi}"
  18. TMPDIR="${TMPDIR:-/tmp}"
  19. INCLUDE_HIDDEN="${INCLUDE_HIDDEN:-0}"
  20. VERBOSE="${VERBOSE:-0}"
  21. selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
  22. exit_status=0
  23. dst_file=$(mktemp "$TMPDIR/.nnnXXXXXX")
  24. if [ -s "$selection" ]; then
  25. resp="s"
  26. else
  27. resp="c"
  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. lines=$(printf "%s\n" "$arr" | wc -l)
  39. width=${#lines}
  40. printf "%s" "$arr" | awk '{printf("%'"${width}"'d %s\n", NR, $0)}' > "$dst_file"
  41. items=("~")
  42. while IFS='' read -r line; do
  43. items+=("$line");
  44. done < <(printf "%s\n" "$arr")
  45. $EDITOR "$dst_file"
  46. while read -r num name; do
  47. if [ -z "$name" ]; then
  48. if [ -z "$num" ]; then
  49. continue
  50. fi
  51. printf "%s: unable to parse line, aborting\n" "$0"
  52. exit 1
  53. fi
  54. # check if $num is an integer
  55. if [ ! "$num" -eq "$num" ] 2> /dev/null; then
  56. printf "%s: unable to parse line, aborting\n" "$0"
  57. exit 1
  58. fi
  59. src=${items[$num]}
  60. if [ -z "$src" ]; then
  61. printf "%s: unknown item number %s\n" "$0" "$num" > /dev/stderr
  62. continue
  63. elif [ "$name" != "$src" ]; then
  64. if [ -z "$name" ]; then
  65. continue
  66. fi
  67. if [ ! -e "$src" ] && [ ! -L "$src" ]; then
  68. printf "%s: %s does not exit\n" "$0" "$src" > /dev/stderr
  69. unset "items[$num]"
  70. continue
  71. fi
  72. # handle swaps
  73. if [ -e "$name" ] || [ -L "$name" ]; then
  74. tmp="$name~"
  75. c=0
  76. while [ -e "$tmp" ] || [ -L "$tmp" ]; do
  77. c=$((c+1))
  78. tmp="$tmp~$c"
  79. done
  80. if mv "$name" "$tmp"; then
  81. if [ "$VERBOSE" -ne 0 ]; then
  82. printf "'%s' -> '%s'\n" "$name" "$tmp"
  83. fi
  84. else
  85. printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
  86. exit_status=1
  87. fi
  88. for key in "${!items[@]}"; do
  89. if [ "${items[$key]}" = "$name" ]; then
  90. items[$key]="$tmp"
  91. fi
  92. done
  93. fi
  94. dir=$(dirname "$name")
  95. if [ ! -d "$dir" ] && ! mkdir -p "$dir"; then
  96. printf "%s: failed to create directory tree %s\n" "$0" "$dir" > /dev/stderr
  97. exit_status=1
  98. elif ! mv "$src" "$name"; then
  99. printf "%s: failed to rename %s to %s: %s\n" "$0" "$name" "$tmp" "$!" > /dev/stderr
  100. exit_status=1
  101. else
  102. if [ -d "$name" ]; then
  103. for key in "${!items[@]}"; do
  104. items[$key]=$(printf "%s" "${items[$key]}" | sed "s|^$src\(\$\|\/\)|$name\1|")
  105. done
  106. if [ "$VERBOSE" -ne 0 ]; then
  107. printf "'%s' => '%s'\n" "$src" "$name"
  108. fi
  109. else
  110. true
  111. if [ "$VERBOSE" -ne 0 ]; then
  112. printf "'%s' -> '%s'\n" "$src" "$name"
  113. fi
  114. fi
  115. fi
  116. fi
  117. unset "items[$num]"
  118. done <"$dst_file"
  119. unset "items[0]"
  120. for item in "${items[@]}"; do
  121. rm -ri "$item"
  122. done
  123. rm "$dst_file"
  124. exit $exit_status