Menu w PowerShell

Menu w PowerShell po którym można poruszać się strzałkami.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function ShowMenu([array]$Menu, [int]$Default)
{
  $minY = [Console]::CursorTop
  $y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0)
  do {
    [Console]::CursorTop = $minY
    [Console]::CursorLeft = 0
    $i = 0
    foreach ($item in $Menu) {
      $colors = @{
         BackgroundColor = if ($i -ne $y) { [Console]::BackgroundColor } else { 'Cyan' }
         ForegroundColor = if ($i -ne $y) { [Console]::ForegroundColor } else {' Blue' }
      }
      Write-Host (' {0}. {1} ' -f ($i+1), $item) @colors
      $i++
    }
    $k = [Console]::ReadKey()
    switch ($k.Key) {
      'UpArrow' { if ($y -gt 0) { $y-- } }
      'DownArrow' { if ($y -lt ($menu.Count - 1)) { $y++ } }
      'Enter' { return $Menu[$y] }
    }
  } while ($k.Key -notin ([ConsoleKey]::Escape, [ConsoleKey]::Enter))
}

$Menu = 'test1','text2','menu3','result4'

ShowMenu $menu 2

I inna wersja zgapiona z Sophiascript:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	<#
		.SYNOPSIS
		The "Show menu" function with the up/down arrow keys and enter key to make a selection

		.PARAMETER Menu
		Array of items to choose from

		.PARAMETER Default
		Default selected item in array

		.PARAMETER AddSkip
		Add localized extracted "Skip" string from shell32.dll

		.EXAMPLE
		Show-Menu -Menu $Items -Default 1

		.LINK
		https://qna.habr.com/answer?answer_id=1522379
	#>
	function script:Show-Menu
	{
		[CmdletBinding()]
		param
		(
			[Parameter(Mandatory = $true)]
			[array]
			$Menu,

			[Parameter(Mandatory = $true)]
			[int]
			$Default,

			[Parameter(Mandatory = $false)]
			[switch]
			$AddSkip
		)

		Write-Information -MessageData "" -InformationAction Continue

		# Add "Please use the arrow keys 🠕 and 🠗 on your keyboard to select your answer" to menu
		$Menu += $Localization.KeyboardArrows -f [System.Char]::ConvertFromUtf32(0x1F815), [System.Char]::ConvertFromUtf32(0x1F817)

		if ($AddSkip)
		{
			# Extract the localized "Skip" string from shell32.dll
			$Menu += [WinAPI.GetStr]::GetString(16956)
		}

		# https://github.com/microsoft/terminal/issues/14992
		[System.Console]::BufferHeight += $Menu.Count
		$minY = [Console]::CursorTop
		$y = [Math]::Max([Math]::Min($Default, $Menu.Count), 0)

		do
		{
			[Console]::CursorTop = $minY
			[Console]::CursorLeft = 0
			$i = 0

			foreach ($item in $Menu)
			{
				if ($i -ne $y)
				{
					Write-Information -MessageData ('  {1}  ' -f ($i+1), $item) -InformationAction Continue
				}
				else
				{
					Write-Information -MessageData ('[ {1} ]' -f ($i+1), $item) -InformationAction Continue
				}

				$i++
			}

			$k = [Console]::ReadKey()
			switch ($k.Key)
			{
				"UpArrow"
				{
					if ($y -gt 0)
					{
						$y--
					}
				}
				"DownArrow"
				{
					if ($y -lt ($Menu.Count - 1))
					{
						$y++
					}
				}
				"Enter"
				{
					return $Menu[$y]
				}
			}
		}
		while ($k.Key -notin ([ConsoleKey]::Escape, [ConsoleKey]::Enter))
	}
comments powered by Disqus
Zbudowano z Hugo
Motyw Stack zaprojektowany przez Jimmy