この記事は2008/03/03にわんくまブログで書いたものです。
指定した年月のカレンダーを表示する の色つき版を作ってみました。
実行結果は下記の下段のほうです。
下図の上段のほうは、前回の実行結果です。
以下ソース
Get-Calendar2.ps1
#=============================================================================== # Get-Calendar2: 指定した年月のカレンダーを表示する(色つき版) # # パラメータ: # $Year: 年(省略した場合は現在の年) # $Month: 月(省略した場合は現在の月) # # 使用例1(年月指定) # PS > Get-Calendar2 2008 2 # # 使用例2(年月省略) # PS > Get-Calendar # # copyright HIRO's.NET(http://hiros-dot.net//) #=============================================================================== function global:Get-Calendar2 { Param ([int]$Year = $(Get-Date).Year, [int]$Month = $(Get-Date).Month) #表示用年月作成 $DispMonth = New-Object DateTime($Year, $Month, 1) $strDispMonth = ($DispMonth.ToString("MMMM",[System.Globalization.CultureInfo]'en-US') + " " + $Year) #横幅35文字の中間に表示されるようにする Write-Host "" Write-Host $strDispMonth.PadLeft([int](35 - (35 - $strDispMonth.Length) / 2)) $firstSunday = Get-FirstSunday $Year $Month $lastDay = Get-LastDay $Year $Month Write-Host "Sun." -foregroundcolor Red -noNewline Write-Host " Mon. Tue. Wed. Thu. Fri. " -noNewline Write-Host "Sat." -foregroundcolor Cyan # 第1日曜日が"1日"以外の第1週を表示 if ( $firstSunday -ne 1 ) { for ( $i = 1; $i -lt $firstSunday - 1; $i++ ) { $week += $i.ToString(" # ") } $spc = " " $spc *= [int](7 - ( $firstSunday - 1 )) Write-Host ($spc + $week) -noNewline #土曜日をCyanにする Write-Host ($firstSunday - 1).ToString(" # ") -foregroundcolor Cyan } #中間週を表示 $loopWeek = [Math]::floor(($LastDay - $firstSunday) / 7) $Day = $firstSunday for ( $weekCnt = 0; $weekCnt -lt $loopWeek; $weekCnt++ ) { for ( $iDay = 0; $iDay -lt 7; $iDay++ ) { if ( $Day -lt 10 ) { $strDay = $Day.ToString(" # ") } else { $strDay = $Day.ToString(" ## ") } #日曜日はRed, 土曜日はCyanにする if ($iDay -eq 0 ) { Write-Host $strDay -foregroundcolor Red -noNewline } elseif ($iDay -eq 6 ) { Write-Host $strDay -foregroundcolor Cyan} else { Write-Host $strDay -noNewline } $Day += 1 } #Write-Host ($week) } #最終週を表示 if ( $Day -le $LastDay ) { $week = "" $LastSunday = $Day $Day += 1 for ( $iDay = $Day; $iDay -le $LastDay - 1; $iDay++ ) { $week += $Day.ToString(" ## ") $Day += 1 } #最終週の日曜日の色をRedにする Write-Host $LastSunday.ToString(" ## ") -foregroundcolor Red -noNewline Write-Host $week -noNewline #最終日が土曜日の場合は色をCyanにする if ($LastDay - $LastSunday -eq 6) { Write-Host $LastDay.ToString(" ## ") -foregroundcolor Cyan } else { Write-Host $LastDay.ToString(" ## ") } } }
ポイントは
Write-Hostコマンドレットで色を付けているということです。
前景色(文字色)はパラメータ -foregroundcolor で指定することができます。
指定可能な色は
です。
あとは むたぐちさんに Write-Hostコマンドレットは、-noNewlineパラメータを使用すれば改行しないで文字を出力できることを教えていただきましたので、それを使用しました。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail