aboutsummaryrefslogtreecommitdiff
path: root/.config/shell/functions.d/c
blob: bc1c033402b4ac50b4cf5409ba20006ee0f55e02 (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
#!/usr/bin/env sh

c()
{
	destination="$1"

	if [ -z "${SHELLSTORE}" ]
	then
		return 1
	fi

	store=${SHELLSTORE}/chdir_history

	# Make sure the store exists
	touch "${store}"

	if [ -z "$1" ]
	then
		destination=$(tail -n 1 "${store}")

		if [ "${destination}" != "" ]
		then
			sed '$ d' "${store}" > "${store}.sed"
			mv "${store}.sed" "${store}"
		fi
	else
		dots=$(expr "${destination}" : '^\.\+$')

		if [ "$dots" -gt 2 ]
		then
			destination=""

			while [ "${dots}" -gt 1 ]
			do
				destination="${destination}../"

				dots=$((dots - 1))
			done
		fi
	fi

	owd=$(pwd)

	cd "${destination}" || return 2
	l

	[ ! -z "$1" ] && printf "%s\n" "${owd}" >> "${store}"

	unset destination dots store
}