Search code examples
windowswindows-terminal

Run Clink in Windows Terminal Preview


Is there any possibility to run Clink within Windows Terminal Preview?


I tried to add this entry in Settings:

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

but it opens Clink in a new window.

I think clink.bat must be somehow modified because it launches Clink with:

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

Solution

  • Looking into the clink.bat file

    Let us look at the clink.bat file:

    :: 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
    
    

    This is quite well commented so we can see the following chronological structure:

    1. Move to the %userprofile% folder
    2. Set the clink_profile_arg to the value of PROFILE_DIR if the call is in the form clink.bat --profile PROFILE_DIR
    3. If there are no arguments jump to the launch code and then end (by jumping to the end of the file)
    4. Select the right .exe based on the system's architecture (technically the process architecture that calls this function: Possible values of %PROCESSOR_ARCHITECTURE%)
    5. The launch "definition" (technically labels)

    You have correctly identified that the launch labelled code is what can be changed, let us look at it further:

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

    So this runs the start command with some arguments, including the string "Clink" and what appears to be a cmd.exe with its own command line arguments. %~dpnx0 being: drive,path,name,extension, 0th argument (see, syntax-args), and %clink_profile_arg% the variable defined earlier.

    Looking at start :

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

    The bold emphasis is my own, but we instantly now can see why you observed the behaviour you described.

    We have several options to consider now.

    Option 1 - New clink_terminal.bat based on clink.bat

    Although we could edit clink.bat the better option would be to make a separate file that we use just for the Terminal.

    We can simply change :launch to:

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

    and then use your commandline: with clink_terminal.bat instead.

    Option 2 - directly use clink with its command line arguments

    Hopefully through you have seen that you can effectively replace calling the .bat and simply call clink directly with its arguments.

    Here assuming you are using a x64 machine:

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

    Set a GUID !!!

    All the profiles in Terminal have a GUID, you can easily generate one yourself.

    Open a PowerShell window and run new-guid

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