-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
executable file
·153 lines (110 loc) · 4 KB
/
install.py
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
#!/usr/bin/env python3
import os
import sys
import stat
import shutil
import subprocess
UNSATISFIED_DEPENDENCY_ERROR = 1
def is_wsl() -> bool:
try:
with open("/proc/version", "r") as f:
return "microsoft" in f.read().lower()
except Exception:
return False
def is_windows() -> bool:
return sys.platform == "win32"
def has_alacritty() -> bool:
return shutil.which("alacritty") is not None
def has_xterm() -> bool:
return shutil.which("xterm") is not None
def common_starter(runtime: str) -> str:
return """#!/usr/bin/env bash
declare startPath="."
declare currentDir=$(pwd)
# if it is an absolute path, we do not want to use "realpath"
if [[ $1 = /* ]]; then
startPath="$1"
else
# if it is a relative path we can use realpath
if [ $# -eq 0 ]; then
startPath="$(realpath $currentDir)"
else
startPath="$(realpath $currentDir/$1)"
fi
fi
"""
def wsl_starter(runtime: str) -> str:
return f"""{common_starter(runtime)}
{runtime}/setup.sh {runtime}/init.lua $@ & > /dev/null
"""
# we have to override the $SHELL environment variable to support underline errors
def alacritty_starter(runtime: str) -> str:
return f"""{common_starter(runtime)}
alacritty -T "$startPath - NovaVim" --class nvim --config-file {runtime}/configs/alacritty.toml -e {runtime}/setup.sh {runtime}/init.lua $@ & > /dev/null
"""
def xterm_starter(runtime: str) -> str:
return f"""{common_starter(runtime)}
xterm +sb -bg black -fg white -fa "M+1Code Nerd Font Mono" -fs 10 -title "$startPath - NovaVim" -name {runtime}/configs/.Xresources -e {runtime}/setup.sh {runtime}/init.lua $@ & > /dev/null
xrdb -query | grep -q 'XTerm/*vt100/.translationsa' |> /dev/null
if [ $? != 0 ]; then xterm -e xrdb -merge ./configs/.Xresources; fi
"""
def has_font(font: str) -> bool:
try:
output = subprocess.check_output(["fc-list", font])
if output == b"":
return False
except Exception:
return False
return True
def check_required_fonts() -> None:
required_fonts = ["JetBrainsMono-Medium", "M+1CodeNerdFontMono-Medium"]
for font in required_fonts:
if not has_font(font):
unsatisfied_font_warning(font)
def unsatisfied_font_warning(required_font: str) -> None:
print(f"Warning: You do not have {required_font} installed")
print("NovaVim might not work as expected");
print("Do you wish to proceed?")
user_feedback = input("(y/n)")
if user_feedback[0].lower() == "y":
return
os.exit(UNSATISFIED_DEPENDENCY_ERROR)
def init_module(runtime: str) -> str:
return f"""
package.path = '{runtime}/?.lua;'..package.path
package.path = '{runtime}/modules/?.lua;'..package.path
require('setup')
"""
def main() -> None:
home = os.getenv("HOME")
bin_target = home + "/.local/bin/2nvim"
runtime = os.path.dirname(os.path.realpath(__file__))
starter_path = runtime + "/start.sh"
init_module_path = runtime + "/init.lua"
if os.path.exists(bin_target):
print("Removing old 2nvim install")
os.remove(bin_target)
starter_template = ""
if is_wsl():
starter_template = wsl_starter(runtime)
elif has_alacritty():
starter_template = alacritty_starter(runtime)
elif has_xterm():
starter_template = xterm_starter(runtime)
else:
print("You do not have a supported terminal emulator installed")
print("Supported terminals: Alacritty, XTerm, WSL")
os.exit(UNSATISFIED_DEPENDENCY_ERROR)
check_required_fonts()
with open(starter_path, "w") as file:
file.write(starter_template)
init_module_template = init_module(runtime)
with open(init_module_path, "w") as file:
file.write(init_module_template)
current_permissions = os.stat(starter_path).st_mode
new_permissions = current_permissions | stat.S_IXUSR
print(f"Creating symlink in {starter_path}")
os.chmod(starter_path, new_permissions)
os.symlink(starter_path, bin_target)
if __name__ == "__main__":
main()