123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- #!/bin/sh
- # PS1>
- abbreviate_aliases() {
- # alias must not end with / ; must be sorted like exceptions—more specific (longer) first
- aliases="$HOME/Music:;$HOME/Code:;$HOME/Desktop:;$HOME/Documents:;$HOME/Downloads:;$HOME/Pictures:;$HOME/Public:;$HOME/Templates:;$HOME/Videos:;$HOME:~"
- path="$PWD"
- IFS=";"; for a in $aliases
- do
- aliasPath=$(echo "$a" | cut -d':' -f1)
- aliasSymbol=$(echo "$a" | cut -d':' -f2)
- if beginswith "$aliasPath" "$path"
- then
- path=$(echo "$path" | sed "s!$aliasPath!$aliasSymbol!")
- break
- fi
- done
- if [ "$(echo "$path" | cut -c1)" = '/' ]
- then
- path="$path"
- fi
- echo "$path"
- }
- makePS1() {
- ex=$?
- bgBlack="$(tput setab 100)"
- bgRed="$(tput setab 101)"
- bgYellow="$(tput setab 103)"
- bgGreen="$(tput setab 102)"
- bgBrown="$(tput setab 107)"
- bgBlue="$(tput setab 106)"
- fgBlack="$(tput setaf 100)"
- fgWhite="$(tput setaf 250)"
- fgRed="$(tput setaf 101)"
- fgBlue="$(tput setaf 106)"
- fgYellow="$(tput setaf 103)"
- fgGreen="$(tput setaf 102)"
- fgBrown="$(tput setaf 107)"
- clear="$(tput sgr0)"
- # todo if $(jobs -l | wc -l) -gt 1 -> print indicator
- if [ "$ex" -gt 0 ]
- then
- printf "\001%b%b\002 %s " "$bgRed" "$fgWhite" "$ex"
- if [ -n "$VIRTUAL_ENV" ]
- then
- printf "\001%b%b\002\001%b\002" "$fgRed" "$bgBlue" "$clear"
- else
- printf "\001%b%b\002\001%b\002" "$fgRed" "$bgYellow" "$clear"
- fi
- fi
- if [ -n "$VIRTUAL_ENV" ]
- then
- venv="$(echo "$VIRTUAL_ENV" | sed 's!/.venv!!')"
- printf "\001%b%b\002 %s \001%b%b\002\001%b\002" "$bgBlue" "$fgBlack" "${venv##*/}" "$fgBlue" "$bgYellow" "$clear"
- fi
- path=$(abbreviate_aliases)
- path=${path%/}
- n=$(echo "$path" | sed -e 's![^/]!!g' | wc -c)
- i=0
- IFS='/'; for d in $path
- do
- # fixme grouping (left,right?)
- if [ "$i" -lt $((n-1)) ]
- then
- if [ "$i" -le 2 ] || [ "$i" -ge $((n-2)) ] || [ "$i" -eq 3 -a "$n" -lt 6 ]
- then
- printf "\001%b%b\002 %s \001%b\002" "$bgYellow" "$fgBlack" "$d" "$clear"
- elif [ "$i" -eq 3 ] && [ "$n" -ge 6 ]
- then
- printf "\001%b%b\002 … \001%b\002" "$bgYellow" "$fgBlack" "$clear"
- fi
- else
- printf "\001%b%b\002 %s \001%b\002" "$bgYellow" "$fgBlack" "$d" "$clear"
- fi
- i=$((i+1))
- done
- if git rev-parse 2>/dev/null
- then
- branch=$(git branch | grep '\*' | cut -d' ' -f2)
- bg=$bgGreen
- fg=$fgBlack
- arrow=$fgGreen
- gitText=" $branch "
- if git status | grep 'Your branch is behind' >/dev/null
- then
- gitText="$gitText"
- fi
- if git status | grep 'Untracked files' >/dev/null
- then
- gitText="$gitTextﱐ "
- fi
- if git status | grep 'Changes not staged for commit' >/dev/null
- then
- gitText="$gitText "
- bg=$bgBrown
- fg=$fgWhite
- arrow=$fgBrown
- fi
- if git status | grep 'Changes to be committed' >/dev/null
- then
- gitText="$gitTextﱣ "
- bg=$bgBrown
- fg=$fgWhite
- arrow=$fgBrown
- fi
- if git status | grep 'Your branch is ahead' >/dev/null
- then
- gitText="$gitTextﰖ "
- fi
- printf "\001%b%b\002\001%b\002" "$fgYellow" "$bg" "$clear"
- printf "\001%b%b\002 %s\001%b\002" "$bg" "$fg" "$gitText" "$clear"
- printf "\001%b%b\002\001%b\002" "$bgBlack" "$arrow" "$clear"
- git_present=1
- else
- git_present=0
- fi
- if hg branch >/dev/null 2>&1
- then
- branch=$(hg branch)
- bg=$bgGreen
- fg=$fgBlack
- arrow=$fgGreen
- gitText=" $branch "
- #if hg inc -l1 >/dev/null
- #then
- # gitText="$gitText"
- #fi
- if [ "$(hg status -u | wc -l)" -gt 0 ]
- then
- gitText="$gitTextﱐ "
- fi
- if [ "$(hg status -dram | wc -l)" -gt 0 ]
- then
- gitText="$gitText "
- bg=$bgBrown
- fg=$fgWhite
- arrow=$fgBrown
- fi
- #if hg out -l1 >/dev/null
- #then
- # gitText="$gitTextﰖ "
- #fi
- printf "\001%b%b\b\002\001%b\002" "$fgYellow" "$bg" "$clear"
- printf "\001%b%b\002 %s\001%b\002" "$bg" "$fg" "$gitText" "$clear"
- printf "\001\b%b%b\002\001%b\002" "$bgBlack" "$arrow" "$clear"
- hg_present=1
- else
- hg_present=0
- fi
- if [ $git_present -eq 0 ] && [ $hg_present -eq 0 ]
- then
- printf "\001%b%b\002\001%b\002" "$bgBlack" "$fgYellow" "$clear"
- fi
- if [ "$(whoami)" = "root" ]
- then
- printf "\001%b%b\002 # \001%b%b\002\001%b\002 " "$bgBlack" "$fgYellow" "$clear" "$fgBlack" "$clear"
- else
- printf "\001%b%b\002 \$ \001%b%b\002\001%b\002 " "$bgBlack" "$fgGreen" "$clear" "$fgBlack" "$clear"
- fi
- }
- # <PS1
- makePS2() {
- bgBlack="$(tput setab 0)"
- fgBlack="$(tput setaf 0)"
- fgGreen="$(tput setaf 2)"
- clear="$(tput sgr0)"
- printf "\001%b%b\002 … \001%b%b\002\001%b\002 " "$bgBlack" "$fgGreen" "$clear" "$fgBlack" "$clear"
- }
- entry() {
- date_full=$(date "+%Y-%m-%d")
- date_short=$(date "+%b %d")
- cd ~/Documents/Creatio/Diary/Current/ || exit 1
- if [ ! -f "$date_full.asciidoc" ]
- then
- printf "=== %s\n\n\n" "$date_short" > "$date_full.asciidoc"
- fi
- kak -e "set buffer spell en-GB" "$date_full.asciidoc"
- cd - >/dev/null || exit 1
- }
- _gen_fzf_default_opts() {
- color00='#28211c'
- color01='#36312e'
- #color02='#5e5d5c'
- #color03='#666666'
- color04='#797977'
- #color05='#8a8986'
- color06='#9d9b97'
- #color07='#baae9e'
- #color08='#cf6a4c'
- #color09='#cf7d34'
- color0A='#f9ee98'
- #color0B='#54be0d'
- color0C='#afc4db'
- color0D='#5ea6ea'
- #color0E='#9b859d'
- #color0F='#937121'
- export FZF_DEFAULT_OPTS="
- --color=bg+:$color01,bg:$color00,spinner:$color0C,hl:$color0D
- --color=fg:$color04,header:$color0D,info:$color0A,pointer:$color0C
- --color=marker:$color0C,fg+:$color06,prompt:$color0A,hl+:$color0D
- "
- }
- beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
- endswith() { case $2 in *"$1") true;; *) false;; esac; }
- screenshot() {
- slurp | grim -g - - | cwebp -lossless -o "$HOME/Pictures/screen_$(date +%Y-%m-%dT%H:%M:%S).webp" -- -
- }
- screenshot_all() {
- grim - | cwebp -lossless -o "$HOME/Pictures/screen_$(date +%Y-%m-%dT%H:%M:%S).webp" -- -
- }
- # golint() {
- # if [ $# -gt 0 ]
- # then
- # result=$(cat "$1")
- # else
- # result=$(golangci-lint run --out-format checkstyle)
- # fi
- # files=$(echo "$result" | xmllint --xpath '//file' - | tr '\n' '\t')
- # IFS=$'\t'; for file in $(echo "$files")
- # do
- # filename=$(echo "$file" | xmllint --xpath 'string(//file/@name)' -)
- # errors=$(echo "$file" | xmllint --xpath '//error' - | tr '\n' '\t')
- # IFS=$'\t'; for error in $(echo "$errors")
- # do
- # line=$(echo "$error" | xmllint --xpath 'string(//error/@line)' -)
- # column=$(echo "$error" | xmllint --xpath 'string(//error/@column)' -)
- # message=$(echo "$error" | xmllint --xpath 'string(//error/@message)' -)
- # kind=$(echo "$error" | xmllint --xpath 'string(//error/@severity)' -)
- # echo "$filename:$line:$column: $kind: $message"
- # done
- # done
- # }
- #
- #fd() {
- # $EDITOR $(ag --nobreak --nonumbers --noheading . | fzf --delimiter=':' --nth=2.. | cut -d ':' -f 1)
- #}
- datediff() {
- d1=$(date -d "$1" +%s)
- d2=$(date -d "$2" +%s)
- if [ "$d1" -gt "$d2" ]
- then
- d=$(qalc -t -b '10 10' "$d1 - $d2")
- else
- d=$(qalc -t -b '10 10' "$d2 - $d1")
- fi
- d=$(qalc -t -b '10 10' "round($d / 86400)")
- if [ "$d" -eq 1 ]
- then
- printf "%s day\n" "$d"
- else
- printf "%s days\n" "$d"
- fi
- }
- shuffle() {
- n=$1
- shuf -i "1-$n" -n "$n"
- }
- alarm() {
- echo "notify-send -u critical -i appointment-soon 'Alarm' '$2'; ffplay ~adam/Music/mps/cloister.ogg" | at "$1"
- }
- ide() {
- # todo get name from build script
- kak -clear
- server_name=$(basename "$PWD")
- socket_file=$(kak -l | grep "$server_name")
- if [ "$socket_file" = '' ]
- then
- kak -d -s "$server_name"
- fi
- kak -c "$server_name" "$@"
- }
- guak() {
- if [ $(ps aux | grep -c 'alacritty --class guak') -lt 2 ]
- then
- /home/adam/.local/bin/alacritty --class guake &
- usleep 500000
- fi
- swaymsg '[app_id="guake"] scratchpad show'
- }
- hex2rgb() {
- hexinput=$(echo "$1"| tr '[:lower:]' '[:upper:]')
- if [ "$(echo "$hexinput" | cut -c1)" = '#' ]
- then
- hexinput=$(echo "$hexinput" |cut -c2-)
- fi
- a=$(echo "$hexinput" | cut -c-2)
- b=$(echo "$hexinput" | cut -c3-4)
- c=$(echo "$hexinput" | cut -c5-6)
- r=$(qalc -t -s 'base 10' "0x$a")
- g=$(qalc -t -s 'base 10' "0x$b")
- b=$(qalc -t -s 'base 10' "0x$c")
- echo "$r" "$g" "$b"
- }
- import_gsettings() {
- for pair in "$@"; do
- gKey=$(echo "$pair" | cut -d':' -f1)
- name=$(echo "$pair" | cut -d':' -f2)
- value=$(cat "${XDG_CONFIG_HOME:-$HOME/.config}"/gtk-3.0/settings.ini | grep "$name" | cut -d'=' -f2)
- gsettings set org.gnome.desktop.interface "$gKey" "$value"
- done
- }
- pad() {
- num=$1
- while [ "${#num}" -lt "$2" ]
- do
- num="0$num"
- done
- echo "$num"
- }
- now() {
- now=$(qalc -t -b '12' "int(($(date +%H) * 3600000000000 + $(date +%M) * 60000000000 + $(date +%S) * 1000000000 + $(date +%N)) / 1000000000 * 144 / 25)")
- now=$(pad "$now" 6)
- hora=$(echo "$now" | colrm 3)
- dunatim=$(echo "$now" | colrm 5 | rev | colrm 3 | rev)
- tim=$(echo "$now" | rev | colrm 3 | rev)
- if [ "$1" = "-t" ]
- then
- printf "{\"text\": \"%s.%s\"}\n" "$hora" "$dunatim"
- else
- printf "{\"text\": \"%s.%s.%s\"}\n" "$hora" "$dunatim" "$tim"
- fi
- }
- screenOnOff() {
- v=$(light -s sysfs/backlight/intel_backlight -G)
- if [ "$v" = "100.00" ]
- then
- light -s sysfs/backlight/intel_backlight -S 0
- else
- light -s sysfs/backlight/intel_backlight -S 100
- fi
- }
- streamDwd() {
- if [ $# -lt 1 ]
- then
- min=1800
- else
- min=$(echo "$1 * 60" | bc)
- fi
- echo "Downloading for $min seconds"
- streamripper http://195.150.20.243:8000/rmf_classic -s -A -a %D -d /home/adam/Downloads/streams/RMFClassic/ -l "$min"
- doas /bin/poweroff
- }
- ffmpegQ() {
- height=$(ffprobe "$*" -show_entries stream=height -of csv=p=0 -v error -select_streams v:0)
- if [ "$height" -lt 480 ]
- then
- echo 37
- elif [ "$height" -lt 720 ]
- then
- echo 35
- elif [ "$height" -lt 1080 ]
- then
- echo 34
- else
- echo 33
- fi
- }
- toVP9() {
- Q=$(ffmpegQ "$1")
- set -x
- ffmpeg -i "$1" -crf "$Q" -threads 16 -b:v 0 -c:v libvpx-vp9 -r 25 "${1%.*}.vp9.webm"
- }
- toAV1() {
- Q=$(ffmpegQ "$1")
- set -x
- 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"
- }
- touchpad() {
- ID=$(xinput list | grep -Eo 'TouchPad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}')
- State=$(xinput list-props "$ID" | grep 'Device Enabled' | awk '{print $4}')
- if [ "$State" -eq 1 ]
- then
- xinput disable "$ID"
- else
- xinput enable "$ID"
- fi
- }
|