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

Customizing PowerShell Prompt - Equivalent to CMD#39;s $M$P$_$+$G?(自定义 PowerShell 提示 - 相当于 CMD 的 $M$P$_$+$G?)
本文介绍了自定义 PowerShell 提示 - 相当于 CMD 的 $M$P$_$+$G?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始玩弄" PowerShell,并试图让它表现".

I've started to "play around" with PowerShell and am trying to get it to "behave".

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

One of the things I'd like to do is to customize the PROMPT to be "similar" to what "$M$P$_$+$G" do on MS-Dos:

这些功能的简要说明:

人物|说明
$m 与当前驱动器号关联的远程名称,如果当前驱动器不是网络驱动器,则为空字符串.
$p 当前驱动器和路径
$_ ENTER-LINEFEED
$+ 零个或多个加号 (+) 字符取决于 pushd 目录堆栈的深度,每个推送的级别一个字符
$g >(大于号)

Character| Description
$m The remote name associated with the current drive letter or the empty string if current drive is not a network drive.
$p Current drive and path
$_ ENTER-LINEFEED
$+ Zero or more plus sign (+) characters depending upon the depth of the pushd directory stack, one character for each level pushed
$g > (greater-than sign)

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

So the final output is something like:

    \spma1fp1JARAVJ$ H:	emp
    ++>

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

I've been able to add the $M and $_ functionality (and a nifty History feature) to my prompt as follows:

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
>"
}

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

But the rest is not yet something I've managed to duplicate....

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

Thanks a lot for the tips that will surely come!

推荐答案

看看这是不是你想要的:

See if this does what you want:

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)>"
}

这篇关于自定义 PowerShell 提示 - 相当于 CMD 的 $M$P$_$+$G?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)