1# Software list:
  2#   xss-lock xkbset xclip xsel xwininfo cifs-utils picom ffmpeg
  3#   vim git tmux maim mc htop entr rsync jq rofi stow newsboat 
  4#   st rsync curl hstr tree make gdb nvtop ctags mpv lazygit fd
  5
  6# Only run if the script is being sourced (bashrc).
  7if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
  8	# Magical environment variables.
  9	export LANG=en_US.UTF-8
 10	export LC_ALL=en_US.UTF-8
 11	export COLORTERM=truecolor
 12	export TERM=xterm-256color
 13	export VISUAL=vim
 14	export EDITOR=vim
 15
 16	# Customized Bash prompt.
 17	SYMBOL='\[\e[38;5;214m\]\$\[\e[0m\]'
 18	git_branch() { git branch 2>/dev/null | sed -n 's/^\* \(.*\)/(\1)/p'; }
 19	export PS1="\n$SYMBOL \u@\h \t \w \$(git_branch)\n$SYMBOL "
 20
 21	# General aliases.
 22	alias l='ls -lh --group-directories-first --color=always'
 23	alias ll='ls -lha --group-directories-first --color=always'
 24	alias t='tree -L 2'
 25	alias ..='cd ..'
 26	alias gg='lazygit'
 27	alias less='less -R'
 28	alias tmux='tmux -T 256 -u'
 29	alias server='python3 -m http.server 6969'
 30	alias newsboat='newsboat -r -u ~/.feeds.txt'
 31	alias emptytrash='gio trash --empty'
 32
 33	# Custom folder jump commands.
 34	alias j='cd ~/Junk'
 35	alias p='cd ~/Projects'
 36	alias d='cd ~/Downloads'
 37
 38	# History and search. Stolen from J.
 39	HISTCONTROL=ignoreboth
 40	shopt -s histappend
 41	export HISTSIZE=
 42	export HISTFILESIZE=
 43	export HISTFILE=~/.bash_history_infinite
 44	PROMPT_COMMAND="history -a; ${PROMPT_COMMAND}"
 45	bind '"\e[A": history-search-backward'
 46	bind '"\e[B": history-search-forward'
 47	export HSTR_CONFIG=hicolor
 48	if [[ $- =~ .i. ]]; then bind '"\C-h": "\C-a hstr -- \C-j"'; fi
 49
 50	# Custom paths.
 51	export PATH="$PATH:$HOME/.local/bin"
 52	export PATH="$PATH:$HOME/.cargo/bin"
 53	export PATH="$PATH:$HOME/go/bin"
 54	export PATH="$PATH:$HOME/Applications"
 55fi
 56
 57backup() {
 58	SNAPSHOT=$(date +%Y-%m-%d)-$(whoami)@$(hostname)
 59	mkdir -p /tmp/$SNAPSHOT
 60
 61	archive_sets=(
 62		"ssh.zip $HOME/.ssh"
 63		"bash_history_infinite.zip $HOME/.bash_history_infinite"
 64		"projects.zip $HOME/Projects"
 65	)
 66
 67	for entry in "${archive_sets[@]}"; do
 68		zip -r /tmp/$SNAPSHOT/${entry} -x "**/.venv/*" "**/.git/*" "**/.import/*" "**/.godot/*" "**/.zig-cache/*" "**/node_modules/*"
 69	done
 70
 71	rsync -azhv /tmp/$SNAPSHOT /media/Void/Backup
 72	rm -Rf /tmp/$SNAPSHOT
 73}
 74
 75slugify() {
 76	local text="$1"
 77	echo "$text" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+|-+$//g'
 78}
 79
 80worldclocks() {
 81	printf "%-18s %s\n" "Local:" "$(TZ='Europe/Ljubljana' date +'%a %H:%M')"
 82	printf "%-18s %s\n" "Brisbane:" "$(TZ='Australia/Brisbane' date +'%a %H:%M')"
 83	printf "%-18s %s\n" "San Francisco:" "$(TZ='America/Los_Angeles' date +'%a %H:%M')"
 84	printf "%-18s %s\n" "New York:" "$(TZ='America/New_York' date +'%a %H:%M')"
 85}
 86
 87record() {
 88	mkdir -p "$HOME/Videos"
 89	win=$(xwininfo -int | awk '/Window id:/{print $4; exit}')
 90	[ -z "$win" ] && { echo "no window picked"; exit 1; }
 91
 92	eval $(
 93		xwininfo -id "$win" | awk '
 94			/Absolute upper-left X:/{x=$4}
 95			/Absolute upper-left Y:/{y=$4}
 96			/Width:/{w=$2}
 97			/Height:/{h=$2}
 98		END{printf("X=%s;Y=%s;W=%s;H=%s\n", x, y, w, h)}
 99		'
100	)
101
102	if [ -z "$W" ] || [ -z "$H" ]; then
103		echo "failed to get geometry"; exit 1
104	fi
105
106	W=$((W / 2 * 2))
107	H=$((H / 2 * 2))
108
109	ts=$(date '+%F-%H-%M-%S')
110	out="$HOME/Videos/record-${ts}.mp4"
111	echo "Recording ${W}x${H}+${X},${Y} -> ${out}"
112
113	ffmpeg -y -f x11grab -video_size "${W}x${H}" -framerate 60 -i "${DISPLAY:-:0.0}+${X},${Y}" -c:v libx264 -preset veryfast -crf 23 -pix_fmt yuv420p "$out"
114}