最近在倒腾Windows下的终端模拟器,然后就发现了 alacritty,由于我经常移动工作环境,所以迫切需要便携的软件。Alacritty 倒是提供了 Windows 下的便携版,但是其配置文件却是在 %APPDATA%\alacritty\alacritty.yml
里,属实不方便,于是扒了扒其源代码,找到了将配置文件改到同级目录的办法。只需要修改alacritty\src\config\mod.rs
的installed_config
函数就OK了。先找到以下代码:
#[cfg(windows)]
fn installed_config() -> Option<PathBuf> {
然后把该函数改为:
#[cfg(windows)]
fn installed_config() -> Option<PathBuf> {
let fallback = env::current_dir().expect("REASON").join("alacritty.yml");
if fallback.exists() {
Some(fallback)
} else {
None
}
}
接下来cargo build --release
编译以下,最终文件在target\release\alacritty.exe
。
我还顺手写了个 python 脚本用以自动化实现上述功能,如下:
import os
import requests
import zipfile
def download():
requests.packages.urllib3.disable_warnings()
r = requests.get(
"https://github.com/alacritty/alacritty/archive/refs/tags/"
+ requests.get(
"https://api.github.com/repos/alacritty/alacritty/releases/latest",
verify=False,
).json()["tag_name"]
+ ".zip",
stream=True,
verify=False,
)
with open("./alacritty.zip", "wb") as f:
for chunk in r.iter_content(chunk_size=512):
f.write(chunk)
zip_file = zipfile.ZipFile("./alacritty.zip")
for names in zip_file.namelist():
zip_file.extract(names, "./")
zip_file.close()
os.remove("./alacritty.zip")
os.rename(
"alacritty-"
+ requests.get(
"https://api.github.com/repos/alacritty/alacritty/releases/latest",
verify=False,
).json()["tag_name"][1:],
"alacritty",
)
def rewrite():
with open("./alacritty/alacritty/src/config/mod.rs", "r") as f:
mod = f.readlines()
mod = (
mod[: mod.index("#[cfg(windows)]\n") + 1]
+ [
"fn installed_config() -> Option<PathBuf> {\n",
' let fallback = env::current_dir().expect("REASON")'+
'.join("alacritty.yml");\n',
" if fallback.exists() {\n",
" Some(fallback)\n",
" } else {\n",
" None\n",
" }\n",
"}\n",
]
+ mod[
mod[mod.index("#[cfg(windows)]\n"):].index("}\n")
+ mod.index("#[cfg(windows)]\n") + 1:
]
)
with open("./alacritty/alacritty/src/config/mod.rs", "w") as f:
for line in mod:
f.write(line)
def build():
os.system("cd alacritty & cargo build --release")
os.rename("./alacritty/target/release/alacritty.exe", "./alacritty.exe")
os.system('powershell -Command "rm -r alacritty"')
def main():
download()
rewrite()
build()
if __name__ == "__main__":
main()