filemanager 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. about="
  3. ### Dmen File Manager #############################################
  4. # Version 0.1.2 by Scott Garrett <wintervenom [(a)] archlinux.us> #
  5. ###################################################################"
  6. ### Changelog #####################################################
  7. # 0.1.0 # * Initial version #
  8. # 0.1.1 # + Option 'Open last used dir' #
  9. # # + Options 'Always open dirs' and 'Always open files' #
  10. # 0.1.2 # + Config moved to $XDG_CONFIG_HOME #
  11. # # + Options for dmenu styling #
  12. # # + Option to change executor #
  13. # # + Option to open file manager in current directory #
  14. # # + Plugins support #
  15. ###################################################################
  16. function defaults {
  17. show_hidden=1
  18. show_backup=1
  19. always_open_files=0
  20. always_open_dirs=1
  21. open_last=1
  22. dmenu_caseinsens=1
  23. dmenu_onbottom=0
  24. dmenu_bg='#000000'
  25. dmenu_fg='#ffffff'
  26. dmenu_sbg='#000000'
  27. dmenu_sfg='#00ff00'
  28. executor='xdg-open'
  29. editor='emacsclient'
  30. filemanager='spacefm'
  31. }
  32. function saveconfig {
  33. echo "show_hidden=$show_hidden
  34. show_backup=$show_backup
  35. always_open_dirs=$always_open_dirs
  36. always_open_files=$always_open_files
  37. open_last=$open_last
  38. dmenu_caseinsens=$dmenu_caseinsens
  39. dmenu_onbottom=$dmenu_onbottom
  40. dmenu_bg='$dmenu_bg'
  41. dmenu_fg='$dmenu_fg'
  42. dmenu_sbg='$dmenu_sbg'
  43. dmenu_sfg='$dmenu_sfg'
  44. editor=\"$editor\"
  45. executor='exo'
  46. filemanager=\"$filemanager\"
  47. last_dir=\"$(pwd)\"
  48. " > "$config"
  49. }
  50. function setmenu {
  51. menu="$menu $([ "$dmenu_caseinsens" == '1' ] && echo '-i') $([ "$dmenu_onbottom" == '1' ] && echo '-b') -nb $dmenu_bg -nf $dmenu_fg -sb $dmenu_sbg -sf $dmenu_sfg"
  52. }
  53. file=1
  54. config_file="preferences"
  55. config_dir="$XDG_CONFIG_HOME/dmenfm"
  56. config="$config_dir/$config_file"
  57. plugins_dir="$XDG_CONFIG_HOME/dmenfm/plugins"
  58. menu='dmenu'
  59. [ ! -d "$config_dir" ] && mkdir "$config_dir"
  60. [ ! -d "$plugins_dir" ] && mkdir "$plugins_dir"
  61. [ ! -d "$plugins_dir/browser" ] && mkdir "$plugins_dir/browser"
  62. [ ! -d "$plugins_dir/operations" ] && mkdir "$plugins_dir/operations"
  63. [ ! -d "$plugins_dir/preferences" ] && mkdir "$plugins_dir/preferences"
  64. [ -e "$HOME/.dmanfs" ] && mv "$HOME/.dmanfs" "$config"
  65. defaults
  66. touch "$config"
  67. source "$config"
  68. setmenu
  69. [[ "$last_dir" && "$open_last" == 1 ]] && cd "$last_dir"
  70. while [ "$file" ]; do
  71. file=$((echo -e '..\n.'; ls $([[ "$show_hidden" == '1' ]] && echo '-A') $([[ "$show_backup" == '1' ]] && echo '-B'); echo -e "[Preferences]\n[Open in $filemanager]"; ls -B "$plugins_dir/browser") | $menu -p "$(basename $(pwd))")
  72. if [ "$file" ]; then
  73. if [ -e "$file" ]; then
  74. if $([ -d "$file" ] && [[ "$always_open_dirs" != '1' && "$file" != "." && "$file" != ".." ]]) || $([ -f "$file" ] && [[ "$always_open_files" != '1' ]]); then
  75. owd=$(pwd)
  76. opt=$((echo 'Open'; ([ -f "$file" ] && echo 'Edit'); echo -e 'Copy\nMove\nRename\nDelete'; ls -B "$plugins_dir/operations") | $menu -p "<$file>")
  77. else
  78. opt='Open'
  79. fi
  80. case "$opt" in
  81. 'Open')
  82. if [ -d "$file" ]; then
  83. cd "$file"
  84. else
  85. if [ "$executor" ] && which "$executor" &> /dev/null; then
  86. exec "$executor" "$owd/$file" &
  87. unset file
  88. elif which exo-open &> /dev/null; then
  89. exo-open "$owd/$file"
  90. unset file
  91. elif gnome-open &> /dev/null; then
  92. gnome-open "$owd/$file"
  93. unset file
  94. else
  95. xmessage 'No executor found. Set one in [Preferences].'
  96. fi
  97. fi
  98. ;;
  99. 'Edit')
  100. if [ "$editor" ] && which "$editor" &> /dev/null; then
  101. exec "$editor" "$owd/$file" &
  102. unset file
  103. elif which geany &> /dev/null; then
  104. exec geany "$owd/$file"
  105. unset file
  106. elif which gedit &> /dev/null; then
  107. exec gedit "$owd/$file"
  108. unset file
  109. elif which kwrite &> /dev/null; then
  110. exec kwrite "$owd/$file"
  111. unset file
  112. else
  113. xmessage 'No editor found. Set one in [Preferences].'
  114. fi
  115. ;;
  116. 'Copy')
  117. cdir=1
  118. while [ "$cdir" ]; do
  119. cdir=$((echo -e '..\n.'; ls $([[ "$show_hidden" == 1 ]] && echo "-A") $([[ "$show_backup" == 1 ]] && echo "-B") -d */) | $menu -p "Copy <$file> to:")
  120. if [[ "$cdir" ]]; then
  121. opt=$(echo -e 'Open\nCopy' | $menu -p "Copy <$file> to <$cdir>:")
  122. if [[ "$opt" == "Open" ]]; then
  123. cd "$cdir"
  124. elif [[ "$opt" == "Copy" ]]; then
  125. echo "$file"
  126. [ -e "$cdir/$file" ] && opt=$(echo -e "No\nYes" | $menu -p "Replace existing <$file>?")
  127. [[ "$opt" == "Copy" || "$opt" == "Yes" ]] && cp -af "$owd/$file" "$cdir/$file"
  128. unset cdir
  129. fi
  130. fi
  131. done
  132. ;;
  133. 'Move')
  134. cdir=1
  135. while [ "$cdir" ]; do
  136. cdir=$((echo -e '..\n.' && ls $([[ "$show_hidden" == 1 ]] && echo "-A") $([[ "$show_backup" == 1 ]] && echo "-B") -d */) | $menu -p "Move <$file> to:")
  137. if [[ "$cdir" ]]; then
  138. opt=$(echo -e 'Open\nMove' | $menu -p "Move <$file> to <$cdir>:")
  139. if [[ "$opt" == "Open" ]]; then
  140. cd "$cdir"
  141. elif [[ "$opt" == "Move" ]]; then
  142. echo "$file"
  143. [ -e "$cdir/$file" ] && opt=$(echo -e "No\nYes" | $menu -p "Replace existing <$file>?")
  144. [[ "$opt" == "Move" || "$opt" == "Yes" ]] && mv -f "$owd/$file" "$cdir/$file"
  145. unset cdir
  146. fi
  147. fi
  148. done
  149. ;;
  150. 'Rename')
  151. opt=$(echo "" | $menu -p "Rename <$file> to:")
  152. [ "$opt" ] && mv -f "$file" "$opt"
  153. ;;
  154. 'Delete')
  155. opt=$(echo -e "No\nYes" | $menu -p "Really delete <$file>?")
  156. [[ "$opt" == "Yes" ]] && rm -rf "$file"
  157. ;;
  158. *) [ "$opt" ] && [ -e "$plugins_dir/operations/$opt" ] && source "$plugins_dir/operations/$opt" ;;
  159. esac
  160. elif [[ "$file" =~ '[Open in' ]]; then
  161. which "$filemanager" &> /dev/null && exec "$filemanager" . &
  162. unset file
  163. elif [[ "$file" == '[Preferences]' ]]; then
  164. opt=1
  165. while [ "$opt" ]; do
  166. opt=$((echo "[$show_hidden] Show hidden files
  167. [$show_backup] Show backup files
  168. [$always_open_dirs] Always open dirs
  169. [$always_open_files] Always open files
  170. [$open_last] Open last used dir
  171. [$dmenu_caseinsens] Case-insensitive
  172. [$dmenu_onbottom] Display on bottom
  173. [$dmenu_bg] Background
  174. [$dmenu_fg] Foreground
  175. [$dmenu_sbg] Selected background
  176. [$dmenu_sfg] Selected foreground
  177. [$executor] Executor
  178. [$editor] Editor
  179. [$filemanager] File manager
  180. Set defaults
  181. About"; ls -B "$plugins_dir/preferences") | $menu -p "Preferences")
  182. case "$opt" in
  183. *'hidden files') (('show_hidden=!show_hidden')) ;;
  184. *'backup dirs') (('show_backup=!show_backup')) ;;
  185. *'open dirs') (('always_open_dirs=!always_open_dirs')) ;;
  186. *'open files') (('always_open_files=!always_open_files')) ;;
  187. *'used dir') (('open_last=!open_last')) ;;
  188. *'insensitive') (('dmenu_caseinsens=!dmenu_caseinsens')) ;;
  189. *'on bottom') (('dmenu_onbottom=!dmenu_onbottom')) ;;
  190. *'Background')
  191. opt=$(echo -e "$dmenu_bg\n[Default]" | $menu -p "Background:")
  192. [[ "$opt" == '[Default]' ]] && opt='000000'
  193. [ "$opt" ] && dmenu_bg="$opt"
  194. dmenu_test=1
  195. ;;
  196. *'Foreground')
  197. opt=$(echo -e "$dmenu_fg\n[Default]" | $menu -p "Foreground:")
  198. [[ "$opt" == '[Default]' ]] && opt='9999CC'
  199. [ "$opt" ] && dmenu_fg="$opt"
  200. dmenu_test=1
  201. ;;
  202. *'Selected background')
  203. opt=$(echo -e "$dmenu_bg\n[Default]" | $menu -p "Selected background:")
  204. [[ "$opt" == '[Default]' ]] && opt='000066'
  205. [ "$opt" ] && dmenu_sbg="$opt"
  206. dmenu_test=1
  207. ;;
  208. *'Selected foreground')
  209. opt=$(echo -e "$dmenu_bg\n[Default]" | $menu -p "Selected foreground:")
  210. [[ "$opt" == '[Default]' ]] && opt='FFFFFF'
  211. [ "$opt" ] && dmenu_sfg="$opt"
  212. dmenu_test=1
  213. ;;
  214. *'Executor')
  215. opt=$(echo -e "$executor\n[Default]" | $menu -p "Executor:")
  216. [[ "$opt" == '[Default]' ]] && opt='exo'
  217. [ "$opt" ] && editor="$opt"
  218. ;;
  219. *'Editor')
  220. opt=$(echo -e "$editor\n[Default]" | $menu -p "Editor:")
  221. [[ "$opt" == '[Default]' ]] && opt='geany'
  222. [ "$opt" ] && editor="$opt"
  223. ;;
  224. *'File manager')
  225. opt=$(echo -e "$filemanager\n[Default]" | $menu -p "File manager:")
  226. [[ "$opt" == '[Default]' ]] && opt='thunar'
  227. [ "$opt" ] && filemanager="$opt"
  228. ;;
  229. *'defaults')
  230. opt=$(echo -e "No\nYes" | $menu -p "Really set defaults?")
  231. [[ "$opt" == "Yes" ]] && defaults
  232. ;;
  233. 'About') xmessage -maxWidth 80 -maxHeight 80 "$about" ;;
  234. *)
  235. [ "$opt" ] && [ -e "$plugins_dir/preferences/$opt" ] && source "$plugins_dir/preferences/$opt"
  236. ;;
  237. esac
  238. done
  239. menu="$menu $([ "$dmenu_caseinsens" == '1' ] && echo '-i') $([ "$dmenu_onbottom" == '1' ] && echo '-b') -nb $dmenu_bg -nf $dmenu_fg -sb $dmenu_sbg -sf $dmenu_sfg"
  240. if [[ "$dmenu_test" == '1' ]]; then
  241. if [[ "OK" != "$(echo 'OK' | $menu -p 'Dmenu settings changed.')" ]]; then
  242. defaults
  243. source "$config"
  244. setmenu
  245. echo 'OK' | $menu -p 'Error in Dmenu settings. Reverted.'
  246. fi
  247. fi
  248. saveconfig
  249. elif [ -e "$plugins_dir/browser/$file" ]; then
  250. source "$plugins_dir/browser/$file"
  251. fi
  252. fi
  253. done
  254. saveconfig