Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Операционные системы » Microsoft Windows » Сценарии для Windows

Модерирует : KLASS, IFkO

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

YuS 2



Silver Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору

Код:
class Menu {
#region Params
    [int] $Left            = 4
    [int] $Top            = 2
    [int] $Width        = 30
    [int] $Height        = 200
    [int] $Current        = 0
    [int] $ViewStart    = 0
 
    [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string[]] $Items
    $Selected = [System.Collections.Generic.List[int]]::new()
 
    #$Background         = [ConsoleColor]::Black
    $Foreground         = [ConsoleColor]::Gray
    #$CurrentBackground  = [ConsoleColor]::DarkGray
    $CurrentForeground  = [ConsoleColor]::White
    #$SelectedBackground = [ConsoleColor]::DarkBlue
    $SelectedForeground = [ConsoleColor]::Yellow
 
    hidden [bool] $CanContinue = $true
    hidden [bool] $BackupCursorVisible = [Console]::CursorVisible
#endregion
 
    Menu() {}
    Menu ([string[]] $items) { $this.Items = $items }
 
    [System.Collections.Generic.List[int]] Run() {
        [Console]::CursorVisible = $false
 
        while ($this.CanContinue) {
            $this.Draw()
            [ConsoleKeyInfo] $keyInfo = [Console]::ReadKey($true)
 
            switch ($keyInfo.Key) {
                ([ConsoleKey]::UpArrow)   { $this.MoveUp();   break }
                ([ConsoleKey]::DownArrow) { $this.MoveDown(); break }
                ([ConsoleKey]::Spacebar)  { $this.Select();   break }
                ([ConsoleKey]::Enter)     { $this.Finish();   break }
                ([ConsoleKey]::Escape)    { $this.Cancel();   break }
            }
        }
 
        [Console]::ResetColor()
        [Console]::CursorVisible = $this.BackupCursorVisible
        return $this.Selected
    }
 
    [void] Cancel() {
        $this.Selected.Clear();
        $this.CanContinue = $false
    }
 
    [void] Finish() {
        if ($this.Selected.Count -gt 0) {
            $this.CanContinue = $false
        }
    }
 
    [void] Select() {
        if ($this.Selected -Contains $this.Current) {
            $this.Selected.Remove($this.Current)
        } else {
            $this.Selected.Add($this.Current)
        }
    }
 
    [void] MoveDown() {
        if ($this.Current -lt ($this.Items.Count - 1)) {
            $this.Current++
        }
 
        if ($this.Current -ge $this.Height -and $this.ViewStart -lt ($this.Items.Count - $this.Height)) {
            $this.ViewStart++
        }
    }
 
    [void] MoveUp() {
        if ($this.Current -gt 0) {
            $this.Current--
        }
 
        if ($this.Current -lt ($this.Items.Length - $this.Height) -and $this.ViewStart -gt 0) {
            $this.ViewStart--
        }
    }
 
    [void] Draw() {
        if ($this.Items.Count -gt $this.Height) {
            $h = $this.Height
        } else {
            $h = $this.Items.Count
        }
        for ([int] $i = 0; $i -lt $h; $i++) {
            [Console]::SetCursorPosition($this.Left, $this.Top + $i)
            #[Console]::BackgroundColor = $this.Background
            [Console]::ForegroundColor = $this.Foreground
            [string] $item = $this.Items[$this.ViewStart + $i]
            [string] $checkbox = "[ ] "
            [string] $cursor = "  "
            [string] $spaces = ""
            [int] $lineLength = $item.Length + $checkbox.Length + $cursor.Length
            [int] $spacesCount = $this.Width - $lineLength
 
            if ($this.Selected -Contains ($this.ViewStart + $i)) {
                #[Console]::BackgroundColor = $this.SelectedBackground
                [Console]::ForegroundColor = $this.SelectedForeground
                $checkbox = "
  • "
                }
     
                if ($this.ViewStart + $i -eq $this.Current) {
                    #[Console]::BackgroundColor = $this.CurrentBackground
                    [Console]::ForegroundColor = $this.CurrentForeground
                    $cursor = "> "
                }
     
                if ($lineLength -lt $this.Width) {
                    $spaces = [string]::new(' ', $spacesCount)
                }
     
                if ($lineLength -gt $this.Width) {
                    [int] $w = $this.Width - $checkbox.Length - $cursor.Length
                    $item = $item.Substring(0, $w)
                }
     
                [Console]::Writeline($checkbox + $cursor + $item + $spaces)
            }
        }
    }
     
    function Menu-Demo {
    cls
     
    [Console]::WriteLine("Демка, только вывод, что Вы выбрали (мультивыбор). Текущий каталог: $PWD")
    [Console]::WriteLine("Стрелки вверх/вниз - перемещение; Spacebar - выбор; Enter - завершить выбор; ESC - отмена")
     
    $Items = Get-ChildItem $PWD
     
    $menu = [Menu]@{
        Items = $Items
        Top   = 3
    }
     
    $menu.Width = ([Console]::WindowWidth - $menu.Left)
     
    if ($null -eq $menu) { throw $Error[0] }
     
    $result = $menu.Run()
     
    if ($result.Count -gt 0) {
        [Console]::Write("`nВы выбрали (индекс): ")
        [Console]::Write(($result -join "; "))
     
        [Console]::Write("`nВы выбрали: ")
        @(foreach ($i in $result) { $Items[$i] }) -join "; "
    }
     
    # cls
    pause
    }
     
    Menu-Demo


  • Всего записей: 3218 | Зарегистр. 03-02-2009 | Отправлено: 21:31 15-06-2021
    Открыть новую тему     Написать ответ в эту тему

    На первую страницук этому сообщениюк последнему сообщению

    Компьютерный форум Ru.Board » Операционные системы » Microsoft Windows » Сценарии для Windows


    Реклама на форуме Ru.Board.

    Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
    Modified by Ru.B0ard
    © Ru.B0ard 2000-2024

    BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

    Рейтинг.ru