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

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