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.

elvmv 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env elvish
  2. use re
  3. fn call [a @b]{
  4. list = [(splits ' ' $a)]
  5. bin = (external $list[0])
  6. rest = [$@b]
  7. if (> (count $list) 1) {
  8. rest = [$list[1:] $@b]
  9. }
  10. $bin $@rest
  11. }
  12. fn list-items [a]{
  13. if (-is-dir $a) { put $a/* } else { put $a }
  14. }
  15. items = []
  16. if (== (count $args) 0) {
  17. items = [./*]
  18. } else {
  19. for i $args {
  20. if (eq $i '-') {
  21. each [x]{ items = [$@items (list-items $x)] }
  22. } else {
  23. items = [$@items (list-items $i)]
  24. }
  25. }
  26. }
  27. high = (- (count $items) 1)
  28. file = (mktemp -p /tmp elvmvXXXXX)
  29. print > $file
  30. c = 0
  31. for i $items {
  32. echo $c"\t"$items[$c] >> $file
  33. c = (+ $c 1)
  34. }
  35. editor = vi
  36. if (has-env VISUAL) {
  37. editor = (get-env VISUAL)
  38. } elif (has-env EDITOR) {
  39. editor = (get-env EDITOR)
  40. }
  41. fn abort [&code=1 a]{
  42. echo $a >&2
  43. rm $file
  44. exit $code
  45. }
  46. try {
  47. call $editor $file
  48. } except {
  49. abort 'Editor exited with error. Aborting...'
  50. }
  51. cat $file | each [l]{
  52. matches = (re:find '^(\d+)\t?(.*)$' $l)
  53. if (not (eq $matches '')) {
  54. id = $matches[groups][1][text]
  55. name = $matches[groups][2][text]
  56. src = $items[$id]
  57. if (> $id $high) {
  58. abort 'Unknown item id: '$id'. Aborting...'
  59. } elif (not (eq $src $name)) {
  60. mkdir -p (path-dir $name)
  61. mv $items[$id] $name
  62. }
  63. } elif (re:match '^\s*$' $l) {
  64. continue
  65. } else {
  66. abort 'Couldn''t parse line "'$l'". Aborting...'
  67. }
  68. }
  69. rm $file