My scripts for startup, dmenu, and the command line
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

597 строки
13 KiB

  1. #!/bin/sh
  2. browser_cmd="brave-browser"
  3. browser_new_cmd="brave-browser --new-window"
  4. # First argument is the directory picking message
  5. ask_dir() {
  6. d=$(mru listdirs | dmenu -i -c -l 20 -p "$1")
  7. if [ -z "$d" ]; then exit; fi
  8. printf "$d"
  9. }
  10. # First argument is the file picking message
  11. ask_file() {
  12. d=$(ask_dir "from")
  13. if [ -z "$d" ]; then exit; fi
  14. f=$(
  15. find "$d" -maxdepth 4 \( -path '*/.*/*' -o -name 'node_modules'\
  16. -o -name '*.uuid' -o -name 'vendor' -o -regex '.*sw[op]$' \) -prune -o -print |
  17. dmenu -i -c -l 20 -p "$1"
  18. )
  19. printf "$f"
  20. }
  21. launch() {
  22. app=$(printf "Notes
  23. Files
  24. Edit
  25. Editor
  26. Open
  27. Pick
  28. Choose
  29. Tasks
  30. Terminal
  31. Multiplexer
  32. Chat
  33. Music
  34. Bookmarks
  35. Equalizer
  36. Browser
  37. Hidden browser
  38. Email
  39. Page
  40. Go page
  41. Package info
  42. Images
  43. Games
  44. Movie
  45. Play clipboard
  46. Play downloads
  47. Processes
  48. Mixer" | tr -d '\t' |
  49. dmenu -i -c -l 20 -p "Launcher")
  50. case $app in
  51. Notes) st -t "Notes" -e vim "+cd ~/Notes" "+CtrlP";;
  52. Files) st -t "Files" -e sh -lc nnn;;
  53. Edit) editor;;
  54. Editor) st -t "Editor" -e vim "+CtrlPMRUFiles";;
  55. Open) opener;;
  56. Pick) pick;;
  57. Choose) choose;;
  58. Bookmarks) st -t "Bookmarks" -e sh -lc buku;;
  59. Terminal) st -t "Terminal";;
  60. Tasks) st -t "Tasks" -e sh -lc taskwarrior-tui;;
  61. Page) pages pager "$(printf "1\n2\n3\n" | dmenu -p 'page')";;
  62. Games) games;;
  63. Movie) mpv $(find /mnt/media/movies -regex '.*\.m\(kv\)\|\(p.\)$'\
  64. -type f -not -path '*.x/*' | dmenu -i -c -l 20 -p 'Play');;
  65. Multiplexer) st -t "Multiplexer" -e tmux attach-session -t 0 ||
  66. st -t "Multiplexer" -e tmux new-session -s 0;;
  67. 'Go page') num=$(go_page); pages pager $num;;
  68. Chat) element-desktop;;
  69. Music) st -t "Music" -e ncmpcpp;;
  70. Browser) $browser_cmd;;
  71. 'Hidden browser') $browser_cmd --incognito;;
  72. Email) st -t 'Email' -e sh -lc neomutt;;
  73. Images) view_images;;
  74. 'Play clipboard') mpv "$(xclip -o -selection clipboard)";;
  75. 'Play downloads') mpv "$HOME/Downloads/tmp/$(ls ~/Downloads/tmp/ |
  76. dmenu -i -l 10 -p 'play')";;
  77. 'Mixer') st -t "Mixer" -e pulsemixer;;
  78. 'Equalizer') qpaeq;;
  79. Processes) st -t "Processes" -e top;;
  80. esac
  81. }
  82. action() {
  83. action=$(printf "Toggle Music
  84. Pause Music
  85. Play Music
  86. Single on
  87. Bookmarks
  88. Command
  89. Command to clipboard
  90. Select VPN
  91. Disable VPN
  92. Enable VPN
  93. VPN Status
  94. Rebind Keys
  95. Show calender
  96. Disable Bar
  97. Password
  98. Username
  99. Alternate password
  100. Select song
  101. Download
  102. Download music
  103. Torrent
  104. Configure
  105. Move
  106. Update cache
  107. Enable Bar
  108. Exit" | tr -d '\t' |
  109. dmenu -i -c -l 20 -p "Actions")
  110. case $action in
  111. 'Play Music') mpc play ;;
  112. 'Pause Music') mpc pause ;;
  113. 'Toggle Music') mpc toggle;;
  114. 'Single on') mpc toggle;;
  115. 'Rebind Keys') setup-xbindkeys;;
  116. 'Disable Bar') tmux set -g status off;;
  117. 'Enable Bar') tmux set -g status on;;
  118. 'Bookmarks') bookmarks;;
  119. 'Command') cmd;;
  120. 'Command to clipboard') cmd_clip;;
  121. 'Username') username;;
  122. 'Password') password;;
  123. 'Alternate password') other_password;;
  124. 'Show calender') notify-send 'Calender' "\n\n$(cal)";;
  125. 'Update cache') { mru update; mru updatedirs; };;
  126. 'Select song') select_song;;
  127. 'Exit') do_exit;;
  128. #This should check for an error code and confirm that download has started
  129. 'Download') download;;
  130. 'Download music') download_music;;
  131. 'Torrent') torrent;;
  132. 'Configure') configure;;
  133. 'Move') move;;
  134. esac
  135. }
  136. login() {
  137. choice=$(printf "Auto
  138. Username
  139. Password
  140. Alternate
  141. Show
  142. Edit" | tr -d '\t' | dmenu -i -c -p "login")
  143. case $choice in
  144. Username) username;;
  145. Password) password;;
  146. Alternate) other_password;;
  147. Show) show_password;;
  148. esac
  149. }
  150. do_search() {
  151. type=$(printf "Manual
  152. DDG
  153. Definition
  154. Godocs
  155. Mojeek
  156. Package
  157. Github" | tr -d '\t' | dmenu -i -c -p "Search")
  158. case "$type" in
  159. Manual) p=$(man -k '' | dmenu -i -c -l 20 -p 'Manual' | cut -d' ' -f1);
  160. if [ -z $p ]; then exit; fi; st -t "Manual - $p" -e man "$p";;
  161. Github) github;;
  162. Godocs) godoc;;
  163. DDG) ddg;;
  164. Package) package;;
  165. Definition) word=$(printf '' | dmenu -p 'word');
  166. if [ -z "$word" ]; then exit; fi;
  167. st -e sh -lc "dict \"$word\" | less";;
  168. esac
  169. }
  170. download() {
  171. youtube-dl --add-metadata --no-progress -o \
  172. "$HOME/Downloads/%(title)s.%(ext)s" "$(xclip -selection clipboard -o)"
  173. notify-send -u low -t 3000 "Download complete"
  174. }
  175. download_music() {
  176. youtube-dl -x --add-metadata --no-progress --audio-format mp3 -o \
  177. "/mnt/media/music/new/%(track)s - %(artist)s.%(ext)s" "$(xclip -selection clipboard -o)"
  178. if [ ! $? ]; then
  179. youtube-dl -x --add-metadata --no-progress --audio-format mp3 -o \
  180. "$HOME/music/new/%(title)s - %(artist)s.%(ext)s" "$(xclip -selection clipboard -o)"
  181. fi
  182. if [ -e "$HOME/music/new/NA - NA"* ]; then
  183. rm "$HOME/music/new/NA - NA"*
  184. youtube-dl -x --add-metadata --no-progress --audio-format mp3 -o \
  185. "$HOME/music/new/%(title)s - %(artist)s.%(ext)s" \
  186. "$(xclip -selection clipboard -o)"
  187. fi
  188. notify-send -u low -t 3000 "Download complete"
  189. }
  190. view_images() {
  191. d=$(mru listdirs | dmenu -c -l 20 -i -p 'where?')
  192. if [ -z "$d" ]; then exit; fi
  193. sxiv -r "$d"
  194. }
  195. package() {
  196. type=$(printf "search\ninfo" | dmenu -c -p 'package')
  197. if [ -z "$type" ]; then
  198. exit
  199. elif [ "$type" = "info" ]; then
  200. s=$(dmenu_path | dmenu -i -c -p 'package info')
  201. st -e sh -lc "apt-cache show $s"
  202. else
  203. s=$(dmenu -i -c -p 'package search')
  204. st -e sh -lc "apt search \"$s\""
  205. fi
  206. }
  207. editor() {
  208. #handle spacing in filenames bug
  209. file=$(mru list | dmenu -i -c -l 20 -p 'edit file')
  210. if [ -z "$file" ]; then exit; fi
  211. first=$(printf "$file" | head -n1 -)
  212. d=$(dirname "$first")
  213. files=$(printf "$file" | tr '\n' ' ')
  214. printf "files: $files"
  215. st -t 'Editor' -e vim "+cd $d" $files
  216. }
  217. opener() {
  218. mru list | dmenu -i -c -l 20 -p 'open' |
  219. while read f; do xdg-open "$f"; done
  220. }
  221. screenshot() {
  222. method=$(
  223. printf "clipboard
  224. file
  225. both" |
  226. dmenu -i -p "Screenshot")
  227. if [ $method = "file" ]; then
  228. scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c
  229. 255,0,0,255 -t 'Hi'"\
  230. -s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png
  231. fi
  232. if [ $method = "clipboard" ]; then
  233. scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c
  234. 255,0,0,255 -t 'Hi'"\
  235. -s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png\
  236. -e "xclip $f; rm $f"
  237. fi
  238. if [ $method = "both" ]; then
  239. scrot --note "-f 'LiterationSans Nerd Font Book/11' -x 10 -y 20 -c
  240. 255,0,0,255 -t 'Hi'"\
  241. -s ~/Pictures/screenshots/screenshot-%Y-%m-%d_$wx$h.png\
  242. -e "xclip $f;"
  243. fi
  244. }
  245. search_type() {
  246. search_string="$(printf "New window\nBookmarks\nHistory" | dmenu -c -p "$1")"
  247. search_mode='tab'
  248. case "$search_string" in
  249. 'New window') search_mode='window';
  250. search_string=$(printf '' | dmenu -c -p "new $1");;
  251. 'Bookmarks');;
  252. 'History') ;;
  253. # *) printf "Invalid argument";;
  254. esac
  255. printf "$search_string|$search_mode"
  256. }
  257. brave_search() {
  258. search_prefix="$1"
  259. search_info="$(search_type "$2")"
  260. search_string=$(printf "$search_info" | cut -d'|' -f1)
  261. search_mode=$(printf "$search_info" | cut -d'|' -f2)
  262. if [ -z "$search_string" ]; then exit; fi
  263. new_string=$(rawurlencode "$search_string")
  264. search="$search_prefix$new_string"
  265. case "$search_mode" in
  266. tab) $browser_cmd "$search";;
  267. window) $browser_new_cmd "$search";;
  268. esac
  269. }
  270. rawurlencode() {
  271. #Takes the first argument and encodes it
  272. search_string="$1"
  273. string_length=$(expr length "$search_string")
  274. char=''; new_string='';
  275. i=1
  276. while [ $i -le $string_length ]
  277. do
  278. char=$(expr substr "$search_string" $i 1)
  279. # new_string="$new_string$(rawurlencode "$char")"
  280. case "$char" in
  281. [-_.~a-zA-Z0-9] ) new_string="$new_string$char";;
  282. * ) new_string="$new_string%$(printf '%%%02x' "'$char")";;
  283. esac
  284. i=$(expr $i + 1)
  285. done
  286. printf "$new_string"
  287. }
  288. ddg() {
  289. brave_search "https://duckduckgo.com/?q=" "ddg"
  290. }
  291. github() {
  292. brave_search "https://github.com/search?q=" "github"
  293. }
  294. godoc() {
  295. brave_search "https://golang.org/pkg/" "godoc"
  296. }
  297. manual() {
  298. search=$(printf '' | dmenu -p 'manual')
  299. result=$(man "$search")
  300. printf "$result" | enscript -p - | ps2pdf - | zathura -
  301. }
  302. goinfo() {
  303. #Maybe this can also show recent searches
  304. search=$(printf "Window\nNotify" | dmenu -p 'goinfo')
  305. case $search in
  306. Window) search=$(printf '' | dmenu -p 'goinfo');
  307. result=$(go doc "$search"); infowindow "$result"; exit;;
  308. Notify) search=$(printf '' | dmenu -p 'goinfo');
  309. result=$(go doc "$search");
  310. notify-send -u low -t 0 "Go documentation" "$result"; exit;;
  311. esac
  312. result=$(go doc "$search");
  313. notify-send -u low -t 0 "Go documentation" "$result";
  314. }
  315. go_page() {
  316. name=$(printf "1\n2\n3\n4" | dmenu -p "page name")
  317. go doc "$(printf "" | dmenu -p 'go doc search')" > /tmp/pages-$name
  318. printf "$name"
  319. }
  320. infowindow() {
  321. printf "$1" | enscript -p - | ps2pdf - | zathura -
  322. }
  323. cmd() {
  324. c="$(printf '' | dmenu -i -p 'cmd')"
  325. output=$( $c )
  326. notify-send -u low 'command output' "$output"
  327. }
  328. cmd_clip() {
  329. value=$(cmd)
  330. show_value="$(expr substr "$value" 1 200)\n..."
  331. notify-send -u low -t 2000 "Items cliped" "$show_value"
  332. printf "%s" "$value" | xclip -selection clipboard
  333. }
  334. sel_account() {
  335. account=$(
  336. find ~/.password-store/[!\.]* -type f |
  337. sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
  338. dmenu -c -i -l 20 -p 'account'
  339. )
  340. if [ -z "$account" ]; then exit; fi
  341. printf "$account"
  342. }
  343. username() {
  344. account=$(sel_account)
  345. if [ -z "$account" ]; then exit; fi
  346. name=$(pass show "$account" | sed -n -e "s/^username: //p" |
  347. dmenu -c -i -l 20 -p 'which username' | xclip -f -selection clipboard)
  348. if [ -z "$name" ]; then exit; fi
  349. notify-send -u low -t 2000 "username copied" "$account: $name"
  350. }
  351. password() {
  352. account=$(find ~/.password-store/[!\.]* -type f |
  353. sed -e "s:$HOME/.password-store/::" -e "s:\.gpg::" |
  354. dmenu -i -c -l 20 -p 'which service')
  355. pass -c $account
  356. notify-send -u low -t 1000 'password copied' "Copied $account"
  357. }
  358. # Giving multiple accounts for the same service the same username may cause problems.
  359. # It assumes that the password is always the first line of the account's section,
  360. # and the username is always the second
  361. other_password() {
  362. account=$(sel_account)
  363. name=$(pass show "$account" | sed -n -e "s/^username: //p" |
  364. dmenu -i -c -l 20 -p 'which user password')
  365. if [ -z "$name" ]; then exit; fi
  366. copy=$(pass show $account | grep -B 1 "^username: $name" | head -n1 | tr -d '\n')
  367. printf "$copy" | xclip -selection clipboard
  368. notify-send -u low -t 2000 "password copied" "$account: $name"
  369. sleep 30s
  370. new_copy=$(xclip -o -selection clipboard)
  371. if [ "$copy" = $newcopy ]; then
  372. printf '' | xclip -selection clipboard
  373. fi
  374. }
  375. show_password() {
  376. account=$(sel_account)
  377. info=$(pass show "$account")
  378. st -e sh -lc "printf '$info' | less"
  379. }
  380. edit_password() {
  381. account=$(sel_account)
  382. if [ -z "$account" ]; then exit; fi
  383. error=$(st -e pass edit "$account" 2>&1)
  384. if [ -n "$error" ];
  385. then notify-send -u low -t 3000 "password edit error" "$error"; fi
  386. }
  387. # Execute a command with the specified file or directory as it's argument
  388. choose() {
  389. d=$(mru listdirs | dmenu -i -c -l 20 -p 'from')
  390. if [ -z "$d" ]; then exit; fi
  391. f=$(
  392. (printf "$d\n"; find "$d" -type f -not \( -path '*/.*/*' -o -path
  393. '*node_modules/*' -o -ipath '*backups/my-plugins*' -o -ipath '*/.uuid'
  394. -ipath '*.swp*' \)) | dmenu -i -c -l 20 -p 'choose file'
  395. )
  396. if [ -z "$f" ]; then exit; fi
  397. c=$(dmenu_path | dmenu -i -c -p 'operation')
  398. if [ -z $c ]; then exit; fi
  399. $c "$f"
  400. }
  401. # It finds all the current files in a known directory without needing to update
  402. # the mru list of files If the directory is chosen instead of the file within
  403. # it, it opens the directory with nnn
  404. pick() {
  405. d=$(printf "$(find $HOME -maxdepth 3 -not -path '*/.*' -not -name \
  406. 'node_modules' -not -name 'vendor'; find /mnt/media -maxdepth 3 -type d \
  407. -not -path '*/.*')" | dmenu -i -c -l 20 -p 'from')
  408. if [ -z "$d" ]; then exit; fi
  409. f=$(
  410. (printf "$d\n"; find "$d" -type f -not \( -path '*/.*/*' -o -path \
  411. '*node_modules/*' -o -ipath '*/.uuid' -ipath '*.swp*' \)) | \
  412. dmenu -i -c -l 20 -p 'pick file'
  413. )
  414. if [ -z "$f" ]; then exit; fi
  415. if [ "$d" = "$f" ]; then st -t 'Files' -e sh -lc "nnn '$f'"; exit; fi
  416. xdg-open "$f"
  417. }
  418. do_exit() {
  419. choice=$(printf "Shutdown
  420. Reboot"| dmenu -i -c -p 'Exit')
  421. case $choice in
  422. Shutdown) ls;;
  423. Reboot) ls;;
  424. esac
  425. }
  426. select_song() {
  427. song=$(mpc playlist | dmenu -i -c -l 20 -p 'song')
  428. num=$(mpc playlist | sed -n "/$song/=")
  429. mpc play $num
  430. }
  431. games() {
  432. sel=$(printf "Albion
  433. Hellblade
  434. Divinity
  435. Rise to Ruins
  436. Witcher 3
  437. POE" | dmenu -i -c -p 'Games')
  438. case "$sel" in
  439. Albion);;
  440. Divinity);;
  441. 'Rise to Ruins');;
  442. 'Witcher 3');;
  443. 'POE');;
  444. esac
  445. }
  446. bookmarks() {
  447. sel=$(printf "Open
  448. Select by tag
  449. Modify
  450. Delete
  451. Edit" | tr -d "\t" | dmenu -i -c -p 'Bookmarks')
  452. case "$sel" in
  453. 'Open') m=$(buku --nc -p | dmenu -i -c -l 30 -p "open bookmark");;
  454. 'Select by tag');;
  455. 'Modify');;
  456. 'Delete');;
  457. esac
  458. }
  459. torrent() {
  460. sel=$(printf "List
  461. Add
  462. Delete
  463. Edit" | tr -d "\t" | dmenu -i -c -p 'Bookmarks')
  464. case "$sel" in
  465. 'Add') btcli add -T -d /mnt/media/torrents/ \
  466. "$(
  467. find ~/Downloads -name '*.torrent' |
  468. dmenu -i -c -l 30 -p "Select torrent"
  469. )";;
  470. 'List');; # Already lists as final output
  471. esac
  472. notify-send -u low -t 6000 "Torrents" "$(btcli list)"
  473. }
  474. move() {
  475. f=$(ask_file "Choose file")
  476. dest=$(ask_dir "Destination")
  477. notify-send -u low -t 6000 "Move" "$(mv $f $dest)"
  478. }
  479. # Should be used for automatically selecting and copying files in
  480. # ~/Source/configs to their correct path
  481. # configure() {
  482. # }
  483. case $1 in
  484. launch ) launch;;
  485. action) action;;
  486. pick) pick;;
  487. cmd) cmd;;
  488. cmd_clip) cmd_clip;;
  489. login) login;;
  490. search) do_search;;
  491. open) opener;;
  492. choose) choose;;
  493. vpn) vpn;;
  494. download) download;;
  495. download_music) download_music;;
  496. *) printf "Invalid argument";;
  497. esac