【问题标题】:Run Clink in Windows Terminal Preview在 Windows 终端预览中运行 Clink
【发布时间】:2020-05-14 16:24:44
【问题描述】:

有没有可能在Windows Terminal Preview 中运行Clink


我尝试在设置中添加此条目:

        {
            "hidden": false,
            "name": "Clink",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "commandline": "\"C:\\Program Files (x86)\\clink\\0.4.9\\clink.bat\" startmenu --profile ~\\clink"
        }

但它会在新窗口中打开 Clink。

我认为clink.bat 必须以某种方式进行修改,因为它会启动 Clink:

start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"

【问题讨论】:

    标签: windows windows-terminal


    【解决方案1】:

    查看clink.bat 文件

    让我们看看clink.bat文件:

    :: Copyright (c) 2012 Martin Ridgers
    :: License: http://opensource.org/licenses/MIT
    
    @echo off
    
    :: Mimic cmd.exe's behaviour when starting from the start menu.
    if /i "%1"=="startmenu" (
        cd /d "%userprofile%"
        shift /1
    )
    
    :: Check for the --profile option.
    if /i "%1"=="--profile" (
        set clink_profile_arg=--profile "%~2"
        shift /1
        shift /1
    )
    
    :: If the .bat is run without any arguments, then start a cmd.exe instance.
    if "%1"=="" (
        call :launch
        goto :end
    )
    
    :: Pass through to appropriate loader.
    if /i "%processor_architecture%"=="x86" (
            "%~dp0\clink_x86.exe" %*
    ) else if /i "%processor_architecture%"=="amd64" (
        if defined processor_architew6432 (
            "%~dp0\clink_x86.exe" %*
        ) else (
            "%~dp0\clink_x64.exe" %*
        )
    )
    
    :end
    set clink_profile_arg=
    goto :eof
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :launch
    start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"
    exit /b 0
    
    

    这是很好的注释,所以我们可以看到以下时间结构:

    1. 移至%userprofile% 文件夹
    2. 如果调用格式为clink.bat --profile PROFILE_DIR,则将clink_profile_arg 设置为PROFILE_DIR 的值
    3. 如果没有参数跳转到launch代码然后结束(通过跳转到文件末尾)
    4. 根据系统架构选择合适的.exe(技术上调用该函数的进程架构:Possible values of %PROCESSOR_ARCHITECTURE%
    5. launch“定义”(技术上是标签

    您已经正确识别出launch标记的代码是可以更改的,让我们进一步看看:

    start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"
    

    因此,这会运行带有一些参数的start 命令,包括字符串“Clink”以及带有自己命令行参数的cmd.exe%~dpnx0being: drive,path,name,extension,0 第一个参数(参见syntax-args)和%clink_profile_arg% 之前定义的变量。

    看着start

    启动单独的命令提示符窗口以运行指定的程序或命令。

    粗体强调是我自己的,但我们现在可以立即看到您为什么会观察到您所描述的行为。

    我们现在有几个选择要考虑。

    选项 1 - 基于 clink.bat 的新 clink_terminal.bat

    虽然我们可以编辑clink.bat,但更好的选择是制作一个单独的文件,仅用于终端。

    我们可以简单地将:launch 更改为:

    cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%"
    

    然后将commandline:clink_terminal.bat 一起使用。

    选项 2 - 直接使用 clink 及其命令行参数

    希望通过您已经看到,您可以有效地替换调用 .bat 并直接使用其参数直接调用 clink

    这里假设您使用的是 x64 机器:

    commandline: "cmd.exe /s /k "PATH_TO_CLINK\\clink_x64.exe inject --profile PROFILE_DIR""
    

    设置 GUID !!!

    终端中的所有配置文件都有一个 GUID,您可以轻松地自己生成一个。

    打开一个 PowerShell 窗口并运行 new-guid

    PS C:\ANYWHERE> New-Guid
    
    Guid
    ----
    c97d08e9-03fc-491f-bbd7-9a12b9d6e191
    

    【讨论】:

    • 我敢打赌,这个 GUID 将成为 Windows 终端 Clink 集成的事实标准!
    • 浪费了设计一个不那么独特但非常怪异的“GUID”并将其插入此处的机会?
    【解决方案2】:

    根据 Adehad 的回答,首先生成新的 GUID:打开 PowerShell 窗口并运行 New-Guid 命令:

    PS C:\ANYWHERE> New-Guid
    Guid
    ----
    ed8f4d54-a8db-4628-ab4f-317cc6469b07 
    

    然后在 Windows 终端的设置中,选择打开 JSON 文件(左下方菜单的最后一个选项)。它将从 C:\Users\your_user_name\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json 等路径打开文件。找到“配置文件”部分,并在“列表”中添加类似这样的内容(在此处使用您新生成的 GUID):

            {
                "commandline": "cmd.exe /s /k \"C:\\my_programs\\clink_0.4.9\\clink_x64.exe inject --profile PROFILE_DIR\"",
                "guid": "{ed8f4d54-a8db-4628-ab4f-317cc6469b07}",
                "hidden": false,
                "name": "clink"
            }
    

    保存文件,如果有错误,Windows 终端会在 settings.json 文件中显示错误消息和发生错误的位置。通常它与未正确转义的引号或斜杠有关。保存更改,重新启动 Windows 终端,然后在选项中查看新项目,clink

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2020-02-24
      • 2013-01-02
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多