15

カスタムPowershellprompt関数の実装のさまざまな例を探しています。独自のカスタム実装がある場合は、スクリプトを投稿してください。既存のリソースへのリンクも良好です。

プロンプトが実際にどのように見えるかのスクリーンショットを投稿するためのボーナスポイント(プレビュー)。

4

6 に答える 6

11

これは、jaykulのプロンプトの修正バージョンです。利点はそれです

-現在の履歴IDがあるので、履歴から以前のアイテムを非常に簡単に呼び出すことができます(IDを知っています)-ちょっとしたリマインダーです-プロンプトにタスクを追加して、忘れないようにします(ショットを参照)

function prompt {
  $err = !$?
  $origOfs = $ofs;
  $ofs = "|"
  $toPrompt = "$($global:__PromptVars)"
  $ofs = $origOfs;
  if ($toPrompt.Length -gt 0) { 
    Write-Host "$($toPrompt) >" -ForegroundColor Green -NoNewline }

  $host.UI.RawUI.WindowTitle = "PS1 > " + $(get-location)

  # store the current color, and change the color of the prompt text
  $script:fg = $Host.UI.RawUI.ForegroundColor
  # If there's an error, set the prompt foreground to "Red"
  if($err) { $Host.UI.RawUI.ForegroundColor = 'Red' }
  else { $Host.UI.RawUI.ForegroundColor = 'Yellow' }

  # Make sure that Windows and .Net know where we are at all times
  [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath

  # Determine what nesting level we are at (if any)
  $Nesting = "$([char]0xB7)" * $NestedPromptLevel

  # Generate PUSHD(push-location) Stack level string
  $Stack = "+" * (Get-Location -Stack).count

  # Put the ID of the command in, so we can get/invoke-history easier
  # eg: "r 4" will re-run the command that has [4]: in the prompt
  $nextCommandId = (Get-History -count 1).Id + 1
  # Output prompt string
  # Notice: no angle brackets, makes it easy to paste my buffer to the web
  Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine

  # Set back the color
  $Host.UI.RawUI.ForegroundColor = $script:fg

  if ($toPrompt.Length -gt 0) { 
      $host.UI.RawUI.WindowTitle = "$($toPrompt) -- " + $host.UI.RawUI.WindowTitle
  }
  " "
}
function AddTo-Prompt($str) {
  if (!$global:__PromptVars) { $global:__PromptVars = @() }
  $global:__PromptVars += $str
}
function RemoveFrom-Prompt($str) {
  if ($global:__PromptVars) {
    $global:__PromptVars = @($global:__PromptVars | ? { $_ -notlike $str })
  }
}

ショット

于 2009-08-27T06:36:09.667 に答える
11

これが私のものです:

function prompt {
   # our theme
   $cdelim = [ConsoleColor]::DarkCyan
   $chost = [ConsoleColor]::Green
   $cloc = [ConsoleColor]::Cyan

   write-host "$([char]0x0A7) " -n -f $cloc
   write-host ([net.dns]::GetHostName()) -n -f $chost
   write-host ' {' -n -f $cdelim
   write-host (shorten-path (pwd).Path) -n -f $cloc
   write-host '}' -n -f $cdelim
   return ' '
}

このヘルパー関数を使用します。

function shorten-path([string] $path) {
   $loc = $path.Replace($HOME, '~')
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}
于 2009-08-27T13:31:41.120 に答える
10

ここに私のプロンプト機能があります

function prompt() {
    if ( Test-Wow64 ) {
        write-host -NoNewLine "Wow64 "
    }
    if ( Test-Admin ) { 
        write-host -NoNewLine -f red "Admin "
    }
    write-host -NoNewLine -ForegroundColor Green $(get-location)
    foreach ( $entry in (get-location -stack)) {
        write-host -NoNewLine -ForegroundColor Red '+';
    }
    write-host -NoNewLine -ForegroundColor Green '>'
    ' '
}
于 2009-08-27T01:46:15.003 に答える
4

私はよくposhを計算として使用するので、$ans変数を設定します。 https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=386493

PS> 100100 PS> $ ans * 9900 PS > $ ans * $ ans
810000



于 2009-08-29T16:54:29.720 に答える
1

これが私のものです。各コマンドに履歴 ID があるだけなので、コマンドの ID を簡単に識別できます。また、ウィンドウタイトルを使用して、プロンプト自体に表示するのではなく、現在の作業ディレクトリを提供します。

106 >  cat function:\prompt

    $history = @(get-history)
    if($history.Count -gt 0)
{
        $lastItem = $history[$history.Count - 1]
        $lastId = $lastItem.Id
    }

    $nextCommand = $lastId + 1
    $Host.ui.rawui.windowtitle = "PS " + $(get-location)
    $myPrompt = "$nextCommand > "
    if ($NestedPromptLevel -gt 0) {$arrows = ">"*$NestedPromptLevel; $myPrompt = "PS-nested $arrows"}
    Write-Host ($myPrompt) -nonewline
    return " "

多くの人がカスタム プロンプトで処理することを忘れているのは、ネストされたプロンプトです。$nestedPromptLevel を確認し、ネストされたレベルごとに矢印を追加していることに注意してください。

アンディ

于 2009-09-15T13:48:39.677 に答える
0

再入力する傾向がある

function prompt { "PS> " }

例を準備するたびに、コピーして誰かに貼り付けることができます。

そして、現在のディレクトリ(そこにつながるパスなし)または(数値の場合)次の上位レベルを使用して、ドライブと場所の有用な概算を表示する適切なプロンプト関数を作成する予定です。しかし、それはおそらく私自身のファイルシステムにかなり固有のものです。そして、私は実際にそれを行うためにデフォルトのプロンプトに悩まされることはありませんでした:-)

于 2009-08-27T05:36:40.660 に答える