.omp.json 中的命令段 Oh-my-posh 主题

问题描述 投票:0回答:1

我一直在根据自己的口味修改主题 kali,来自 oh my posh。

我遇到了以下问题。

我有这个片段

{
  "type": "prompt",
  "alignment": "left",
  "segments": [
    {
      "type": "command",
      "style": "plain",
      "foreground": "#ffffff",
      "properties": {
        "shell": "cmd",
        "command": "cmd /C D:\\repos\\zsh-pwsh-wt\\windows-pws-posh\\center.bat"
      },
      "template": "{{ .Output }}"
    }
  ]
},

该段的要点是打印一条与主提示上方的窗口一样宽的线。由于与 pwsh 的兼容性问题,我通过 cmd 进行操作。

文件

center.bat
可以完美地工作,按其应有的方式打印该行。 问题是,当我调用该脚本文件时,它打印的行比窗口宽度短。

以下是示例:

  • 直接运行脚本: works

  • 添加段/块: doesn't work

此外,这个示例场景:

  • 当我单独运行
    center.bat
    时,宽度为 172。
  • 当从
    omp.json
    运行时,它是120。

enter image description here

再次检查,当它从模板运行时,无论窗口大小如何,它总是返回 120。

  • 模板:
> {
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [

    {
      "type": "prompt",
      "alignment": "right",
      "segments": [
        {
          "type": "command",
          "style": "plain",
          "foreground": "#ffffff",
          "properties": {
            "shell": "cmd",
            "command": "cmd /C D:\\repos\\zsh-pwsh-wt\\windows-pws-posh\\center.bat"
          },
          "template": "{{ .Output }}"
        }
      ]
    },
    

    {
      "alignment": "left",
      "segments": [
        {

          "properties": { "display_host": true },
          "style": "plain",
          "template": "\n<#ffffff>\u250c\u2500\u2500(</#ffffff><#00FF00>{{ .UserName }}</#00FF00>💀⌛<#ffffff><#ffffff>\u2500▶ </#ffffff><#ff0000>{{ .HostName }}</#ff0000><#ffffff><#ffffff>)</#ffffff></ffffff>",
          "type": "session"
        },
        {
          "foreground": "yellow",
          "properties": { "fetch_version": false, "fetch_virtual_env": true },
          "style": "plain",
          "template": "<#2fff00>[\ue235</#2fff00> {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }}{{ end }}{{ .Full }}{{ end }}<{{ if .Root }}#ffffff{{ else }}#00FF00{{ end }}>]</>",
          "type": "python"
        },

        {
          "properties": {
            "folder_separator_icon": "/",
            "style": "full"
          },
          "style": "plain",
          "template": "<#ffffff>──[</#ffffff><#00FF00>{{ .Path }}</#00FF00>",
          "type": "path"
        },
        {
          "type": "text",
          "template": "<#ffffff>]</#ffffff>"
        },
        {
          "type": "text",

          "template": "<#ffffff><d></d></#ffffff>"
        }
      ],
      "type": "prompt"
    },

    
    {
      "alignment": "right",
      "segments": [
        {
          "type": "git",
          "style": "plain",
          "properties": {
            "always_enabled": true,
            "branch_ahead_icon": "",
            "branch_behind_icon": "",
            "branch_gone_icon": "",
            "branch_icon": "",
            "branch_identical_icon": "",
            "cherry_pick_icon": "",
            "commit_icon": "",
            "fetch_status": true,
            "merge_icon": "",
            "rebase_icon": "",
            "revert_icon": "",
            "tag_icon": ""
          },
          "template": "<#2fff00>on─►</#2fff00><#ffffff> {{ .HEAD }}─►</#FF0000> {{ .Staging.String }}{{ if .Working.Changed }}<#FF0000> \u25cf─►</#FF0000> <#00FF00>{{ .Working.String }}{{ end }}</#00FF00>\n"
        }
        
      ],
      "type": "prompt"

    },
    

    {
      "alignment": "left",
      "segments": [
        {
          "type": "text",
          "template": "<#ffffff>\u2502</#ffffff> "
        }
        
      ],
      "type": "prompt"

    },



    {
      "alignment": "right",
      "newline": false,
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "template": "<#ff0000>[</#ff0000>"
        },
        
        {
          "type": "time",
          "style": "plain",
          "properties": {
            "time_format": "<#ffffff>Monday, 02-Jan-06 15:04:05</#ffffff>"
          }
        },
        {
          "type": "text",
          "style": "plain",
          "template": "<#ff0000>]</#ff0000>"
        }
      ],
      "type": "prompt"
    }



    ,
    {
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "style": "plain",
          "template": "<#ffffff>\u2514\u2500▶<r> λ:</r> </#ffffff>",
          "type": "text"
        }
      ],
      "type": "prompt"
    }
  ],
  "version": 3
}

