c# - 自定义 PowerShell 提示符 - 相当于 CMD 的 $M$P$_$+$G?

标签 c# powershell command prompt customization

我已经开始“玩弄”PowerShell 并试图让它“正常运行”。

我想做的一件事是将 PROMPT 自定义为“类似于”MS-Dos 上的“$M$P$_$+$G”:

这些功能的简要概述:

字符|说明
$m 与当前驱动器盘符关联的远程名称,如果当前驱动器不是网络驱动器,则为空字符串。
$p 当前驱动器和路径
$_ 输入换行
$+ 零个或多个加号 (+) 字符,具体取决于 pushd 目录堆栈的深度,每个插入的级别一个字符
$g >(大于号)

所以最终的输出是这样的:

    \\spma1fp1\JARAVJ$ H:\temp
    ++>

我已经能够将 $M$_ 功能(以及一个漂亮的历史记录功能)添加到我的提示中,如下所示:

function prompt
{
   ## Get the history. Since the history may be either empty,
   ## a single item or an array, the @() syntax ensures
   ## that PowerShell treats it as an array
   $history = @(get-history)


   ## If there are any items in the history, find out the
   ## Id of the final one.
   ## PowerShell defaults the $lastId variable to '0' if this
   ## code doesn't execute.
   if($history.Count -gt 0)
   {
      $lastItem = $history[$history.Count - 1]
      $lastId = $lastItem.Id
   }

   ## The command that we're currently entering on the prompt
   ## will be next in the history. Because of that, we'll
   ## take the last history Id and add one to it.
   $nextCommand = $lastId + 1

   ## Get the current location
   $currentDirectory = get-location

   ## Set the Windows Title to  the current location
   $host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory

   ## And create a prompt that shows the command number,
   ## and current location
   "PS:$nextCommand $currentDirectory
>"
}

但其余的还不是我设法复制的东西......

非常感谢您一定会提供的提示!

最佳答案

看看这是否符合您的要求:

function prompt
{
   ## Get the history. Since the history may be either empty,
   ## a single item or an array, the @() syntax ensures
   ## that PowerShell treats it as an array
   $history = @(get-history)


   ## If there are any items in the history, find out the
   ## Id of the final one.
   ## PowerShell defaults the $lastId variable to '0' if this
   ## code doesn't execute.
   if($history.Count -gt 0)
   {
      $lastItem = $history[$history.Count - 1]
      $lastId = $lastItem.Id
   }

   ## The command that we're currently entering on the prompt
   ## will be next in the history. Because of that, we'll
   ## take the last history Id and add one to it.
   $nextCommand = $lastId + 1

   ## Get the current location
   $currentDirectory = get-location

   ## Set the Windows Title to  the current location
   $host.ui.RawUI.WindowTitle = "PS: " + $currentDirectory

   ##pushd info
   $pushdCount = $(get-location -stack).count
   $pushPrompt = ""
   for ($i=0; $i -lt $pushdCount; $i++)
   {
       $pushPrompt += "+"
    }

   ## And create a prompt that shows the command number,
   ## and current location
   "PS:$nextCommand $currentDirectory `n$($pushPrompt)>"
}

关于c# - 自定义 PowerShell 提示符 - 相当于 CMD 的 $M$P$_$+$G?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/157923/

相关文章:

c# - 使用 MongoDB 和 ASP.NET MVC 构建 Web 应用程序,我应该使用 C# 驱动程序还是 Javascript 驱动程序

powershell - powershell 中 ForEach-Object 的语法

python - 无法在 Windows Powershell 中打开 python.exe

node.js - 如何检查输入是 Linux shell 命令还是 Nodejs 中的普通文本消息?

python - 使用 djangos manage.py 自定义命令启动守护进程服务?

git - 有没有办法让 git status 显示特定时间的变化?

c# - 如何从 WPF 应用程序写入命令行?

c# - 为什么.Net5项目引用了Microsoft.VisualBasic?

c# - 使用 C# 将视频流式传输到基于 RTMP 的媒体服务器 (Red5)

.net - PowerShell 3 : Every Command Execution Results In "The type initializer [...] threw an exception" Error