Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Created May 9, 2023 22:25
Show Gist options
  • Select an option

  • Save CodeByAidan/8adf6920dc8eb8d0769b6fa8e97af722 to your computer and use it in GitHub Desktop.

Select an option

Save CodeByAidan/8adf6920dc8eb8d0769b6fa8e97af722 to your computer and use it in GitHub Desktop.
Simple calendar program that asks for user's input for year.
Clear-Host && Get-ChildItem | Out-Null
[int]$Year = Read-Host "Enter a year"
Clear-Host && Get-ChildItem | Out-Null
$COL_WIDTH = 21
$COLS = 3
$MONTH_COUNT = 12
$MONTH_LINES = 9
Function CenterStr([string]$s, [int]$lineSize) {
$padSize = [int](($lineSize - $s.Length) / 2)
return (($s.PadLeft($s.Length + $padSize, ' ')).PadRight($lineSize, ' '))
}
Function MonthLines([int]$month) {
$dt = [System.DateTime]::new($Year, $month, 1)
$line = CenterStr $dt.ToString("MMMM") $COL_WIDTH
$line += 'Su Mo Tu We Th Fr Sa'
$line += $(' ' * $dt.DayOfWeek.value__)
$line += (-join ($(1..$($dt.AddMonths(1).AddDays(-1).Day)) | ForEach-Object{ $("" + $_).PadLeft(3) }))
$line = $line.PadRight($MONTH_LINES * $COL_WIDTH)
return New-Object –TypeName PSObject –Prop(@{
'Lines'=(0..($MONTH_LINES - 1)) | ForEach-Object{ $_ * $COL_WIDTH } | ForEach-Object{ -join $line[$_..($_ + $COL_WIDTH - 1)] }
'Dt'=$dt
})
}
Write-Output (CenterStr $Year ($COL_WIDTH * $COLS + 4))
for($i = 0; $i -lt ($MONTH_COUNT / $COLS); $i++) {
$fm = $i * $COLS
$monthNums = $fm..($fm + $COLS - 1) | ForEach-Object{ $_ + 1 }
$months = $monthNums | ForEach-Object{ MonthLines $_ }
for($j = 0; $j -lt $MONTH_LINES; $j++) {
$ml = $j
Write-Output $(-join ($(0..($COLS - 1)) | ForEach-Object{ $(if ($_ -eq 0) { '' } else {' '}) + $months[$_].Lines[$ml] }))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment