Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[PS] Calendrier visuel PowerShell ...

29 réponses
Avatar
Jean
#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#=6/2006=====================================Version 0=#
#=========== "human readable" grid calendar ==========#
#
# To test :
#
# .\gadget-calendar
# °shows month calendar with current day
# highlighted (red on white by default)
# .\gadget-calendar "6/2006"|tee -file calendar.txt
# .\gadget-calendar "june 2006"|tee -file calendar.txt
# .\gadget-calendar "juin 2006"|tee -file calendar.txt
# °outputs june 2006 month grid and writes it
# to calendar.txt file
# .\gadget-calendar -fullyear 2006|tee -file calendar.txt
# °outputs full year 2006 grid (3 x 4) and writes it
# to calendar.txt file
#
# NOTE : gadget-calendar without parameters doesn't
# highlight current day if you call it directly
# after you've launched PowerShell (probably a
# console bug).
# If you recall gagdet-calendar, that works.
#
#======================================================#

param(
[string]$strdate=(get-date),
[ConsoleColor]$calendarbg='DarkGray',
[ConsoleColor]$monthyearfg='Cyan',
[ConsoleColor]$daynamefg='DarkBlue',
[ConsoleColor]$dayfg='White',
[ConsoleColor]$highlightdayfg='Red',
[ConsoleColor]$highlightdaybg='White',
[int32]$fullyear
)

#### Build an array of two first letters ####
#### localized days names ####

$firstdate=[datetime](0)
$daysnames=@(0..6)|%{
($_=$firstdate.ToString("dddd").SubString(0,2))
$firstdate=$firstdate.AddDays(1)
}

function build_date_table($date){
#### Returns an array containing days numbers ####
#### and month name ####

## Sets variables containing month and year of the date ##
$m=$date.Month
$y=$date.Year
## Get month's first day position in the week ##
[datetime]$firstday=[string]$m+'/'+[string]$y
$fd=[int]$firstday.DayOfWeek
## As "dayofweek" is 0 indexed from Sunday to ##
## Saturday and we want from Monday to Sunday we ##
## use a "1 indexed" concept : ##
if($fd -eq 0){$fd=7}
## Sets a variable containing the number ##
## of days in the month ##
$ld=(Get-Culture).Calendar.GetDaysInMonth($y,$m)
## Build an array containing days numbers relative ##
## to days names positions. ##
## If a position is not used, the value is set to ##
## a space char. ##
$table=@()

for($i=0;$i -lt $fd-1+$ld;$i++){
if($i+1 -lt $fd)
{$table+=' '*2}
else
{$table+=[string]($i-$fd+2)}
if($table[$table.Length-1].Length -eq 1)
{$table[$table.Length-1]+=' '}
if(($i+1) % 7 -eq 0)
{
$table[$table.Length-1]+=' '
if(!($table[$table.Length-1].Trim() -eq $ld))
{$table[$table.Length-1]+='`n'}
}
}
## Adds some extra space chars needed for formating output##
for($i=0;$i -lt $table.Length % 7)
{$table+=' '*2}
if($table[$table.Length-1] -eq ' '*2)
{$table[$table.Length-1]+=' '}
## get Month/Year localized string ##
$monthyear=$date.ToString("MMMM yyyy")
$monthyear=(' '*((21-$monthyear.Length)/2))+$monthyear
$monthyear+=(' '*(21-$monthyear.Length))+' '
return $table,$monthyear
}#end function build_date_table

#### Writes + colorizes calendar grid ####

$rui=$host.UI.RawUI
## Stores current background/foreground console's colors ##
$oldfg=$rui.ForegroundColor
$oldbg=$rui.BackgroundColor
## Set console's colors for Month/Year line and grid ##
$rui.BackgroundColor=$calendarbg

