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.

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