My scripts for startup, dmenu, and the command line
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.

484 lines
12 KiB

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