if(!$fullyear){
$monthtable=build_date_table(get-date $strdate)
$monthyear=$monthtable[1]
$daystable=$monthtable[0]
$rui.ForegroundColor=$monthyearfg
## Ouputs month/year line ##
[string]$monthyear
## Set days names line foreground color ##
$rui.ForegroundColor=$daynamefg
## Outputs day names ##
' '+[string]$daysnames+' '
## Set days numbers foreground color ##
$rui.ForegroundColor=$dayfg

if($strdate -ne (get-date))
## Outputs calendar month grid ##
{(' '+([string]($daystable))).Split('`n')|%{$_}}
else
## Shows current month grid and highlight current day ##
{
($daystable)|
%{
write-host (' ') -NoNewLine
if($_.Trim() -eq (get-date $strdate).Day)
{
write-host ($_) `
-NoNewLine `
-ForegroundColor $highlightdayfg `
-BackgroundColor $highlightdaybg
}
else
{write-host ($_) -NoNewLine}
}
write-host '`r'
}
}else{
## Outputs full year grid ##
$yeartable=@(0..11)
$year=[string]$fullyear
$monthnames=@()
for($i=1;$i -lt 13;$i++){
$monthdays=
build_date_table(get-date([string]$i+'/'+$year))
$yeartable[$i-1]=
((' '+[string]($monthdays)[0]).Split('`n'))
$monthnames+=($monthdays[1])
}
for($i=0;$i -lt 12;$i+=3){
$rui.ForegroundColor=$monthyearfg
$monthnames[$i]+$monthnames[$i+1]+$monthnames[$i+2]
$rui.ForegroundColor=$daynamefg
((' '+[string]$daysnames+' ')*3)
$max=(
(
$yeartable[$i].length,`
$yeartable[$i+1].length,`
$yeartable[$i+2].length
)|sort
)[2]
for($j=0;$j -lt 3;$j++){
if($yeartable[$i+$j].Length -lt $max)
{$yeartable[$i+$j]+=' '*22}
}
$rui.ForegroundColor=$dayfg
for($j=0;$j -lt $max;$j++)
{
(
$yeartable[$i][$j]+
$yeartable[$i+1][$j]+
$yeartable[$i+2][$j]
)
}
''
}
}
## Restores console's background/foreground colors ##
$rui.ForegroundColor=$oldfg
$rui.BackgroundColor=$oldbg

#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

# Amicalement,

--
Jean - JMST
Belgium

9 réponses

1 2 3
Avatar
Jean
Bon, manque de temps aujourd'hui, je m'y mettrai ce week-end :-)


Pourvu qu'il pleuve :-)

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Pourvu qu'il pleuve


En attendant je viens de fixer un problème avec le premier siècle.

Pour les années 1 à 99 quand le premier paramètre est une chaîne il
*faut* préfixer l'année de 0 à concurrence d'une longueur de 4.
Soit 0001 à 0099.

par ex :

.gadget-calendar "3/0026"

pour afficher le mois de mars de l'année 26

Ce n'est pas nécessaire pour les années 100 à 999.

.gadget-calendar "7/999"

Lorsqu'un entier est passé en paramètre (pour avoir une année complète)
ce n'est pas nécessaire non plus.

.gadget-calendar 26

pour afficher l'année 26.

Je poste le script corrigé en réponse à ce message.

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Je poste le script corrigé en réponse à ce message


#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#=9/2006===================================Version 1.1===#
#=========== "human readable" grid calendar ============#
#
# To test :
#
# .gadget-calendar
# °shows month calendar with current day
# highlighted (red on white by default)
# .gadget-calendar "6/2006"|tee -file calendar.txt
# .gadget-calendar "june 2006"|tee -file calendar.txt
# .gadget-calendar "juin 2006"|tee -file calendar.txt
# °outputs june 2006 month grid and writes it
# to calendar.txt file
# .gadget-calendar 2006|tee -file calendar.txt
# °outputs full year 2006 grid (3 x 4) and writes it
# to calendar.txt file
#
# NOTES
#
# About 1st century :
#
# When a string is passed for first argument, years between
# 1 and 99 must be 0000 formated (so 0001 to 0099).
# .gadget-calendar "5/0023"
#
# Values allowed for years :
#
# As in .Net specifications years can be in range 1..9999
#
# THANKS :
#
# to MOW to have fix a previous ennoying bug "current
# day not always highlighted".
# See this message on microsoft.public.windows.powershell :
#
#========================================================#

param(
$vardate=(get-date).Date,
[ConsoleColor]$calendarbg='DarkGray',
[ConsoleColor]$monthyearfg='Cyan',
[ConsoleColor]$daynamefg='DarkBlue',
[ConsoleColor]$dayfg='White',
[ConsoleColor]$highlightdayfg='Red',
[ConsoleColor]$highlightdaybg='White'
)

## Stores actual $OFS and set it to a space char ##
$oldOFS=$OFS
$OFS=" "

#### Build an array of two first letters ####
#### localized days names ####

$firstdate=[datetime](0)
$daysnames=(0..6)|%{
($_=$firstdate.ToString("dddd").SubString(0,2))
$firstdate=$firstdate.AddDays(1)
}

function build_date_table($date){
#### Returns an array containing days numbers ####
#### and month name ####

## Sets variables containing month and year of the date ##
$m=$date.Month
$y=($date.Year).ToString("0000")
## Get month's first day position in the week ##
[datetime]$firstday=[string]$m+'/'+[string]$y
$fd=[int]$firstday.DayOfWeek
## As "dayofweek" is 0 indexed from Sunday to ##
## Saturday and we want from Monday to Sunday we ##
## use a "1 indexed" concept : ##
if($fd -eq 0){$fd=7}
## Sets a variable containing the number ##
## of days in the month ##
$ld=(Get-Culture).Calendar.GetDaysInMonth($y,$m)
## Build an array containing days numbers relative ##
## to days names positions. ##
## If a position is not used, the value is set to ##
## a space char. ##
$table=@()

for($i=0;$i -lt $fd-1+$ld;$i++){
if($i+1 -lt $fd)
{$table+=' '*2}
else
{$table+=[string]($i-$fd+2)}
if($table[$table.Length-1].Length -eq 1)
{$table[$table.Length-1]+=' '}
if(($i+1) % 7 -eq 0)
{
$table[$table.Length-1]+=' '
if(!($table[$table.Length-1].Trim() -eq $ld))
{$table[$table.Length-1]+='`n'}
}
}
## Adds some extra space chars needed for formating output##
for($i=0;$i -lt $table.Length % 7)
{$table+=' '*2}
if($table[$table.Length-1] -eq ' '*2)
{$table[$table.Length-1]+=' '}
## get Month/Year localized string ##
$monthyear=$date.ToString("MMMM yyyy")
$monthyear=(' '*((21-$monthyear.Length)/2))+$monthyear
$monthyear+=(' '*(21-$monthyear.Length))+' '
return $table,$monthyear
}#end function build_date_table

#### Writes + colorizes calendar grid ####

$rui=(get-host).UI.RawUI
## Stores current background/foreground console's colors ##
$oldfg=$rui.ForegroundColor
$oldbg=$rui.BackgroundColor
## Set console's colors for Month/Year line and grid ##
$rui.BackgroundColor=$calendarbg

## If an integer is passed for $vardate then defines ##
## $fullyear to $vardate ##
if($vardate.GetTypeCode() -eq
"Int32"){$fullyear=$vardate.ToString("0000")}

if(!$fullyear){
$monthtable=build_date_table(get-date $vardate)
$monthyear=$monthtable[1]
$daystable=$monthtable[0]
$rui.ForegroundColor=$monthyearfg
## Ouputs month/year line ##
[string]$monthyear
## Set days names line foreground color ##
$rui.ForegroundColor=$daynamefg
## Outputs day names ##
' '+[string]$daysnames+' '
## Set days numbers foreground color ##
$rui.ForegroundColor=$dayfg

if($vardate -ne (get-date).Date)
## Outputs calendar month grid ##
{(' '+([string]($daystable))).Split('`n')|%{$_}}
else
## Shows current month grid and highlight current day ##
{
($daystable)|`
%{
write-host (' ') -NoNewLine
if($_.Trim() -eq (get-date $vardate).Day)
{
write-host ($_[0]+$_[1]) `
-NoNewLine `
-ForegroundColor $highlightdayfg `
-BackgroundColor $highlightdaybg
if($_[2] -eq ' '){write-host($_[2])}
}
else
{write-host ($_) -NoNewLine}
}
write-host '`r'
}
}else{
## Outputs full year grid ##
$yeartable=(0..11)
$year=$fullyear
$monthnames=@()
for($i=1;$i -lt 13;$i++){
$monthdays build_date_table(get-date('1/'+[string]$i+'/'+$year))
$yeartable[$i-1] ((' '+[string]($monthdays)[0]).Split('`n'))
$monthnames+=($monthdays[1])
}
for($i=0;$i -lt 12;$i+=3){
$rui.ForegroundColor=$monthyearfg
$monthnames[$i]+$monthnames[$i+1]+$monthnames[$i+2]
$rui.ForegroundColor=$daynamefg
((' '+[string]$daysnames+' ')*3)
$max=(
(
$yeartable[$i].length,`
$yeartable[$i+1].length,`
$yeartable[$i+2].length
)|sort
)[2]
for($j=0;$j -lt 3;$j++){
if($yeartable[$i+$j].Length -lt $max)
{$yeartable[$i+$j]+=' '*22}
}
$rui.ForegroundColor=$dayfg
for($j=0;$j -lt $max;$j++)
{
(
$yeartable[$i][$j]+
$yeartable[$i+1][$j]+
$yeartable[$i+2][$j]
)
}
''
}
}
## Restores console's background/foreground colors ##
$rui.ForegroundColor=$oldfg
$rui.BackgroundColor=$oldbg

