summaryrefslogtreecommitdiff
path: root/.config/bash/rc.sh
blob: 70f1ce41181177d3936411623af0af165d17636d (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
#!/hint/bash
# ~/.bashrc: Sourced by bash(1) for interactive non-login shells
#
# I include this file for all interactive invocations of bash(1), whether
# they are login shells or not.

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

# GDM failsafe ignores profile (login) settings, but I use XDG stuff
# here.
. "${XDG_CONFIG_HOME:-$HOME/.config}"/login.d/??_xdg.sh

# Why is this not on by default?
# "We have a cached value, but it isn't valid anymore. Should we trash it?"
# "Duh, yes!"
shopt -s checkhash

################################################################################

# History settings
# don't overwrite GNU Midnight Commander's setting of `ignorespace'.
HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
HISTFILE=${XDG_CACHE_HOME}/bash/history
HISTTIMEFORMAT='[%Y-%m-%d %H:%M] '
HISTSIZE=5000
shopt -s histappend # append to the history file, don't overwrite it
mkdir -p "${HISTFILE%/*}"

# General settings
shopt -s checkwinsize # update the values of LINES and COLUMNS
shopt -s globstar # Let ** recursively scan directories

################################################################################
# Overly complicated setting of PS1                                            #
################################################################################

# Belongs in aliases, but I use it here
term-title() {
	local fmt=''
	case "$TERM" in
		screen|tmux) fmt='\ek%s\e\\';;
		xterm*|rxvt*) fmt='\e]0;%s\a';;
	esac
	printf "$fmt" "$*"
}
PROMPT_COMMAND=''

make_prompt() {
	echo "${BOLD}${GREEN}\u@\h${BLUE}:\w${RESET}"
}

if tput setaf 1 &>/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	RESET="$(tput sgr0)"
	BOLD="$(tput bold)"
	RED="$(tput setaf 1)"
	GREEN="$(tput setaf 2)"
	BLUE="$(tput setaf 4)"

	_STATUS="${BOLD}["
	_STATUS+="\$(v=\$?; [[ \$v = 0 ]] && c='${GREEN}' || c='${RED}'; printf %s%03i \$c \$v)"
	_STATUS+="${RESET}${BOLD}]${RESET}"
else
	_STATUS='[$(printf "%03i" $?)]'
fi
PS1="${_STATUS} $(make_prompt)"'\n\$ '
unset RESET BOLD RED GREEN BLUE _STATUS
PS1="$(term-title $(make_prompt))$PS1"
unset make_prompt

################################################################################

# Include modular config files
if [[ -d ${XDG_CONFIG_HOME}/bash/rc.d ]]; then
	for file in "${XDG_CONFIG_HOME}/bash/rc.d"/*.sh; do
		. "$file"
	done
fi