PoshCode Archive  Artifact Content

Artifact 25337ddacb2d7b075c3b64061aa54b9f10b451cf4617268df7ed20ca995add6c:


# encoding: ascii
# api: powershell
# title: Resize Console Window
# description: ##
# version: 0.1
# type: function
# author: William
# license: CC0
# x-poshcode-id: 874
# x-archived: 2013-08-28T20:58:53
# x-published: 2009-02-16T16:26:00
#
# Author   : Roman Kuzmin
# Synopsis : Resize console window/buffer using arrow keys
# # ##
# function Size($w, $h)
# {
# New-Object System.Management.Automation.Host.Size($w, $h)
# }
# function resize()
# {
# Write-Host ‘[Arrows] resize  [Esc] exit …’
# $ErrorActionPreference = ‘SilentlyContinue’
# for($ui = $Host.UI.RawUI;;) {
# $b = $ui.BufferSize
# $w = $ui.WindowSize
# switch($ui.ReadKey(6).VirtualKeyCode) {
# 37 {
# $w = Size ($w.width – 1) $w.height
# $ui.WindowSize = $w
# $ui.BufferSize = Size $w.width $b.height
# break
# }
# 39 {
# $w = Size ($w.width + 1) $w.height
# $ui.BufferSize = Size $w.width $b.height
# $ui.WindowSize = $w
# break
# }
# 38 {
# $ui.WindowSize = Size $w.width ($w.height – 1)
# break
# }
# 40 {
# $w = Size $w.width ($w.height + 1)
# if ($w.height -gt $b.height) {
# $ui.BufferSize = Size $b.width $w.height
# }
# $ui.WindowSize = $w
# break
# }
# 27 {
# return
# }
# }
# }
# }
#
##
## Author   : Roman Kuzmin
## Synopsis : Resize console window/buffer using arrow keys
##

function Size($w, $h)
{
    New-Object System.Management.Automation.Host.Size($w, $h)
}

function resize()
{
Write-Host '[Arrows] resize  [Esc] exit ...'
$ErrorActionPreference = 'SilentlyContinue'
for($ui = $Host.UI.RawUI;;) {
    $b = $ui.BufferSize
    $w = $ui.WindowSize
    switch($ui.ReadKey(6).VirtualKeyCode) {
        37 {
            $w = Size ($w.width - 1) $w.height
            $ui.WindowSize = $w
            $ui.BufferSize = Size $w.width $b.height
            break
        }
        39 {
            $w = Size ($w.width + 1) $w.height
            $ui.BufferSize = Size $w.width $b.height
            $ui.WindowSize = $w
            break
        }
        38 {
            $ui.WindowSize = Size $w.width ($w.height - 1)
            break
        }
        40 {
            $w = Size $w.width ($w.height + 1)
            if ($w.height -gt $b.height) {
                $ui.BufferSize = Size $b.width $w.height
            }
            $ui.WindowSize = $w
            break
        }
        27 {
            return
        }
    }
  }
}