## Restores OFS to its previous value
$OFS=$oldOFS
#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
J'ai un problème d'affichage quand j'essaie le script. Une ligne vierge
s'intercale entre deux lignes du calendrier.


Le problème a été fixé par James Truher sur
microsoft.public.windows.powershell :
http://groups.google.com/group/microsoft.public.windows.powershell/msg/7164c7d9364ad45a?dmode=source

Encore merci à lui.

Comme le script n'a pas besoin du comportement de l'apostrophe, plutôt
que de modifier uniquement la ligne indiquée, j'ai remplacé toute les
apostrophes par des double guillemets.

Je poste le script corrigé en réponse à ce message.

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Je poste le script corrigé en réponse à ce message.


#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#=9/2006===================================Version 1.2===#
#=========== "human readable" grid calendar ============#
#
# To test :
#
# .gadget-calendar
# °shows month calendar with current day
# highlighted (red on white by default)
# .gadget-calendar "6/2006"|tee -file calendar.txt
# .gadget-calendar "june 2006"|tee -file calendar.txt
# .gadget-calendar "juin 2006"|tee -file calendar.txt
# °outputs june 2006 month grid and writes it
# to calendar.txt file
# .gadget-calendar 2006|tee -file calendar.txt
# °outputs full year 2006 grid (3 x 4) and writes it
# to calendar.txt file
#
# NOTES
#
# About 1st century :
#
# When a string is passed for first argument, years between
# 1 and 99 must be 0000 formated (so 0001 to 0099).
# .gadget-calendar "5/0023"
#
# Values allowed for years :
#
# As in .Net specifications years can be in range 1..9999
#
# THANKS :
#
# To MOW to have fix a previous ennoying bug "current
# day not always highlighted".
# See this message on microsoft.public.windows.powershell :
#
#
# To Jacques Barathon to have reported a problem with RC2 :
# "blank lines added".
# See this message on microsoft.public.fr.scripting :
#
# To James Truher to have fix this RC2 compatibility problem.
# See this message on microsoft.public.windows.powershell :
#
#
#========Tested on PowerShell 1 RC1 (1.0.9567.1)=========#