脚本:

@echo off
setlocal enabledelayedexpansion
set width=0

:: Get the current window width (number of columns)
for /f "tokens=2 delims=:" %%I in ('mode con ^| findstr "Columns"') do set width=%%I

:: Set the initial TEXT value to be repeated
set TEXT=─

:: Initialize an empty string to hold the repeated TEXT
set result=

:: Loop through the window width and append the TEXT to the result string
for /L %%i in (1,1,!width!) do (
    set result=!result!!TEXT!
)

:: Now echo the final string in one go
echo !result!
endlocal

编辑: 我已经尝试了很多,我尝试将宽度作为参数传递给 .bat,它本身可以工作,但在模板中不起作用,至少在我传递参数时不起作用。 因此,我决定仅通过 $Profile 来完成此操作,但仅在提示开始时才起作用:

oh-my-posh init pwsh --config 'D:\repos\zsh-pwsh-wt\windows-pws-posh\kalimod.omp.json' | Invoke-Expression
function prompt {
    $width = [console]::WindowWidth
    $result = "-" * $width
    [System.Environment]::SetEnvironmentVariable("PROMPT_LINE", $result, [System.EnvironmentVariableTarget]::Process)

    write-Host($result)
oh-my-posh init pwsh --config 'D:\repos\zsh-pwsh-wt\windows-pws-posh\kalimod.omp.json' | Invoke-Expression

    # Output the custom prompt (optional to display additional info)
    #return $line
}

# This line ensures the prompt is re-executed after each command is run
$ExecutionContext.InvokeCommand.CommandNotFoundAction = {
    # Force a prompt redraw after each command
    prompt

}

batch-file cmd powershell-core windows-terminal oh-my-posh
1个回答
0
投票

已解决。 感谢@Compo 的观察。 我放弃了 .bat 脚本方法。 最终的解决方案,我在 .ps1 配置文件中创建一个事件,该事件在每次命令(除了其本身)运行时运行,计算当前窗口宽度并将其保存在可从 omp.json 主题文件访问的全局变量中。 这是代码:

oh-my-posh init pwsh --config 'D:\repos\zsh-pwsh-wt\windows-pws-posh\kalimod.omp.json' | Invoke-Expression
$width = [console]::WindowWidth
    $result = "ˍ" * ($width-1)
    [System.Environment]::SetEnvironmentVariable("PROMPT_LINE", $result, [System.EnvironmentVariableTarget]::Process)
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -Action {
    $width = [console]::WindowWidth
    $result = "ˍ" * ($width-1)
    [System.Environment]::SetEnvironmentVariable("PROMPT_LINE", $result, [System.EnvironmentVariableTarget]::Process)
    oh-my-posh init pwsh --config 'D:\repos\zsh-pwsh-wt\windows-pws-posh\kalimod.omp.json' | Invoke-Expression
}

这是该块以及我在主题中如何称呼它:

   {
  "type": "prompt",
  "alignment": "left",
  "segments": [
    {
      "type": "command",
      "style": "plain",
      "foreground": "#ffffff",
      "properties": {
        "shell": "powershell",
        "command": "$env:PROMPT_LINE"
      }
    }
  ]
},
© www.soinside.com 2019 - 2024. All rights reserved.