运行 Clink 在 Windows 终端预览中

Run Clink in Windows Terminal Preview

有没有可能运行Clink within Windows Terminal Preview?


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

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

但它会在新 window 中打开 Clink。

我认为 clink.bat 必须以某种方式修改,因为它启动 Clink 时使用:

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

查看 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
  3. ,则将 clink_profile_arg 的值设为 PROFILE_DIR
  4. 如果没有参数跳转到launch代码然后结束(通过跳转到文件末尾)
  5. Select 正确.exe 基于系统的架构(技术上调用此函数的进程架构:Possible values of %PROCESSOR_ARCHITECTURE%
  6. launch"definition"(技术上标签

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

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

所以这个 运行 是带有一些参数的 start 命令,包括字符串 "Clink" 和似乎是带有自己的命令行参数的 cmd.exe%~dpnx0 是:drive,path,name,extension,0th 参数(参见,syntax-args),以及 %clink_profile_arg% 之前定义的变量。

正在查看 start :

Starts a separate Command Prompt window to run a specified program or command.

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

我们现在有几个选项可以考虑。

选项 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 window 和 运行 new-guid

PS C:\ANYWHERE> New-Guid

Guid
----
c97d08e9-03fc-491f-bbd7-9a12b9d6e191

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

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

然后在 Windows 终端中,在设置中,select 打开 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