param(
$vardate=(get-date).Date,
[ConsoleColor]$calendarbg="DarkGray",
[ConsoleColor]$monthyearfg="Cyan",
[ConsoleColor]$daynamefg="DarkBlue",
[ConsoleColor]$dayfg="White",
[ConsoleColor]$highlightdayfg="Red",
[ConsoleColor]$highlightdaybg="White"
)

## Stores actual $OFS and set it to a space char ##
$oldOFS=$OFS
$OFS=" "

#### Build an array of two first letters ####
#### localized days names ####

$firstdate=[datetime](0)
$daysnames=(0..6)|%{
($_=$firstdate.ToString("dddd").SubString(0,2))
$firstdate=$firstdate.AddDays(1)
}

function build_date_table($date){
#### Returns an array containing days numbers ####
#### and month name ####

## Sets variables containing month and year of the date ##
$m=$date.Month
$y=($date.Year).ToString("0000")
## Get month's first day position in the week ##
[datetime]$firstday=[string]$m+"/"+[string]$y
$fd=[int]$firstday.DayOfWeek
## As "dayofweek" is 0 indexed from Sunday to ##
## Saturday and we want from Monday to Sunday we ##
## use a "1 indexed" concept : ##
if($fd -eq 0){$fd=7}
## Sets a variable containing the number ##
## of days in the month ##
$ld=(Get-Culture).Calendar.GetDaysInMonth($y,$m)
## Build an array containing days numbers relative ##
## to days names positions. ##
## If a position is not used, the value is set to ##
## a space char. ##
$table=@()

for($i=0;$i -lt $fd-1+$ld;$i++){
if($i+1 -lt $fd)
{$table+=" "*2}
else
{$table+=[string]($i-$fd+2)}
if($table[$table.Length-1].Length -eq 1)
{$table[$table.Length-1]+=" "}
if(($i+1) % 7 -eq 0)
{
$table[$table.Length-1]+=" "
if(!($table[$table.Length-1].Trim() -eq $ld))
{$table[$table.Length-1]+="`n"}
}
}
## Adds some extra space chars needed for formating output##
for($i=0;$i -lt $table.Length % 7)
{$table+=" "*2}
if($table[$table.Length-1] -eq " "*2)
{$table[$table.Length-1]+=" "}
## get Month/Year localized string ##
$monthyear=$date.ToString("MMMM yyyy")
$monthyear=(" "*((21-$monthyear.Length)/2))+$monthyear
$monthyear+=(" "*(21-$monthyear.Length))+" "
return $table,$monthyear
}#end function build_date_table

