~nijaba/byobu/configure-keybindings

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
#!/bin/sh -e
#
#    GNU screen-profiles
#    Copyright (C) 2008 Canonical Ltd.
#
#    Authors: Dustin Kirklan <dustin.kirkland@ubuntu.com>
#             Nick Barcet <nick.barcet@ubuntu.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


# To generate localization information, run:
#   xgettext -o - -L Shell select-screen-profile

usage () {
    cat <<EOT
usage: select-screen-profiles [(-l|--list)][(-h|--help)][(-s|--set) PROFILE]
    -l,--list           list available profiles
    -s,--set PROFILE    set profile
    -h,--help           this help

    Without any parameters, runs interactively.
EOT
}

# Initialize variables
BASE_DIR="/usr/share/screen-profiles"
PROFILE_DIR="$BASE_DIR/profiles"
profile=""
selected=-1

assert_symlink() {
	if [ -e "$1" ]; then
		if [ ! -L "$1" ]; then
			echo `gettext 'Error:'` $1 `gettext ' file exists, but is not a symlink'`
			exit 1
		fi
	fi
}

# If these files exist, they must be a symlink
assert_symlink "$HOME/.screenrc-profile"

listprofiles() {
	# Display list of profiles, one per line
	for x in $(ls $PROFILE_DIR); do
		if [ $x = "common" ]; then
			# Skip the common profile, no value there
			continue
		fi
		echo "$x"
	done
}

prompt() {
	# Prompt the user to choose among the available profiles
	echo
	echo `gettext "Select a screen profile: "`
	i=0
	profiles=$(listprofiles)
	for x in $profiles; do
		i=$(expr $i + 1)
		desc="          "
		if [ $x = "ubuntu" ]; then
			desc="<---- ` gettext 'recommended'`"
			simple=$i
		fi
		echo "  $i. $x\t\t$desc"
	done
	echo
	selected=x
	while /bin/true; do
		if [ -z "$selected" -a ! -z "$simple" ]; then
			selected="$simple"
		elif ! test $selected -gt 0 2>/dev/null; then
			read -p "`gettext 'Choose: '` 1-$i [$simple]: " -r selected
		elif ! test $selected -le $i 2>/dev/null; then
			read -p "`gettext 'Choose: '` 1-$i [$simple]: " -r selected
		else
			break
		fi
	done
	SELECTED="$selected"
}

setprofile() {
	# Apply a profile by name or index
	if [ -n "$1" ]; then
		selected="$1"
	else
		selected="$SELECTED"
	fi
	i=0
	found=0
	profiles=$(listprofiles)
	for x in $profiles; do
		i=`expr $i + 1`
		if [ "$i" = "$selected" -o "$x" = "$selected" ]; then
			if [ ! -e "$HOME/.screenrc" ]; then
				# If the user doesn't have a .screenrc, seed one
				echo "source $HOME/.screenrc-profile" > "$HOME/.screenrc"
			else
				# If the user does have a .screenrc, see if it has the
				# sources lines
				if ! grep -qs "source $HOME/.screenrc-profile" "$HOME/.screenrc"; then
					# And if it's missing the source line, add it
					# to the top
					tmp=`mktemp "$HOME/.screenrc.XXXXXX"`
					echo "source $HOME/.screenrc-profile" > "$tmp"
					cat "$HOME/.screenrc" >> "$tmp"
					mv -f "$tmp" "$HOME/.screenrc"
				fi
			fi
			rm -f "$HOME/.screenrc-profile"
			ln -s "$PROFILE_DIR/$x" "$HOME/.screenrc-profile"
			found=1
			break
		fi
	done
	if [ $found -eq 0 ]; then
		echo "Invalid profile"
	fi
	if [ ! -e "$HOME/.screenrc-windows" ]; then
		cp "$BASE_DIR/windows/common" "$HOME/.screenrc-windows"
	fi
}

if [ $# -eq 0 ]; then
	prompt
	setprofile
else
	TEMP=`getopt -o lhs: --long list,help,set: -- "$@"`
	eval set -- "$TEMP"
	while true; do
		case "$1" in
			-s|--set)
				setprofile "$2"
				shift 2
				break
			;;
			-l|--list)
				listprofiles
				shift
				break
			;;
			*)
				usage()
				exit 1
			;;
			--)
				shift
				break
			;;
		esac
	done
fi

exit 0