about summary refs log tree commit diff stats
path: root/bash/functions.bash
blob: 66724af6c217689c0b58592a3ff8fc2f4d0539f6 (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
# Functions

memq() { # memq ITEM ARRAY
	## Test whether an ITEM is a member of ARRAY.
	## Pass ARRAY as ${ARRAY[@]}.
	local e needle="$1"
	shift
	for e; do
		[[ "$e" == "$needle" ]] && {
			return 0
		}
	done
	return 1
}

rebashrc() { # rebashrc
	## Reload ~/.bashrc
	printf "Loading ~/.bashrc..." >&2
	if source "$HOME/.bashrc"; then
		echo "OK." >&2
	else
		echo "ERROR!" >&2
	fi
}

first_which() { # first_which COMMAND...
	## Return the fully-qualified path of the first COMMAND found in $PATH.
	while :; do
		command -v "$1" && break
		[ -z "$1" ] && return 1
		shift
	done
}

please() { # please [COMMAND...]
	# if run without arguments, run the last command with 'sudo' (aka sudo !!)
	# if run WITH arguments, alias as sudo
	history -d -1
	if [ -z "$1" ]; then
		#set --	$(HISTTIMEFORMAT=$'\t' history 2 | sed 's/^.*\t//;q')
		set -- $(fc -lnr | sed 1q)
	fi
	echo >&2 sudo "$@"
	history -s sudo "$@"
	"${DEBUG:-false}" || sudo "$@"
}

mkcd() {
	if [ $# -lt 1 ]; then
		command cd
		return "$?"
	fi
	if [ "x$1" = x- ]; then
		command cd -
		return "$?"
	fi
	if ! [ -d "$1" ]; then
		read -p "$1 doesn't exist.  Create (y/N)? " yn
		case "$yn" in
			n* | N*) return 1 ;;
			y* | Y*) mkdir -p "$1" ;;
			*) return 1 ;;
		esac
	fi
	command cd "$1"
}
alias cd='mkcd '

# from tomasino
# alias julian='echo "x = $(date +%s); scale=5; x / 86400 + 2440587.5" | bc'
julian() {
	echo "x = $(date ${1:+-d "$*"} +%s); scale=5; x / 86400 + 2440587.5" | bc
}

# find files for pipelines
f() {
	find "${1:-$PWD}" -depth |
		while read -r file; do
			printf '%q\n' "$file"
		done
}

words() {
	grep -E "$1" /usr/share/dict/words |
		sed -e '/^[A-Z]/d' -e "/'s$/d"
}

if ! command -v dict >/dev/null 2>&1; then
	dict() {
		curl "dict://dict.org/d:$1" | less
	}
fi

if command -v thesauracles >/dev/null 2>&1; then
	thesauraphrase() {
		for word in "$@"; do
			thesauracles -q "$word"
		done | tr '\n' ' '
		echo
	}
fi

up() {
	: "${UP_TODIRECTORY:=..}"
	: "${UP_SPECIALARGS:=true}"
	local ret=0
	# echo "$UP_TODIRECTORY" "$UP_SPECIALARGS"
	if "$UP_SPECIALARGS"; then
		case "$1" in
			'') cd "$UP_TODIRECTORY" ;;
			up) UP_TODIRECTORY="${UP_TODIRECTORY}/.." up "${@:2}" ;;
			--) UP_SPECIALARGS=false up "${@:2}" ;;
			-*) if (( "$1" == -1 )); then
				    up
			    else
				    UP_TODIRECTORY="${UP_TODIRECTORY}/.." up $(( "$1" + 1 ))
			    fi
			    ;;
			*) while cd ..; do
				   case "$PWD" in
					   /) ret=1; break ;;
					   */"$1") break ;;
				   esac
			   done
			   ;;
		esac
	else
		case "$1" in
			'') cd "$UP_TODIRECTORY" ;;
			*) while cd ..; do
				   case "$PWD" in
					   /) ret=1; break ;;
					   */"$1") break ;;
				   esac
			   done
			   ;;
		esac
	fi
	UP_TODIRECTORY=
	UP_SPECIALARGS=
	return "$ret"
}

apk() { # wrapper for apk(1)
	apk="$(which apk)"
	test -z "$apk" && return 1
	case "$1" in
		add|del|fix|update|upgrade) doas "$apk" "$@" ;;
		install) shift; apk add "$@" ;;
		remove) shift; apk del "$@" ;;
		*) "$apk" "$@" ;;
	esac
}

sl() {
	printf >&2 '%s\n' "CHOO CHOO!"
	ls "$@"
}

mkssh() {
	ssh-keygen -t ed25519 -f "$HOME/.ssh/$1"
	cat "$HOME/.ssh/$1.pub" | tee /dev/stdout | wl-copy
}

p() {
	cmd="$(history |
		awk '{for(i=4;i<=NF;i++)printf "%s ",$i;print""}' |
		tail -n2 |
		head -n1)"
	echo "$cmd"
	eval $cmd
}