diff --git a/bash/.bashrc b/bash/.bashrc new file mode 100644 index 0000000..5b3746c --- /dev/null +++ b/bash/.bashrc @@ -0,0 +1,21 @@ +# ~/.bashrc + +[[ $- != *i* ]] && return + +BASH_CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/bash" + +for config in \ + env \ + aliases \ + build \ + perf \ + systemd \ + tools +do + file="$BASH_CONFIG_DIR/$config.bash" + [[ -r "$file" ]] && source "$file" +done + +# Machine/private configuration. +[[ -r "$BASH_CONFIG_DIR/local.bash" ]] && + source "$BASH_CONFIG_DIR/local.bash" diff --git a/bash/aliases.bash b/bash/aliases.bash new file mode 100644 index 0000000..0d556ec --- /dev/null +++ b/bash/aliases.bash @@ -0,0 +1,20 @@ +if command -v eza &>/dev/null; then + alias ls='eza -1 --icons' + alias ll='eza -la --icons' + alias la='eza -a --icons' + alias l='eza --icons' +else + alias ll='ls -alF' + alias la='ls -A' + alias l='ls -CF' +fi + +alias lsf='ls -p | grep -v /' + +alias grep='grep --color=auto' +alias fgrep='fgrep --color=auto' +alias egrep='egrep --color=auto' + +alias alert='notify-send --urgency=low \ + -i "$([ $? = 0 ] && echo terminal || echo error)" \ + "$(history | tail -n1 | sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' diff --git a/bash/build.bash b/bash/build.bash new file mode 100644 index 0000000..68b9c0a --- /dev/null +++ b/bash/build.bash @@ -0,0 +1,101 @@ +_builddir_for() { + case "$1" in + debug) + printf '%s\n' "${BUILDDIR_DEBUG:-builddir-debug}" + ;; + release) + printf '%s\n' "${BUILDDIR_RELEASE:-builddir-release}" + ;; + *) + echo "Unknown build type: $1" >&2 + return 2 + ;; + esac +} + +_ensure_builddir() { + local type="$1" + local dir + local native_args=() + + dir="$(_builddir_for "$type")" || return + + if [[ -f native.ini ]]; then + native_args+=(--native-file native.ini) + fi + + if [[ ! -f "$dir/meson-private/coredata.dat" ]]; then + echo "Configuring Meson $type build in '$dir'..." >&2 + + meson setup "$dir" \ + --buildtype="$type" \ + "${native_args[@]}" >&2 || return + fi + + printf '%s\n' "$dir" +} + +_nic() { + local type="$1" + shift + + local dir + dir="$(_ensure_builddir "$type")" || return + + ninja -C "$dir" "$@" +} + +nic() { + _nic "${BUILD_TYPE:-debug}" "$@" +} + +nic-debug() { + _nic debug "$@" +} + +nic-release() { + _nic release "$@" +} + +nit() { + if (($# == 0)); then + echo "usage: nit [args...]" >&2 + return 2 + fi + + local target="$1" + local type="${BUILD_TYPE:-debug}" + local dir + + shift + + dir="$(_ensure_builddir "$type")" || return + + ninja -C "$dir" "$target" || return + "$dir/$target" "$@" +} + +nicc() { + if (($# == 0)); then + echo "usage: nicc [ninja args...]" >&2 + return 2 + fi + + local target="$1" + local type="${BUILD_TYPE:-debug}" + local dir + local filename + + shift + + dir="$(_ensure_builddir "$type")" || return + filename="${target##*/}" + + ninja -C "$dir" "$target" "$@" || return + + install -Dm755 \ + "$dir/$target" \ + "$HOME/.local/bin/$filename" || return + + "$filename" --version +} diff --git a/bash/env.bash b/bash/env.bash new file mode 100644 index 0000000..d7598cf --- /dev/null +++ b/bash/env.bash @@ -0,0 +1,37 @@ +export TERM=xterm-256color + +path_prepend() { + [[ -d "$1" ]] || return + case ":$PATH:" in + *":$1:"*) ;; + *) PATH="$1:$PATH" ;; + esac +} + +path_append() { + [[ -d "$1" ]] || return + case ":$PATH:" in + *":$1:"*) ;; + *) PATH="$PATH:$1" ;; + esac +} + +path_prepend "$HOME/.local/bin" +path_prepend "${GOBIN:-${GOPATH:-$HOME/go}/bin}" +path_prepend "$HOME/.cargo/bin" +path_prepend "$HOME/.ghcup/bin" +path_prepend "$HOME/gitui" +path_append "$HOME/Git/FlameGraph" +path_append "$HOME/.fzf/bin" + +export PATH + +export FZF_DEFAULT_COMMAND='fd --type file' +export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" + +HISTCONTROL=ignoreboth +HISTSIZE=10000 +HISTFILESIZE=20000 + +shopt -s histappend +shopt -s checkwinsize diff --git a/bash/perf.bash b/bash/perf.bash new file mode 100644 index 0000000..8bb7585 --- /dev/null +++ b/bash/perf.bash @@ -0,0 +1,35 @@ +alias perf-rec='perf record -g -e cpu-cycles -p' +alias perf-rep='perf report' + +perf-dat() { + perf script | stackcollapse-perf.pl > "$1" +} + +flamegraph() { + local perf_name="${1:-pdat}" + + [[ -f perf.data ]] || { + echo "perf.data not found" >&2 + return 1 + } + + mkdir -p img + + perf-dat "$perf_name" && + flamegraph.pl "$perf_name" > "img/$perf_name.svg" +} + +flamegraph-diff() { + if [[ -f "$1" && -f "$2" ]]; then + mkdir -p img + difffolded.pl "$1" "$2" | + flamegraph.pl > "img/perf-diff-$1-$2.svg" + fi +} + +binlog-dump() { + barsic dump \ + --prefix="$1" \ + --no-start-echo \ + -- binlog-dumper --module "$2" +} diff --git a/bash/systemd.bash b/bash/systemd.bash new file mode 100644 index 0000000..c05e71f --- /dev/null +++ b/bash/systemd.bash @@ -0,0 +1,27 @@ +set-scope() { + local scope="${1:-default-scope}" + + systemd-run \ + --user \ + --scope \ + --unit="$scope" \ + bash +} + +check-scope() { + local scope="${1:-default-scope}" + + systemctl --user status "$scope.scope" +} + +shtop() { + local scope="${1:-default-scope.scope}" + local cg + + cg=$(systemctl --user show \ + "$scope" \ + -p ControlGroup \ + --value) || return + + htop -p "$(paste -sd, "/sys/fs/cgroup${cg}/cgroup.procs")" +} diff --git a/bash/tools.bash b/bash/tools.bash new file mode 100644 index 0000000..da78446 --- /dev/null +++ b/bash/tools.bash @@ -0,0 +1,34 @@ +# bash-completion +if ! shopt -oq posix; then + if [[ -r /usr/share/bash-completion/bash_completion ]]; then + source /usr/share/bash-completion/bash_completion + elif [[ -r /etc/bash_completion ]]; then + source /etc/bash_completion + fi +fi + +# ble.sh +[[ -r "$HOME/.local/share/blesh/out/ble.sh" ]] && + source "$HOME/.local/share/blesh/out/ble.sh" + +# NVM +export NVM_DIR="$HOME/.nvm" + +[[ -s "$NVM_DIR/nvm.sh" ]] && + source "$NVM_DIR/nvm.sh" + +[[ -s "$NVM_DIR/bash_completion" ]] && + source "$NVM_DIR/bash_completion" + +# Shell integrations +command -v starship &>/dev/null && + eval "$(starship init bash)" + +command -v zoxide &>/dev/null && + eval "$(zoxide init bash)" + +command -v atuin &>/dev/null && + eval "$(atuin init bash)" + +command -v direnv &>/dev/null && + eval "$(direnv hook bash)"