#### Writes + colorizes calendar grid ####

$rui=(get-host).UI.RawUI
## Stores current background/foreground console's colors ##
$oldfg=$rui.ForegroundColor
$oldbg=$rui.BackgroundColor
## Set console's colors for Month/Year line and grid ##
$rui.BackgroundColor=$calendarbg

## If an integer is passed for $vardate then defines ##
## $fullyear to $vardate ##
if($vardate.GetTypeCode() -eq "Int32")
{$fullyear=$vardate.ToString("0000")}

if(!$fullyear){
$monthtable=build_date_table(get-date $vardate)
$monthyear=$monthtable[1]
$daystable=$monthtable[0]
$rui.ForegroundColor=$monthyearfg
## Ouputs month/year line ##
[string]$monthyear
## Set days names line foreground color ##
$rui.ForegroundColor=$daynamefg
## Outputs day names ##
" "+[string]$daysnames+" "
## Set days numbers foreground color ##
$rui.ForegroundColor=$dayfg

if($vardate -ne (get-date).Date)
## Outputs calendar month grid ##
{(" "+([string]($daystable))).Split("`n")|%{$_}}
else
## Shows current month grid and highlight current day ##
{
($daystable)|`
%{
write-host (" ") -NoNewLine
if($_.Trim() -eq (get-date $vardate).Day)
{
write-host ($_[0]+$_[1]) `
-NoNewLine `
-ForegroundColor $highlightdayfg `
-BackgroundColor $highlightdaybg
if($_[2] -eq " "){write-host($_[2])}
}
else
{write-host ($_) -NoNewLine}
}
write-host "`r"
}
}else{
## Outputs full year grid ##
$yeartable=(0..11)
$year=$fullyear
$monthnames=@()
for($i=1;$i -lt 13;$i++){
$monthdays build_date_table(get-date("1/"+[string]$i+"/"+$year))
$yeartable[$i-1] ((" "+[string]($monthdays)[0]).Split("`n"))
$monthnames+=($monthdays[1])
}
for($i=0;$i -lt 12;$i+=3){
$rui.ForegroundColor=$monthyearfg
$monthnames[$i]+$monthnames[$i+1]+$monthnames[$i+2]
$rui.ForegroundColor=$daynamefg
((" "+[string]$daysnames+" ")*3)
$max=(
(
$yeartable[$i].length,`
$yeartable[$i+1].length,`
$yeartable[$i+2].length
)|sort
)[2]
for($j=0;$j -lt 3;$j++){
if($yeartable[$i+$j].Length -lt $max)
{$yeartable[$i+$j]+=" "*22}
}
$rui.ForegroundColor=$dayfg
for($j=0;$j -lt $max;$j++)
{
(
$yeartable[$i][$j]+
$yeartable[$i+1][$j]+
$yeartable[$i+2][$j]
)
}
""
}
}
## Restores console's background/foreground colors ##
$rui.ForegroundColor=$oldfg
$rui.BackgroundColor=$oldbg

## Restores OFS to its previous value
$OFS=$oldOFS
#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---


#Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
par des double guillemets


par des guillemets
(english spaghetti :-) )

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jacques Barathon [MS]
Merci Jean.

Pour info, j'ai toujours un souci avec la version corrigée. Cette fois-ci
c'est les couleurs qui "bavent". L'exécution du script étale la couleur gris
sur toutes les lignes où le calendrier s'affiche, alors que le fond de base
de ma session est noir. Cela donne des effets assez peu heureux.

