【问题标题】:How to detect if running in the new Windows Terminal?如何检测是否在新的 Windows 终端中运行?
【发布时间】:2020-05-01 04:13:56
【问题描述】:

Windows 终端预览版即将推出的一项功能是全面支持表情符号:

相比:

在 Node.js 中,我如何检测我是否在由 Windows 终端包装的终端中运行,而不是在其“裸”变体中运行?是否有我可以提取的环境变量或我可以执行的同步测试?

【问题讨论】:

标签: javascript node.js windows terminal windows-terminal


【解决方案1】:

您可以检查设置为 v4 UUID 的 WT_SESSION 环境变量:https://github.com/microsoft/terminal/issues/1040

如果您正在寻找一种快速而肮脏的检查方式,这应该可行:

!!process.env.WT_SESSION

您还可以使用更精细的方法,利用is-uuidis-wslprocess.platform

import isUUID from 'is-uuid';
import isWsl from 'is-wsl';

const isWindowsTerminal = (process.platform === "win32" || isWsl) && isUUID.v4(process.env.WT_SESSION);

【讨论】:

    【解决方案2】:

    我更喜欢 https://github.com/microsoft/terminal/issues/6269 的这种方法(在 PowerShell 中):

    function IsWindowsTerminal ($childProcess) {
        if (!$childProcess) {
            return $false
        } elseif ($childProcess.ProcessName -eq 'WindowsTerminal') {
            return $true
        } else {
            return IsWindowsTerminal -childProcess $childProcess.Parent
        }
    }
    
    

    然后我在我的个人资料中使用它来打开例如哦-我的-psh

    $IsWindowsTerminal = IsWindowsTerminal -childProcess (Get-Process -Id $PID)
    if($IsWindowsTerminal) {
        oh-my-posh --init --shell pwsh --config $HOME\Documents\mytheme.omp.json | Invoke-Expression
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 2021-12-04
      • 1970-01-01
      • 2023-04-06
      • 2011-10-20
      • 1970-01-01
      • 2011-09-12
      • 2020-02-06
      • 2018-04-12
      相关资源
      最近更新 更多