-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ps1
105 lines (87 loc) · 3.16 KB
/
functions.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
function Get-Repeated {
param(
[object]
$Value,
[int]
$Count,
[switch]
$AsString
)
if ($AsString) {
[System.Linq.Enumerable]::Repeat($Value, $Count) -join ""
}
else {
[System.Linq.Enumerable]::Repeat($Value, $Count)
}
}
function Write-Box {
param (
[string]
$Text,
# Number of characters to pad content on each side of the box
[int]
$HorizontalPadding = 1,
# Number of lines to pad above and below the box
[int]
$VerticalPadding = 0,
# Make the box take up the full terminal screen
[switch]
$FullScreen
)
begin {
$BoxingLines = @{
TopLeft = [char]0x256D # ╭
BottomLeft = [char]0x2570 # ╰
TopRight = [char]0x256E # ╮
BottomRight = [char]0x256F # ╯
Horizontal = [char]0x2500 # ─
Vertical = [char]0x2502 # │
}
$lineFormat = "{0}{1}{2}"
$padding = $HorizontalPadding * 2
$splitString = $Text.Split("`n")
# Length of longest string + sum of horizontal padding
$contentWidth = ($splitString | Sort-Object | Select-Object -First 1).Length + $padding
$topBottomLine = Get-Repeated -Value $BoxingLines.Horizontal -Count $contentWidth -AsString
# return top line
$lineFormat -f $BoxingLines.TopLeft, $topBottomLine, $BoxingLines.TopRight
}
process {
if ($VerticalPadding -gt 0) {
$PaddedVerticalLine = $lineFormat -f $BoxingLines.Vertical, "".PadLeft($contentWidth), $BoxingLines.Vertical
}
Get-Repeated $PaddedVerticalLine $VerticalPadding
$horizontalPaddingText = "".PadLeft($HorizontalPadding)
foreach ($line in $splitString) {
$paddedLine = $lineFormat -f $horizontalPaddingText, $line, $horizontalPaddingText
$lineFormat -f $BoxingLines.Vertical, $paddedLine.PadLeft(($contentWidth -2) / 2).PadRight($contentWidth), $BoxingLines.Vertical
}
Get-Repeated $PaddedVerticalLine $VerticalPadding
}
end {
$lineFormat -f $BoxingLines.BottomLeft, $topBottomLine, $BoxingLines.BottomRight
}
}
function Write-TipAsk {
param(
[int]
$LeftPercentage = 15,
[int]
$MiddlePercentage = 20,
[int]
$RightPercentage = 25
)
$left = Write-Box -Text "$LeftPercentage%" -VerticalPadding 2 -HorizontalPadding 9
$middle = Write-Box -Text "$MiddlePercentage%" -VerticalPadding 2 -HorizontalPadding 9
$right = Write-Box -Text "$RightPercentage%" -VerticalPadding 2 -HorizontalPadding 9
$joined = @()
for ($i = 0; $i -lt $left.Count; $i++) {
$joined += "$($left[$i]) $($middle[$i]) $($right[$i])"
}
$totalLineLength = $joined[0].Length - 4
$custStr = "Custom".PadLeft($totalLineLength / 2).PadRight($totalLineLength)
$latStr = "Leave a tip?".PadLeft(($totalLineLength+12) / 2).PadRight($totalLineLength)
$joined += @(Write-Box -Text $custStr -VerticalPadding 2)
Write-Host "`n`n$latStr`n`n"
Write-Host ($joined | Out-String) -BackgroundColor Blue
}