mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-22 16:03:22 +01:00
Update write-big.ps1 and write-calendar.ps1
This commit is contained in:
parent
dfeb7ae699
commit
71f8fdba2f
@ -402,9 +402,8 @@ function BigChar { param([string]$Char, [int]$Row)
|
||||
}
|
||||
|
||||
try {
|
||||
if ($Text -eq "" ) {
|
||||
[String]$Text = read-host "Enter text to write"
|
||||
}
|
||||
if ($Text -eq "" ) { [String]$Text = read-host "Enter text to write" }
|
||||
|
||||
[char[]]$ArrayOfChars = $Text.ToUpper()
|
||||
write-output ""
|
||||
for ($Row = 1; $Row -lt 5; $Row++) {
|
||||
|
@ -1,61 +1,31 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Writes out calendar elements, either a single month or an entire year depending on the inputs.
|
||||
write-calendar.ps1 [<Month>] [<Year>]
|
||||
.DESCRIPTION
|
||||
Writes out calendar elements, either a single month or an entire year.
|
||||
.PARAMETER Month
|
||||
If specified, will limit output to a single month with this numeral value.
|
||||
.PARAMETER Year
|
||||
If specified, will output an entire year.
|
||||
.PARAMETER $DateColors
|
||||
A [ConsoleColor] array of two elements specifying the foreground color and background color for each day printed
|
||||
.PARAMETER $TodayColors
|
||||
A [ConsoleColor] array of two elements specifying the foreground color and background color for today's date
|
||||
.PARAMETER $HolidayColors
|
||||
A [ConsoleColor] array of two elements specifying the foreground color and background color for holidays printed
|
||||
.NOTES
|
||||
This script has some functionality which many would consider weird or inconsistent. Specifically, if a month is specifed and a year is not, then the output is typically the calendar for the input month and the current year. However, if the specified month is greater than 12, then it's treated as a year and the whole year is outputted.
|
||||
The reason for this is to emulate the *NIX cal function, which behaves similarly. That is, cal outputs the current month, cal 2012 outpus the calendar for 2012 and cal 05 2012 outputs the calendar for May 2012.
|
||||
That is pretty much how Write-Calendar works with the exception that Write-Calendar 05 will write out the calendar for May of the current year whereas cal will output the calendar for the year 5.
|
||||
Since the point of this script is to emulate cal's functionality I will probably not change it to make it more consistent.
|
||||
.EXAMPLE
|
||||
Write-Calendar
|
||||
PS> write-calendar
|
||||
Outputs the current month.
|
||||
.EXAMPLE
|
||||
Write-Calendar 2013
|
||||
PS> write-calendar 2013
|
||||
Outputs the calendar for 2013.
|
||||
.EXAMPLE
|
||||
Write-Calendar 04 2011
|
||||
PS> write-calendar 04 2011
|
||||
Outputs the calendar for April, 2011.
|
||||
.EXAMPLE
|
||||
Write-Calendar 7
|
||||
Outputs the calendar for September of this year.
|
||||
PS> write-calendar 7
|
||||
Outputs the calendar for July of this year.
|
||||
.NOTES
|
||||
Author: Markus Fleschutz · License: CC0
|
||||
.LINK
|
||||
https://github.com/fleschutz/PowerShell
|
||||
#>
|
||||
|
||||
param (
|
||||
[int] $Month = (Get-Date).Month,
|
||||
[int] $Year = (Get-Date).Year,
|
||||
[ConsoleColor[]] $DateColors = @([ConsoleColor]::White, [Console]::BackgroundColor),
|
||||
[ConsoleColor[]] $TodayColors = @([ConsoleColor]::Red, [Console]::BackgroundColor),
|
||||
[ConsoleColor[]] $HolidayColors = @([ConsoleColor]::White, [ConsoleColor]::DarkCyan)
|
||||
)
|
||||
|
||||
Set-Variable -name daysLine -option Constant -value "Su Mo Tu We Th Fr Sa "
|
||||
|
||||
if ($year -lt 0) { throw "Year parameter must be greater than 0" }
|
||||
if ($month -lt 0) { throw "Month parameter must be between 1 and 12" }
|
||||
|
||||
if (($month -gt 12) -and ($year -eq (Get-Date).Year)) {
|
||||
$year = $month
|
||||
$month = 0
|
||||
}
|
||||
elseif (($month -gt 12) -and ($year -ne (Get-Date).Year)) {
|
||||
throw "Month parameter must be between 1 and 12"
|
||||
}
|
||||
|
||||
foreach ($array in @($DateColors, $TodayColors, $HolidayColors)) {
|
||||
if ($array.Length -lt 2) {
|
||||
throw "Must specify both foreground and background colors for color parameters"
|
||||
}
|
||||
}
|
||||
param([int]$Month = (Get-Date).Month, [int]$Year = (Get-Date).Year)
|
||||
|
||||
function Print-Month ($month, $year) {
|
||||
$firstDayOfMonth = Get-Date -month $month -day 1 -year $year
|
||||
@ -67,14 +37,6 @@ function Print-Month ($month, $year) {
|
||||
Write-Host $daysLine
|
||||
|
||||
for ($day = $firstDayOfMonth; $day -le $lastDayOfMonth; $day = $day.AddDays(1)) {
|
||||
$ForegroundColor = $DateColors[0]
|
||||
$BackgroundColor = $DateColors[1]
|
||||
|
||||
if ($day.date -eq (get-date).date) {
|
||||
$ForegroundColor = $TodayColors[0]
|
||||
$BackgroundColor = $TodayColors[1]
|
||||
}
|
||||
|
||||
if ($day.day -eq 1) {
|
||||
Write-Host (" " * 3 * [int](Get-Date $day -uformat %u)) -NoNewLine
|
||||
}
|
||||
@ -120,8 +82,6 @@ function Print-Year ($year) {
|
||||
$dayOfMonth = $dayCounts[$i]
|
||||
$dayCounts[$i]++
|
||||
$dayOffset = [int](Get-Date -day 1 -month ($month + $i) -year $year -uformat %u)
|
||||
$ForegroundColor = $DateColors[0]
|
||||
$BackgroundColor = $DateColors[1]
|
||||
|
||||
if ($dayOfMonth -eq 1) {
|
||||
Write-Host (" " * 3 * $dayOffSet) -NoNewLine
|
||||
@ -130,11 +90,6 @@ function Print-Year ($year) {
|
||||
if ($dayOfMonth -le (Get-Date -day 1 -month ((($i + $month) % 12) + 1) -year $year).AddDays(-1).day) {
|
||||
$currentDay = (Get-Date -day $dayOfMonth -month ((($i + $month - 1) % 12) + 1) -year $year)
|
||||
|
||||
if ($currentDay.date -eq (Get-Date).date) {
|
||||
$ForegroundColor = $TodayColors[0]
|
||||
$BackgroundColor = $TodayColors[1]
|
||||
}
|
||||
|
||||
Write-Host ((Get-Date -month ($i + $month) -day $dayOfMonth -year $year -Format dd).ToString()) -NoNewLine
|
||||
Write-Host " " -NoNewLine
|
||||
}
|
||||
@ -157,24 +112,6 @@ function Print-Year ($year) {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-Holidays ($year) {
|
||||
$holidays = @(
|
||||
Get-Date -Year $year -Month 1 -Day 1 # New Year's Day
|
||||
Find-WeekDayMultiple $year 1 "Monday" 3 # MLK Day
|
||||
Find-WeekDayMultiple $year 2 "Monday" 3 # President's Day
|
||||
Find-GoodFriday $year # Good Friday
|
||||
Find-LastWeekDay $year 5 "Monday" # Memorial Day
|
||||
Get-Date -Year $year -Month 7 -Day 4 # Fourth of July
|
||||
Find-WeekDayMultiple $year 9 "Monday" 1 # Labor Day
|
||||
Find-WeekDayMultiple $year 10 "Monday" 2 # Columbus Day
|
||||
Get-Date -Year $year -Month 11 -Day 11 # Veterans Day
|
||||
Find-WeekDayMultiple $year 11 "Thursday" 4 # Thanksgiving
|
||||
Get-Date -Year $year -Month 12 -Day 25 # Christmas
|
||||
) |% {$_.Date}
|
||||
|
||||
return $holidays
|
||||
}
|
||||
|
||||
function Find-WeekDayMultiple ($year, $month, $dayOfWeek, $multiple) {
|
||||
$result = Get-Date -Year $year -Month $month -Day 1
|
||||
$multipleCount = 0
|
||||
@ -209,28 +146,25 @@ function Find-LastWeekDay ($year, $month, $dayOfWeek) {
|
||||
return $result
|
||||
}
|
||||
|
||||
function Find-GoodFriday ($year) {
|
||||
# Taken from http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm
|
||||
$a = $year % 19
|
||||
$b = [Math]::Floor($year / 100)
|
||||
$c = $year % 100
|
||||
$d = [Math]::Floor($b / 4)
|
||||
$e = $b % 4
|
||||
$f = [Math]::Floor(($b + 8) / 25)
|
||||
$g = [Math]::Floor(($b - $f + 1) / 3)
|
||||
$h = (19 * $a + $b - $d - $g + 15) % 30
|
||||
$i = [Math]::Floor($c / 4)
|
||||
$k = $c % 4
|
||||
$L = (32 + 2 * $e + 2 * $i - $h - $k) % 7
|
||||
$m = [Math]::Floor(($a + 11 * $h + 22 * $L) / 451)
|
||||
$month = [Math]::Floor(($h + $L - 7 * $m + 114) / 31)
|
||||
$day = (($h + $L - 7 * $m + 114) % 31) + 1
|
||||
return (Get-Date -Year $year -Month $month -Day $day).AddDays(-2)
|
||||
}
|
||||
try {
|
||||
Set-Variable -name daysLine -option Constant -value "Su Mo Tu We Th Fr Sa "
|
||||
|
||||
if ($year -lt 0) { throw "Year parameter must be greater than 0" }
|
||||
if ($month -lt 0) { throw "Month parameter must be between 1 and 12" }
|
||||
|
||||
if (($month -gt 12) -and ($year -eq (Get-Date).Year)) {
|
||||
$year = $month
|
||||
$month = 0
|
||||
} elseif (($month -gt 12) -and ($year -ne (Get-Date).Year)) {
|
||||
throw "Month parameter must be between 1 and 12"
|
||||
}
|
||||
if ($month -ne 0) {
|
||||
Print-Month $month $year
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Print-Year $year
|
||||
}
|
||||
exit 0
|
||||
} catch {
|
||||
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
||||
exit 1
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user