-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshrc_helpers
More file actions
executable file
·145 lines (126 loc) · 4.41 KB
/
Copy pathshrc_helpers
File metadata and controls
executable file
·145 lines (126 loc) · 4.41 KB
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
# Source helper function definitions
__rc_helpers_main() {
eval "$(__sh_color_definitions)"
eval "$(__sh_os_definitions)"
local SH_INTERACTIVE
case $- in
*i*)
# interactive shell
SH_INTERACTIVE=1
;;
esac
__add_to_path() {
local SHOW_HEADER=1
__add_to_path_no_header "$@"
}
__add_to_path_no_header() {
local PATH_DIRS PATH_DIR PATH_DIRS_LIST
PATH_DIRS=( "$@" )
for PATH_DIR in "${PATH_DIRS[@]}"; do
if [[ -d $PATH_DIR ]]; then
if [[ ":$PATH:" != *":$PATH_DIR:"* ]]; then
if [[ -n $SHOW_HEADER ]]; then
[[ $SH_INTERACTIVE ]] && echo
[[ $SH_INTERACTIVE ]] && echo -e 'Adding to the '$COLOR_CYAN_BOLD'path'$COLOR_NONE':'
unset SHOW_HEADER
fi
[[ $SH_INTERACTIVE ]] && echo -e ' '$COLOR_YELLOW_BOLD$PATH_DIR$COLOR_NONE
if [[ -n $PATH_DIRS_LIST ]]; then
PATH_DIRS_LIST=$PATH_DIRS_LIST:$PATH_DIR
else
PATH_DIRS_LIST=$PATH_DIR
fi
fi
fi
done
if [[ -n $PATH_DIRS_LIST ]]; then
if [[ -n $PATH ]]; then
export PATH=$PATH_DIRS_LIST:$PATH
else
export PATH=$PATH_DIRS_LIST
fi
fi
}
__add_to_cd_path() {
local SHOW_HEADER=1
__add_to_cd_path_no_header $*
}
__add_to_cd_path_no_header() {
local CDPATH_DIRS CDPATH_DIR CDPATH_DIRS_LIST
CDPATH_DIRS=( $* )
for CDPATH_DIR in "${CDPATH_DIRS[@]}"; do
if [[ -d $CDPATH_DIR ]]; then
if [[ ":$CDPATH:" != *":$CDPATH_DIR:"* ]]; then
if [[ -n $SHOW_HEADER ]]; then
[[ $SH_INTERACTIVE ]] && echo
[[ $SH_INTERACTIVE ]] && echo -e 'Adding to the '$COLOR_CYAN_BOLD'cd path'$COLOR_NONE':'
unset SHOW_HEADER
fi
[[ $SH_INTERACTIVE ]] && echo -e ' '$COLOR_YELLOW_BOLD$CDPATH_DIR$COLOR_NONE
if [[ -n $CDPATH_DIRS_LIST ]]; then
CDPATH_DIRS_LIST=$CDPATH_DIRS_LIST:$CDPATH_DIR
else
CDPATH_DIRS_LIST=$CDPATH_DIR
fi
fi
fi
done
if [[ -n $CDPATH_DIRS_LIST ]]; then
if [[ -n $CDPATH ]]; then
export CDPATH=$CDPATH:$CDPATH_DIRS_LIST
else
export CDPATH=$CDPATH_DIRS_LIST
fi
fi
}
__include_files() {
local SH_FILES SH_FILE
SH_FILES=( $* )
for SH_FILE in "${SH_FILES[@]}"; do
if [[ -f "$SH_FILE" ]]; then
[[ $SH_INTERACTIVE ]] && echo -e 'Loading '$COLOR_YELLOW_BOLD$SH_FILE$COLOR_NONE
. "$SH_FILE"
fi
done
}
# Pick a tmux session for SSH auto-attach.
# auto: prefer the base session, but avoid sharing it when it already has clients
# shared: always use the base session
# dedicated: always use a per-connection session
__tmux_auto_attach_target_session() {
local base_session_name mode clients client_ip client_port server_ip server_port session_suffix
base_session_name="${TMUX_AUTO_ATTACH_SESSION:-main}"
mode="${TMUX_AUTO_ATTACH_MODE:-auto}"
case "$mode" in
shared)
echo "$base_session_name"
return
;;
dedicated)
;;
auto)
clients=$(tmux list-clients -t "$base_session_name" 2>/dev/null)
if [[ -z $clients ]]; then
echo "$base_session_name"
return
fi
;;
*)
echo "$base_session_name"
return
;;
esac
if [[ -n $SSH_CONNECTION ]]; then
read client_ip client_port server_ip server_port <<< "$SSH_CONNECTION"
session_suffix="${client_ip}-${client_port}"
elif [[ -n $SSH_TTY ]]; then
session_suffix="$SSH_TTY"
else
session_suffix="$$"
fi
session_suffix=$(printf '%s' "$session_suffix" | tr -c '[:alnum:]_-' '-')
echo "${base_session_name}-${session_suffix}"
}
}
__rc_helpers_main "$@"
unset -f __rc_helpers_main