My scripts for startup, dmenu, and the command line
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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