aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/term
blob: 3d89b01b229082ee27526ec5dc9f8446f92620ef (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
#!/bin/sh

# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.

# A list of possible terminal emulators, sorted by preference
TERMINALS="
	alacritty
	termite
	st
	urxvt
	rxvt
	xterm
"

main()
{
	# Handle opts
	while getopts ":hc" opt
	do
		case "$opt" in
			c) OPT_NO_HIST=1 ;;
			h) usage && exit 0 ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	shift $(( OPTIND - 1 ))

	# Unset history related environment variables if requested
	if [ "$OPT_NO_HIST" ]
	then
		unset HISTFILE
		unset HISTSIZE
		unset SAVEHIST

		export TERM_NO_HISTORY=1
	fi

	# Check the list of possible terminal emulators, and exec the first one
	# that's available
	for terminal in $TERMINALS
	do
		command -v "$terminal" > /dev/null || continue

		exec "$terminal" "$@"
	done

	# No usable terminal found, print error
	printf "No suitable terminal emulator found. The following were tried:\n"

	for terminal in $TERMINALS
	do
		printf "\t%s\n" "$terminal"
	done

	exit 127
}

usage()
{
	cat <<EOF
Usage:
	${0##*/} -h

Nondescript

Options:
	-h  Show this help text and exit.
	-c  Unset HISTFILE before starting the terminal.
EOF
}

main "$@"