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.

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