-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions
executable file
·105 lines (88 loc) · 2.86 KB
/
sessions
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
#!/usr/bin/bash
if [ "$(command -v zellij)" = "" ]; then
echo "Zellij is not installed"
exit 1
fi
LAST_SESSION="$(cat /tmp/zellij_last_session || '')"
home_replacer() {
HOME_REPLACER="" # default to a noop
echo "$HOME" | grep -E "^[a-zA-Z0-9\-_/.@]+$" &>/dev/null # chars safe to use in sed
HOME_SED_SAFE=$?
if [ $HOME_SED_SAFE -eq 0 ]; then # $HOME should be safe to use in sed
HOME_REPLACER="s|^$HOME/|~/|"
fi
echo "$HOME_REPLACER"
}
transform_home_path() {
HOME_SED_SAFE=$?
if [ $HOME_SED_SAFE -eq 0 ]; then
echo "$1" | sed -e "s|^~/|$HOME/|"
else
echo "$1"
fi
}
fzf_window() {
fzf --reverse --no-sort --border "rounded" --info inline --pointer "→" --prompt "Session > " --header "Select session" --preview "bash -c \"echo {2} | grep -q 'Session' && echo {} || ls {3}\""
}
set_last_session() {
echo "$1" > /tmp/zellij_last_session
}
last_session() {
if [[ -n "$LAST_SESSION" ]]; then
echo $LAST_SESSION | awk '{ print "("NR")\t[Session]\t"$1 }'
fi
}
sessions_list(){
if [[ -n "$LAST_SESSION" ]]; then
zellij list-sessions -s | grep -v "$(echo $LAST_SESSION)" | awk '{ print "("NR")\t[Session]\t"$1 }'
else
zellij list-sessions -s | awk '{ print "("NR")\t[Session]\t"$1 }'
fi
}
project_list(){
list=$(find ~/Projects/ -maxdepth 1 -type d)
list="$(realpath ~/dotfiles) ${list}"
echo $list | tr --truncate-set1 " /" "\n" | awk '{ print "("NR")\t[Directory]\t"$1 }'
}
select_project() {
project_dir=$({ last_session; sessions_list; project_list; } | fzf_window)
if [ "$project_dir" = "" ]; then
exit
fi
echo "$project_dir"
}
get_sanitized_selected(){
echo "$1" | sed "s/^([0-9]*)\t\[[^]]*\]\t//"
}
get_session_name() {
project_dir=$1
directory=$(basename "$project_dir")
session_name=$(echo "$directory" | tr ' .:' '_')
echo "$session_name"
}
if [[ -n "$1" ]]; then
selected=$(realpath $1)
else
selected=$(select_project)
fi
if [ -z "$selected" ]; then
exit 0
fi
cwd=$(get_sanitized_selected "$selected")
session_name=$(get_session_name "$(transform_home_path "$cwd")")
session=$(zellij list-sessions | grep "$session_name")
current_session=$(zellij list-sessions -n | grep '(current)' | grep -o '^[^ ]*')
is_current_session=$(zellij list-sessions -n | grep "^$session_name \[Created" | grep "(current)")
set_last_session "$current_session"
# If we're inside of zellij, detach
if [[ -n "$ZELLIJ" ]]; then
if [[ -z "$is_current_session" ]]; then
zellij pipe --plugin https://github.com/mostafaqanbaryan/zellij-switch/releases/download/0.2.0/zellij-switch.wasm -- "--session=$session_name --cwd=$cwd"
fi
else
if [[ -n "$session" ]]; then
zellij attach $session_name -c
else
zellij attach $session_name -c options --default-cwd $cwd
fi
fi