Je n'ai pas bcp de temps en ce moment, mais j'essaierai de regarder plus
tard...

Jacques

"Jean" a écrit dans le message de news:

Je poste le script corrigé en réponse à ce message.


#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#=9/2006===================================Version 1.2===#
#=========== "human readable" grid calendar ============#
#
# To test :
#
# .gadget-calendar
# °shows month calendar with current day
# highlighted (red on white by default)
# .gadget-calendar "6/2006"|tee -file calendar.txt
# .gadget-calendar "june 2006"|tee -file calendar.txt
# .gadget-calendar "juin 2006"|tee -file calendar.txt
# °outputs june 2006 month grid and writes it
# to calendar.txt file
# .gadget-calendar 2006|tee -file calendar.txt
# °outputs full year 2006 grid (3 x 4) and writes it
# to calendar.txt file
#
# NOTES
#
# About 1st century :
#
# When a string is passed for first argument, years between
# 1 and 99 must be 0000 formated (so 0001 to 0099).
# .gadget-calendar "5/0023"
#
# Values allowed for years :
#
# As in .Net specifications years can be in range 1..9999
#
# THANKS :
#
# To MOW to have fix a previous ennoying bug "current
# day not always highlighted".
# See this message on microsoft.public.windows.powershell :
#
#
# To Jacques Barathon to have reported a problem with RC2 :
# "blank lines added".
# See this message on microsoft.public.fr.scripting :
#
# To James Truher to have fix this RC2 compatibility problem.
# See this message on microsoft.public.windows.powershell :
#
#
#========Tested on PowerShell 1 RC1 (1.0.9567.1)=========#

param(
$vardate=(get-date).Date,
[ConsoleColor]$calendarbg="DarkGray",
[ConsoleColor]$monthyearfg="Cyan",
[ConsoleColor]$daynamefg="DarkBlue",
[ConsoleColor]$dayfg="White",
[ConsoleColor]$highlightdayfg="Red",
[ConsoleColor]$highlightdaybg="White"
)

## Stores actual $OFS and set it to a space char ##
$oldOFS=$OFS
$OFS=" "

#### Build an array of two first letters ####
#### localized days names ####

$firstdate=[datetime](0)
$daysnames=(0..6)|%{
($_=$firstdate.ToString("dddd").SubString(0,2))
$firstdate=$firstdate.AddDays(1)
}

function build_date_table($date){
#### Returns an array containing days numbers ####
#### and month name ####

## Sets variables containing month and year of the date ##
$m=$date.Month
$y=($date.Year).ToString("0000")
## Get month's first day position in the week ##
[datetime]$firstday=[string]$m+"/"+[string]$y
$fd=[int]$firstday.DayOfWeek
## As "dayofweek" is 0 indexed from Sunday to ##
## Saturday and we want from Monday to Sunday we ##
## use a "1 indexed" concept : ##
if($fd -eq 0){$fd=7}
## Sets a variable containing the number ##
## of days in the month ##
$ld=(Get-Culture).Calendar.GetDaysInMonth($y,$m)
## Build an array containing days numbers relative ##
## to days names positions. ##
## If a position is not used, the value is set to ##
## a space char. ##
$table=@()

for($i=0;$i -lt $fd-1+$ld;$i++){
if($i+1 -lt $fd)
{$table+=" "*2}
else
{$table+=[string]($i-$fd+2)}
if($table[$table.Length-1].Length -eq 1)
{$table[$table.Length-1]+=" "}
if(($i+1) % 7 -eq 0)
{
$table[$table.Length-1]+=" "
if(!($table[$table.Length-1].Trim() -eq $ld))
{$table[$table.Length-1]+="`n"}
}
}
## Adds some extra space chars needed for formating output##
for($i=0;$i -lt $table.Length % 7)
{$table+=" "*2}
if($table[$table.Length-1] -eq " "*2)
{$table[$table.Length-1]+=" "}
## get Month/Year localized string ##
$monthyear=$date.ToString("MMMM yyyy")
$monthyear=(" "*((21-$monthyear.Length)/2))+$monthyear
$monthyear+=(" "*(21-$monthyear.Length))+" "
return $table,$monthyear
}#end function build_date_table

#### Writes + colorizes calendar grid ####

