-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Set-ConsoleSize.ps1
117 lines (100 loc) · 2.62 KB
/
Set-ConsoleSize.ps1
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<#
.Synopsis
Sets the current console size, interactively by default.
Author: Roman Kuzmin
.Description
The script allows resizing of the current console either interactively by
arrow keys (other keys stop resizing) or by specifying Width and/or Height.
The buffer width is always set equal to window width. The buffer height is
set equal to width if it was initially equal or not changed otherwise.
.Parameter Width
New console width. Default is 0 (the current).
.Parameter Height
New console height. Default is 0 (the current).
.Inputs
None.
.Outputs
None.
.Example
Set-ConsoleSize.ps1
Starts interactive resizing with arrow keys.
.Example
Set-ConsoleSize.ps1 80 25
Sets classic small console size.
.Example
Set-ConsoleSize.ps1 80
Sets only new width.
.Link
https://github.com/nightroman/PowerShelf
#>
param($Width = 0, $Height = 0)
$ErrorActionPreference = 0
$UI = $Host.UI.RawUI
$eq = $UI.BufferSize -eq $UI.WindowSize
function NewSize($Width, $Height)
{
New-Object System.Management.Automation.Host.Size $Width, $Height
}
function SetSize($Width, $Height)
{
# reduce width
if ($Width -lt $UI.WindowSize.Width -and $Width -gt 0) {
$UI.WindowSize = NewSize $Width $UI.WindowSize.Height
$UI.BufferSize = NewSize $Width $UI.BufferSize.Height
}
# increase width
elseif ($Width -gt $UI.WindowSize.Width) {
$UI.BufferSize = NewSize $Width $UI.BufferSize.Height
$UI.WindowSize = NewSize $Width $UI.WindowSize.Height
}
# reduce height
if ($Height -lt $UI.WindowSize.Height -and $Height -gt 0) {
$UI.WindowSize = NewSize $UI.WindowSize.Width $Height
if ($eq) {
$UI.BufferSize = $UI.WindowSize
}
}
# increase height
elseif ($Height -gt $UI.WindowSize.Height) {
if ($Height -gt $UI.BufferSize.Height) {
$UI.BufferSize = NewSize $UI.BufferSize.Width $Height
}
$UI.WindowSize = NewSize $UI.WindowSize.Width $Height
}
# sync buffer
if ($eq -and ($UI.BufferSize -ne $UI.WindowSize)) {
$UI.BufferSize = $UI.WindowSize
}
}
### Set the specified size
if ($Width -gt 0 -or $Height -gt 0) {
SetSize $Width $Height
return
}
### Interactive sizing
$title = $UI.WindowTitle
for(;;) {
$UI.WindowTitle = '{0} x {1} Arrow keys: resize; other keys: exit ...' -f $UI.WindowSize.Width, $UI.WindowSize.Height
switch($UI.ReadKey(6).VirtualKeyCode) {
37 {
SetSize ($UI.WindowSize.Width - 1) $UI.WindowSize.Height
break
}
39 {
SetSize ($UI.WindowSize.Width + 1) $UI.WindowSize.Height
break
}
38 {
SetSize $UI.WindowSize.Width ($UI.WindowSize.Height - 1)
break
}
40 {
SetSize $UI.WindowSize.Width ($UI.WindowSize.Height + 1)
break
}
default {
$UI.WindowTitle = $title
return
}
}
}