Windows 下 alacritty 的完全便携化方案

最近在倒腾Windows下的终端模拟器,然后就发现了 alacritty,由于我经常移动工作环境,所以迫切需要便携的软件。Alacritty 倒是提供了 Windows 下的便携版,但是其配置文件却是在 %APPDATA%\alacritty\alacritty.yml里,属实不方便,于是扒了扒其源代码,找到了将配置文件改到同级目录的办法。只需要修改alacritty\src\config\mod.rsinstalled_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()
terminal·rust·python
426 views
Comments
登录后评论
Sign In
·

+1 外行表示,开个fork和release呗 ………不求最新,但求便携……… pray