$rui=(get-host).UI.RawUI
## Stores current background/foreground console's colors ##
$oldfg=$rui.ForegroundColor
$oldbg=$rui.BackgroundColor
## Set console's colors for Month/Year line and grid ##
$rui.BackgroundColor=$calendarbg

## If an integer is passed for $vardate then defines ##
## $fullyear to $vardate ##
if($vardate.GetTypeCode() -eq "Int32")
{$fullyear=$vardate.ToString("0000")}

if(!$fullyear){
$monthtable=build_date_table(get-date $vardate)
$monthyear=$monthtable[1]
$daystable=$monthtable[0]
$rui.ForegroundColor=$monthyearfg
## Ouputs month/year line ##
[string]$monthyear
## Set days names line foreground color ##
$rui.ForegroundColor=$daynamefg
## Outputs day names ##
" "+[string]$daysnames+" "
## Set days numbers foreground color ##
$rui.ForegroundColor=$dayfg

if($vardate -ne (get-date).Date)
## Outputs calendar month grid ##
{(" "+([string]($daystable))).Split("`n")|%{$_}}
else
## Shows current month grid and highlight current day ##
{
($daystable)|`
%{
write-host (" ") -NoNewLine
if($_.Trim() -eq (get-date $vardate).Day)
{
write-host ($_[0]+$_[1]) `
-NoNewLine `
-ForegroundColor $highlightdayfg `
-BackgroundColor $highlightdaybg
if($_[2] -eq " "){write-host($_[2])}
}
else
{write-host ($_) -NoNewLine}
}
write-host "`r"
}
}else{
## Outputs full year grid ##
$yeartable=(0..11)
$year=$fullyear
$monthnames=@()
for($i=1;$i -lt 13;$i++){
$monthdays > build_date_table(get-date("1/"+[string]$i+"/"+$year))
$yeartable[$i-1] > ((" "+[string]($monthdays)[0]).Split("`n"))
$monthnames+=($monthdays[1])
}
for($i=0;$i -lt 12;$i+=3){
$rui.ForegroundColor=$monthyearfg
$monthnames[$i]+$monthnames[$i+1]+$monthnames[$i+2]
$rui.ForegroundColor=$daynamefg
((" "+[string]$daysnames+" ")*3)
$max=(
(
$yeartable[$i].length,`
$yeartable[$i+1].length,`
$yeartable[$i+2].length
)|sort
)[2]
for($j=0;$j -lt 3;$j++){
if($yeartable[$i+$j].Length -lt $max)
{$yeartable[$i+$j]+=" "*22}
}
$rui.ForegroundColor=$dayfg
for($j=0;$j -lt $max;$j++)
{
(
$yeartable[$i][$j]+
$yeartable[$i+1][$j]+
$yeartable[$i+2][$j]
)
}
""
}
}
## Restores console's background/foreground colors ##
$rui.ForegroundColor=$oldfg
$rui.BackgroundColor=$oldbg

## Restores OFS to its previous value
$OFS=$oldOFS
#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---


#Amicalement,

--
Jean - JMST
Belgium





Avatar
Jean
L'exécution du script étale la couleur gris sur toutes les lignes où le
calendrier s'affiche,


Je pense que c'est le bug dont j'avais parlé sur windows.powershell
().

Je l'avais rapporté sur Connect :
https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID6655&SiteID™&wa=wsignin1.0

... résolu : By Design ...

C'est lié à la taille du tampon d'affichage de la console.
Avec l'installation par défaut de la RC2, pour reproduire le phénomène,
faire

cls

puis exécuter le script suivant :

#---8<---color.ps1---8<---
$rui=$host.UI.RawUI

$oldbg=$rui.BackgroundColor
$oldfg=$rui.ForegroundColor

$rui.BackgroundColor='DarkGray'
$rui.ForegroundColor='White'

(1..3000)|%{$_}

$rui.ForegroundColor=$oldfg
$rui.BackgroundColor=$oldbg
#---8<---color.ps1---8<---

... pour revenir à une situation normale il faut faire un "cls" dans la
console.

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Je l'avais rapporté sur Connect :

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID6655&SiteID™&wa=wsignin1.0

... résolu : By Design ...


oups ... c'est celui-ci :

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID6656&SiteID™&wa=wsignin1.0

... Résolu : Reporté (ouf :-) )

--
Jean - JMST
Belgium

1 2 3