functions 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #!/bin/sh
  2. # PS1>
  3. abbreviate_aliases() {
  4. # alias must not end with / ; must be sorted like exceptions—more specific (longer) first
  5. aliases="$HOME/Music:;$HOME/Code:;$HOME/Desktop:;$HOME/Documents:;$HOME/Downloads:;$HOME/Pictures:;$HOME/Public:;$HOME/Templates:;$HOME/Videos:;$HOME:~"
  6. path="$PWD"
  7. IFS=";"; for a in $aliases
  8. do
  9. aliasPath=$(echo "$a" | cut -d':' -f1)
  10. aliasSymbol=$(echo "$a" | cut -d':' -f2)
  11. if beginswith "$aliasPath" "$path"
  12. then
  13. path=$(echo "$path" | sed "s!$aliasPath!$aliasSymbol!")
  14. break
  15. fi
  16. done
  17. if [ "$(echo "$path" | cut -c1)" = '/' ]
  18. then
  19. path="$path"
  20. fi
  21. echo "$path"
  22. }
  23. makePS1() {
  24. ex=$?
  25. bgBlack="$(tput setab 100)"
  26. bgRed="$(tput setab 101)"
  27. bgYellow="$(tput setab 103)"
  28. bgGreen="$(tput setab 102)"
  29. bgBrown="$(tput setab 107)"
  30. bgBlue="$(tput setab 106)"
  31. fgBlack="$(tput setaf 100)"
  32. fgWhite="$(tput setaf 250)"
  33. fgRed="$(tput setaf 101)"
  34. fgBlue="$(tput setaf 106)"
  35. fgYellow="$(tput setaf 103)"
  36. fgGreen="$(tput setaf 102)"
  37. fgBrown="$(tput setaf 107)"
  38. clear="$(tput sgr0)"
  39. # todo if $(jobs -l | wc -l) -gt 1 -> print indicator
  40. if [ "$ex" -gt 0 ]
  41. then
  42. printf "\001%b%b\002 %s " "$bgRed" "$fgWhite" "$ex"
  43. if [ -n "$VIRTUAL_ENV" ]
  44. then
  45. printf "\001%b%b\002\001%b\002" "$fgRed" "$bgBlue" "$clear"
  46. else
  47. printf "\001%b%b\002\001%b\002" "$fgRed" "$bgYellow" "$clear"
  48. fi
  49. fi
  50. if [ -n "$VIRTUAL_ENV" ]
  51. then
  52. venv="$(echo "$VIRTUAL_ENV" | sed 's!/.venv!!')"
  53. printf "\001%b%b\002 %s \001%b%b\002\001%b\002" "$bgBlue" "$fgBlack" "${venv##*/}" "$fgBlue" "$bgYellow" "$clear"
  54. fi
  55. path=$(abbreviate_aliases)
  56. path=${path%/}
  57. n=$(echo "$path" | sed -e 's![^/]!!g' | wc -c)
  58. i=0
  59. IFS='/'; for d in $path
  60. do
  61. # fixme grouping (left,right?)
  62. if [ "$i" -lt $((n-1)) ]
  63. then
  64. if [ "$i" -le 2 ] || [ "$i" -ge $((n-2)) ] || [ "$i" -eq 3 -a "$n" -lt 6 ]
  65. then
  66. printf "\001%b%b\002 %s \001%b\002" "$bgYellow" "$fgBlack" "$d" "$clear"
  67. elif [ "$i" -eq 3 ] && [ "$n" -ge 6 ]
  68. then
  69. printf "\001%b%b\002 … \001%b\002" "$bgYellow" "$fgBlack" "$clear"
  70. fi
  71. else
  72. printf "\001%b%b\002 %s \001%b\002" "$bgYellow" "$fgBlack" "$d" "$clear"
  73. fi
  74. i=$((i+1))
  75. done
  76. if git rev-parse 2>/dev/null
  77. then
  78. branch=$(git branch | grep '\*' | cut -d' ' -f2)
  79. bg=$bgGreen
  80. fg=$fgBlack
  81. arrow=$fgGreen
  82. gitText=" $branch "
  83. if git status | grep 'Your branch is behind' >/dev/null
  84. then
  85. gitText="$gitText"
  86. fi
  87. if git status | grep 'Untracked files' >/dev/null
  88. then
  89. gitText="$gitTextﱐ "
  90. fi
  91. if git status | grep 'Changes not staged for commit' >/dev/null
  92. then
  93. gitText="$gitText "
  94. bg=$bgBrown
  95. fg=$fgWhite
  96. arrow=$fgBrown
  97. fi
  98. if git status | grep 'Changes to be committed' >/dev/null
  99. then
  100. gitText="$gitTextﱣ "
  101. bg=$bgBrown
  102. fg=$fgWhite
  103. arrow=$fgBrown
  104. fi
  105. if git status | grep 'Your branch is ahead' >/dev/null
  106. then
  107. gitText="$gitTextﰖ "
  108. fi
  109. printf "\001%b%b\002\001%b\002" "$fgYellow" "$bg" "$clear"
  110. printf "\001%b%b\002 %s\001%b\002" "$bg" "$fg" "$gitText" "$clear"
  111. printf "\001%b%b\002\001%b\002" "$bgBlack" "$arrow" "$clear"
  112. git_present=1
  113. else
  114. git_present=0
  115. fi
  116. if hg branch >/dev/null 2>&1
  117. then
  118. branch=$(hg branch)
  119. bg=$bgGreen
  120. fg=$fgBlack
  121. arrow=$fgGreen
  122. gitText=" $branch "
  123. #if hg inc -l1 >/dev/null
  124. #then
  125. # gitText="$gitText"
  126. #fi
  127. if [ "$(hg status -u | wc -l)" -gt 0 ]
  128. then
  129. gitText="$gitTextﱐ "
  130. fi
  131. if [ "$(hg status -dram | wc -l)" -gt 0 ]
  132. then
  133. gitText="$gitText "
  134. bg=$bgBrown
  135. fg=$fgWhite
  136. arrow=$fgBrown
  137. fi
  138. #if hg out -l1 >/dev/null
  139. #then
  140. # gitText="$gitTextﰖ "
  141. #fi
  142. printf "\001%b%b\b\002\001%b\002" "$fgYellow" "$bg" "$clear"
  143. printf "\001%b%b\002 %s\001%b\002" "$bg" "$fg" "$gitText" "$clear"
  144. printf "\001\b%b%b\002\001%b\002" "$bgBlack" "$arrow" "$clear"
  145. hg_present=1
  146. else
  147. hg_present=0
  148. fi
  149. if [ $git_present -eq 0 ] && [ $hg_present -eq 0 ]
  150. then
  151. printf "\001%b%b\002\001%b\002" "$bgBlack" "$fgYellow" "$clear"
  152. fi
  153. if [ "$(whoami)" = "root" ]
  154. then
  155. printf "\001%b%b\002 # \001%b%b\002\001%b\002 " "$bgBlack" "$fgYellow" "$clear" "$fgBlack" "$clear"
  156. else
  157. printf "\001%b%b\002 \$ \001%b%b\002\001%b\002 " "$bgBlack" "$fgGreen" "$clear" "$fgBlack" "$clear"
  158. fi
  159. }
  160. # <PS1
  161. makePS2() {
  162. bgBlack="$(tput setab 0)"
  163. fgBlack="$(tput setaf 0)"
  164. fgGreen="$(tput setaf 2)"
  165. clear="$(tput sgr0)"
  166. printf "\001%b%b\002 … \001%b%b\002\001%b\002 " "$bgBlack" "$fgGreen" "$clear" "$fgBlack" "$clear"
  167. }
  168. entry() {
  169. date_full=$(date "+%Y-%m-%d")
  170. date_short=$(date "+%b %d")
  171. cd ~/Documents/Creatio/Diary/Current/ || exit 1
  172. if [ ! -f "$date_full.asciidoc" ]
  173. then
  174. printf "=== %s\n\n\n" "$date_short" > "$date_full.asciidoc"
  175. fi
  176. kak -e "set buffer spell en-GB" "$date_full.asciidoc"
  177. cd - >/dev/null || exit 1
  178. }
  179. _gen_fzf_default_opts() {
  180. color00='#28211c'
  181. color01='#36312e'
  182. #color02='#5e5d5c'
  183. #color03='#666666'
  184. color04='#797977'
  185. #color05='#8a8986'
  186. color06='#9d9b97'
  187. #color07='#baae9e'
  188. #color08='#cf6a4c'
  189. #color09='#cf7d34'
  190. color0A='#f9ee98'
  191. #color0B='#54be0d'
  192. color0C='#afc4db'
  193. color0D='#5ea6ea'
  194. #color0E='#9b859d'
  195. #color0F='#937121'
  196. export FZF_DEFAULT_OPTS="
  197. --color=bg+:$color01,bg:$color00,spinner:$color0C,hl:$color0D
  198. --color=fg:$color04,header:$color0D,info:$color0A,pointer:$color0C
  199. --color=marker:$color0C,fg+:$color06,prompt:$color0A,hl+:$color0D
  200. "
  201. }
  202. beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
  203. endswith() { case $2 in *"$1") true;; *) false;; esac; }
  204. screenshot() {
  205. slurp | grim -g - - | cwebp -lossless -o "$HOME/Pictures/screen_$(date +%Y-%m-%dT%H:%M:%S).webp" -- -
  206. }
  207. screenshot_all() {
  208. grim - | cwebp -lossless -o "$HOME/Pictures/screen_$(date +%Y-%m-%dT%H:%M:%S).webp" -- -
  209. }
  210. # golint() {
  211. # if [ $# -gt 0 ]
  212. # then
  213. # result=$(cat "$1")
  214. # else
  215. # result=$(golangci-lint run --out-format checkstyle)
  216. # fi
  217. # files=$(echo "$result" | xmllint --xpath '//file' - | tr '\n' '\t')
  218. # IFS=$'\t'; for file in $(echo "$files")
  219. # do
  220. # filename=$(echo "$file" | xmllint --xpath 'string(//file/@name)' -)
  221. # errors=$(echo "$file" | xmllint --xpath '//error' - | tr '\n' '\t')
  222. # IFS=$'\t'; for error in $(echo "$errors")
  223. # do
  224. # line=$(echo "$error" | xmllint --xpath 'string(//error/@line)' -)
  225. # column=$(echo "$error" | xmllint --xpath 'string(//error/@column)' -)
  226. # message=$(echo "$error" | xmllint --xpath 'string(//error/@message)' -)
  227. # kind=$(echo "$error" | xmllint --xpath 'string(//error/@severity)' -)
  228. # echo "$filename:$line:$column: $kind: $message"
  229. # done
  230. # done
  231. # }
  232. #
  233. #fd() {
  234. # $EDITOR $(ag --nobreak --nonumbers --noheading . | fzf --delimiter=':' --nth=2.. | cut -d ':' -f 1)
  235. #}
  236. datediff() {
  237. d1=$(date -d "$1" +%s)
  238. d2=$(date -d "$2" +%s)
  239. if [ "$d1" -gt "$d2" ]
  240. then
  241. d=$(qalc -t -b '10 10' "$d1 - $d2")
  242. else
  243. d=$(qalc -t -b '10 10' "$d2 - $d1")
  244. fi
  245. d=$(qalc -t -b '10 10' "round($d / 86400)")
  246. if [ "$d" -eq 1 ]
  247. then
  248. printf "%s day\n" "$d"
  249. else
  250. printf "%s days\n" "$d"
  251. fi
  252. }
  253. shuffle() {
  254. n=$1
  255. shuf -i "1-$n" -n "$n"
  256. }
  257. alarm() {
  258. echo "notify-send -u critical -i appointment-soon 'Alarm' '$2'; ffplay ~adam/Music/mps/cloister.ogg" | at "$1"
  259. }
  260. ide() {
  261. # todo get name from build script
  262. kak -clear
  263. server_name=$(basename "$PWD")
  264. socket_file=$(kak -l | grep "$server_name")
  265. if [ "$socket_file" = '' ]
  266. then
  267. kak -d -s "$server_name"
  268. fi
  269. kak -c "$server_name" "$@"
  270. }
  271. guak() {
  272. if [ $(ps aux | grep -c 'alacritty --class guak') -lt 2 ]
  273. then
  274. /home/adam/.local/bin/alacritty --class guake &
  275. usleep 500000
  276. fi
  277. swaymsg '[app_id="guake"] scratchpad show'
  278. }
  279. hex2rgb() {
  280. hexinput=$(echo "$1"| tr '[:lower:]' '[:upper:]')
  281. if [ "$(echo "$hexinput" | cut -c1)" = '#' ]
  282. then
  283. hexinput=$(echo "$hexinput" |cut -c2-)
  284. fi
  285. a=$(echo "$hexinput" | cut -c-2)
  286. b=$(echo "$hexinput" | cut -c3-4)
  287. c=$(echo "$hexinput" | cut -c5-6)
  288. r=$(qalc -t -s 'base 10' "0x$a")
  289. g=$(qalc -t -s 'base 10' "0x$b")
  290. b=$(qalc -t -s 'base 10' "0x$c")
  291. echo "$r" "$g" "$b"
  292. }
  293. import_gsettings() {
  294. for pair in "$@"; do
  295. gKey=$(echo "$pair" | cut -d':' -f1)
  296. name=$(echo "$pair" | cut -d':' -f2)
  297. value=$(cat "${XDG_CONFIG_HOME:-$HOME/.config}"/gtk-3.0/settings.ini | grep "$name" | cut -d'=' -f2)
  298. gsettings set org.gnome.desktop.interface "$gKey" "$value"
  299. done
  300. }
  301. pad() {
  302. num=$1
  303. while [ "${#num}" -lt "$2" ]
  304. do
  305. num="0$num"
  306. done
  307. echo "$num"
  308. }
  309. now() {
  310. now=$(qalc -t -b '12' "int(($(date +%H) * 3600000000000 + $(date +%M) * 60000000000 + $(date +%S) * 1000000000 + $(date +%N)) / 1000000000 * 144 / 25)")
  311. now=$(pad "$now" 6)
  312. hora=$(echo "$now" | colrm 3)
  313. dunatim=$(echo "$now" | colrm 5 | rev | colrm 3 | rev)
  314. tim=$(echo "$now" | rev | colrm 3 | rev)
  315. if [ "$1" = "-t" ]
  316. then
  317. printf "{\"text\": \"%s.%s\"}\n" "$hora" "$dunatim"
  318. else
  319. printf "{\"text\": \"%s.%s.%s\"}\n" "$hora" "$dunatim" "$tim"
  320. fi
  321. }
  322. screenOnOff() {
  323. v=$(light -s sysfs/backlight/intel_backlight -G)
  324. if [ "$v" = "100.00" ]
  325. then
  326. light -s sysfs/backlight/intel_backlight -S 0
  327. else
  328. light -s sysfs/backlight/intel_backlight -S 100
  329. fi
  330. }
  331. streamDwd() {
  332. if [ $# -lt 1 ]
  333. then
  334. min=1800
  335. else
  336. min=$(echo "$1 * 60" | bc)
  337. fi
  338. echo "Downloading for $min seconds"
  339. streamripper http://195.150.20.243:8000/rmf_classic -s -A -a %D -d /home/adam/Downloads/streams/RMFClassic/ -l "$min"
  340. doas /bin/poweroff
  341. }
  342. ffmpegQ() {
  343. height=$(ffprobe "$*" -show_entries stream=height -of csv=p=0 -v error -select_streams v:0)
  344. if [ "$height" -lt 480 ]
  345. then
  346. echo 37
  347. elif [ "$height" -lt 720 ]
  348. then
  349. echo 35
  350. elif [ "$height" -lt 1080 ]
  351. then
  352. echo 34
  353. else
  354. echo 33
  355. fi
  356. }
  357. toVP9() {
  358. Q=$(ffmpegQ "$1")
  359. set -x
  360. ffmpeg -i "$1" -crf "$Q" -threads 16 -b:v 0 -c:v libvpx-vp9 -r 25 "${1%.*}.vp9.webm"
  361. }
  362. toAV1() {
  363. Q=$(ffmpegQ "$1")
  364. set -x
  365. ffmpeg -i "$1" -crf "$Q" -threads 16 -b:v 0 -c:v libaom-av1 -r 25 -strict experimental -tile-columns 2 -pix_fmt yuv420p "${1%%.*}.av1.webm"
  366. }
  367. touchpad() {
  368. ID=$(xinput list | grep -Eo 'TouchPad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}')
  369. State=$(xinput list-props "$ID" | grep 'Device Enabled' | awk '{print $4}')
  370. if [ "$State" -eq 1 ]
  371. then
  372. xinput disable "$ID"
  373. else
  374. xinput enable "$ID"
  375. fi
  376. }