aboutsummaryrefslogtreecommitdiff
path: root/.bashrc
blob: 025661cfa5cf9366df1e03bf7f41fe86122b565b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
##
# Locale.

# Set LC_ALL always to UTF-8.
if [ "$(locale -a | grep ca_ES.utf8)" != "" ]; then
  export LC_ALL=ca_ES.utf8
else
  if [ "$(locale -a | grep en_US.utf8)" != "" ]; then
    export LC_ALL=en_US.utf8
  else
    echo "[Warning] Could not set up a proper locale." \
      "There is probably something missing on your host machine..."
  fi
fi

##
# Utility functions.

# Sources the given file if it really exists.
source_maybe() {
  if [ -f "$1" ]; then
    source "$1"
  fi
}

##
# If we are inside of a VM, chances are that the TTY size is borked. Let's
# resize the TTY to the actual terminal size.

if grep -Fq "hypervisor" /proc/cpuinfo; then
  if [ -x "$(command -v resize)" ]; then
    eval $(resize)
  else
    echo "Consider installing the $(resize) binary if you want a proper TTY size."
  fi
fi

##
# XDG: sourcing the variables on $HOME/.config/user-dirs.dirs might ease up the
# configuration of other tools.

source_maybe "$HOME/.config/user-dirs.dirs"

##
# Basics.

# Beautifying ls command.
alias ls='ls --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F'
alias lisa='ls -lisa --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F'

# Alias related to cd
alias cd..='cd ..'

##
# If `bat` is available, use it as a drop-in replacement for `cat (1)`.

if command -v bat &>/dev/null || command -v batcat &>/dev/null; then
  if command -v batcat &>/dev/null; then
    _bat_cmd="batcat"
  else
    _bat_cmd="bat"
  fi

  alias cat="$_bat_cmd"

  # Use it on man pages too.
  export MANPAGER="sh -c 'col -bx | $_bat_cmd -l man -p'"
  export MANROFFOPT="-c"

  unset _bat_cmd
fi

# Set up fzf key bindings and fuzzy completion
if command -v fzf &>/dev/null; then
  eval "$(fzf --bash)"
fi

# Some tools allow me to use another diff tool by using this environment variable.
export DIFF=difft

##
# Editors & terminals.

# The default editor is simply called 'e', which is a shell script that will
# call the proper editor depending on what's installed on the system.
export EDITOR=e
export VISUAL=e

# We want a full-fledged 256-color terminal.
TERM=xterm-256color

# Setup the prompt command for terminals that don't touch it by themselves
# (e.g. alacritty).
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'

# Set the LESS and the PAGER environment variables.
export LESS="FSRX"
export PAGER=less

# git thingies.
alias gti=git

# Setting up PS1.
export PS1='$(__git_ps1 "\[\033[0;32m\]@%s\[\033[0m\]\] ")\$ '

# Introduce `cclip` as a fast way to use `xclip` but selecting the clipboard.
alias cclip='xclip -selection clipboard'

##
# Programming languages and environments.

# Rake completion
if [ -f "$HOME/.rake_completion" ]; then
  complete -C "$HOME/.rake_completion" -o default rake
fi

# Alias bundle exec
alias be='bundle exec'

# After experimenting with GCC & Clang, I'm sticking with GCC.
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++

# Go things
export GOPATH=$HOME

##
# RISC-V

# Setting this value as expected by the Linux Kernel when building it.
export ARCH=riscv

# If this is not a RISC-V machine, set up cross compilation for it.
if [ "$(uname -m)" != "riscv64" ]; then
  export CROSS_COMPILE=riscv64-suse-linux-
fi

# Local builds for busybox and the Linux kernel. Needed by scripts like
# `bin/run-riscv-qemu.sh`.
export BUSYBOX_SRC="$HOME/src/busybox/1.36.1"

##
# Misc.

# The g utility. See: https://github.com/mssola/g
source_maybe "$HOME/.g.sh"
source_maybe "$HOME/.gcompletion.sh"

# Git prompt.
source_maybe "$HOME/.git-prompt.sh"

# Complete the `docker` command if possible.
source_maybe "$HOME/.dockercompletion.sh"

# Add the `bin` dir to the path if possible. See:
# https://github.com/mssola/dotfiles.
if [ -d "$HOME/bin" ]; then
  export PATH=$HOME/bin:$PATH
fi

# Some packages install inside of ~/.local/bin
export PATH=$HOME/.local/bin:$PATH

# Some other misc. alias.
alias iosc="osc -A https://api.suse.de"
alias random_string="cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1"

# Cargo environment.
source_maybe "$HOME/.cargo/env"

# Convert the given video into GIF. I have tested this with webM and it appears
# to work fine. This is just a wrapper for ffmpeg, but oh boy it's hard to
# remember anything ffmpeg-related...
#
# It accepts two parameters: scale (defaults to 320); and fps (defaults to 10).
#
# Taken from some random answer from StackOverflow, no longer remember from. If
# you see this and recognize it: thanks! and just give me a ping for
# acknowledging you.
video2gif() {
  ffmpeg -y -i "${1}" -vf fps=${3:-10},scale=${2:-320}:-1:flags=lanczos,palettegen "${1}.png"
  ffmpeg -i "${1}" -i "${1}.png" -filter_complex "fps=${3:-10},scale=${2:-320}:-1:flags=lanczos[x];[x][1:v]paletteuse" "${1}".gif
  rm "${1}.png"
}

# Handy alias for the ip command
alias ip="ip --color"

# If we have Rust's source code, export the special `RUST_SRC_PATH`, since this
# is useful for completion tools.
if command -v rustc &>/dev/null; then
  if [ -d "$(rustc --print sysroot)/lib/rustlib/src/rust/library" ]; then
    export RUST_SRC_PATH="$(rustc --print sysroot)/lib/rustlib/src/rust/library"
  fi
fi

##
# $ mise activate bash >> .bashrc

if command -v mise &>/dev/null; then
  export MISE_SHELL=bash
  export __MISE_ORIG_PATH="$PATH"

  mise() {
    local command
    command="${1:-}"
    if [ "$#" = 0 ]; then
      command mise
      return
    fi
    shift

    case "$command" in
    deactivate | s | shell)
      # if argv doesn't contains -h,--help
      if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then
        eval "$(command mise "$command" "$@")"
        return $?
      fi
      ;;
    esac
    command mise "$command" "$@"
  }

  _mise_hook() {
    local previous_exit_status=$?
    eval "$(mise hook-env -s bash)"
    return $previous_exit_status
  }
  if [[ ";${PROMPT_COMMAND:-};" != *";_mise_hook;"* ]]; then
    PROMPT_COMMAND="_mise_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
  fi
  if [ -z "${_mise_cmd_not_found:-}" ]; then
    _mise_cmd_not_found=1
    if [ -n "$(declare -f command_not_found_handle)" ]; then
      _mise_cmd_not_found_handle=$(declare -f command_not_found_handle)
      eval "${_mise_cmd_not_found_handle/command_not_found_handle/_command_not_found_handle}"
    fi

    command_not_found_handle() {
      if mise hook-not-found -s bash -- "$1"; then
        _mise_hook
        "$@"
      elif [ -n "$(declare -f _command_not_found_handle)" ]; then
        _command_not_found_handle "$@"
      else
        echo "bash: command not found: $1" >&2
        return 127
      fi
    }
  fi
fi