-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvt
More file actions
executable file
·163 lines (133 loc) · 3.3 KB
/
nvt
File metadata and controls
executable file
·163 lines (133 loc) · 3.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env zsh
setopt errexit nounset pipefail extendedglob
readonly SOCKET_DIR="${XDG_RUNTIME_DIR:-/tmp}"
readonly MAX_TTY_DEPTH=20
check_requirements() {
local missing=0
local required_cmds=(nvim lsof)
if [[ -n "$TMUX" ]]; then
required_cmds+=(tmux)
fi
for cmd in "${required_cmds[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: Required command '$cmd' not found" >&2
missing=1
fi
done
if [[ $missing -eq 1 ]]; then
exit 1
fi
}
get_pipe() {
local dir
local servername
if [[ -n "$TMUX" && -n "$TMUX_PANE" ]]; then
servername="t$(tmux list-panes -t "$TMUX_PANE" -F '#S' | head -n1)"
else
dir="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
servername="s${dir//\//__}"
fi
echo "${SOCKET_DIR}/nvt-${servername}.pipe"
}
get_tty() {
local pid="${1}"
local depth="${2:-0}"
if [[ "$pid" == "1" ]] || [[ $depth -ge $MAX_TTY_DEPTH ]]; then
return 1
fi
local tty
if ! tty=$(ps -p "$pid" -o "tty" 2>/dev/null | tail -1); then
return 1
fi
tty="${tty## #}"
tty="${tty%% #}"
if [[ "$tty" == "??" || "$tty" == "?" ]]; then
local ppid
if ! ppid=$(ps -p "$pid" -o "ppid" 2>/dev/null | tail -1); then
return 1
fi
ppid="${ppid## #}"
ppid="${ppid%% #}"
get_tty "$ppid" $((depth + 1))
return
fi
echo "$tty"
}
go_to_instance() {
if [[ -n "$TMUX" && -n "$TMUX_PANE" ]]; then
local socket="${1}"
local pid
pid="$(lsof -t "$socket" 2>/dev/null | head -1)"
if [[ -z "$pid" ]]; then
return 0
fi
local tty
if ! tty=$(get_tty "$pid" 2>/dev/null); then
return 0
fi
if [[ -z "$tty" ]]; then
return 0
fi
local tmux_path
if ! tmux_path=$(tmux list-panes -s -F'#I.#P,#{pane_tty}' 2>/dev/null | grep "${tty}\$" | cut -d ',' -f1); then
return 0
fi
if [[ -z "$tmux_path" ]]; then
return 0
fi
tmux select-pane -t "$tmux_path" 2>/dev/null && tmux select-window -t "${tmux_path%%.*}" 2>/dev/null
fi
return 0
}
make_absolute() {
local path="$1"
[[ "$path" = /* ]] && echo "$path" || echo "$PWD/$path"
}
resolve_absolute_path() {
local path="$1"
local result
if [[ -e "$path" ]]; then
if result=$(realpath -s "$path" 2>/dev/null); then
echo "$result"
else
make_absolute "$path"
fi
else
make_absolute "$path"
fi
}
is_socket_alive() {
local socket="$1"
[[ -e "$socket" ]] && lsof -t "$socket" &>/dev/null
}
cleanup() {
local exit_code=$?
if [[ $exit_code -ne 0 ]] && [[ -e "$socket" ]] && ! is_socket_alive "$socket"; then
rm -f "$socket" 2>/dev/null
fi
}
check_requirements
readonly socket="$(get_pipe)"
trap cleanup EXIT INT TERM
if is_socket_alive "$socket"; then
if [[ $# -gt 0 ]]; then
typeset -a abs_paths
for arg in "$@"; do
abs_paths+=("$(resolve_absolute_path "$arg")")
done
if ! nvim --server "$socket" --remote-silent "${abs_paths[@]}" 2>/dev/null; then
echo "Error: Failed to send files to nvim server" >&2
exit 1
fi
else
echo "NVIM already running on $socket"
echo "Usage: $0 <file1> [file2 ...]"
fi
go_to_instance "$socket"
elif [[ -e "$socket" ]]; then
echo "Warning: Stale socket found at $socket, removing it" >&2
rm -f "$socket"
nvim --listen "$socket" "$@"
else
nvim --listen "$socket" "$@"
fi