-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvim
executable file
·129 lines (113 loc) · 3 KB
/
dvim
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
#!/bin/bash
# ================================
# Variables
# ================================
CMD=""
# ================================
# Configuration
# ================================
image_name="nvim-alpine"
main_container_name="nvim-main"
resource_dir="$HOME/.local/share/nvim-alpine"
source $resource_dir/config.cfg
# ================================
# Set Builder
# ================================
builder=""
if [ "$CONTAINERIZER" == "docker" ]; then
if which docker-buildx > /dev/null 2>&1; then
builder="docker-buildx"
else
builder="docker"
fi
elif [ "$CONTAINERIZER" == "nerdctl" ]; then
builder="nerdctl"
if [ ! -d "/opt/cni/bin" ]; then
echo "CNI plugins not found. Please install CNI plugins before proceeding."
exit 1
fi
fi
# ================================
# Set Path
# ================================
real=""
if [ ! -z "$1" ]; then
real="$(realpath $1)"
else
real="$(realpath .)"
fi
path=""
nvim_path=""
if [ -d "$real" ]; then
if [ -z "$1" ]; then
nvim_path=""
else
nvim_path=" ."
fi
path="$real"
else
nvim_path=" $(basename $real)"
path="$(dirname $real)"
fi
echo $real
echo $path
echo $nvim_path
# ================================
# Pre-checks
# ================================
if [ ! -d "$resource_dir" ]; then
mkdir "$resource_dir"
fi
# ================================
# Image and Container Checks
# ================================
if [ -z "$($CONTAINERIZER images -q $image_name)" ]; then
echo "Image does not exist. Building now..."
CMD="$builder build -t $image_name $resource_dir"
$CMD
fi
if [ ! "$($CONTAINERIZER ps -aq -f name=$main_container_name)" ]; then
echo "Container does not exist. Creating now..."
CMD="$CONTAINERIZER run -d \
--name $main_container_name \
-v $resource_dir/.config/nvim:/root/.config/nvim \
-v nvim_state:/root/.local/state/nvim \
-v nvim_share:/root/.local/share/nvim \
-v $resource_dir/clipboard.txt:/root/clipboard-sync/clipboard.txt \
$image_name"
$CMD
fi
# ================================
# Clipboard Sync
# ================================
(file_path="$resource_dir/clipboard.txt"
while true; do
content=$(cat "$file_path" | sed -e '$ d')
if [ "$content" != "" ]; then
if [ -n "$(command -v pbcopy)" ]; then
pbcopy < "$file_path"
elif [ -n "$(command -v xclip)" ]; then
xclip -selection clipboard < "$file_path"
elif [ -n "$(command -v xsel)" ]; then
xsel --clipboard < "$file_path"
else
break
fi
echo "" > "$file_path"
fi
sleep 1
done) &
clipboard_pid=$!
# ================================
# Run nvim
# ================================
CMD="$CONTAINERIZER run --rm -it \
--volumes-from $main_container_name \
-v $path:/mnt/volume \
--workdir /mnt/volume \
$image_name nvim$nvim_path"
$CMD
# ================================
# Cleanup
# ================================
kill $clipboard_pid