OVH Cloud OVH Cloud

[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

10 réponses

1 2 3
Avatar
Jean
J'espère que cela t'aura consolé.


On voit ici le résultat de vos excursions belges :O)

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jean
Reste le problème de la mise en évidence du jour ... mais c'est plus long a
tester parcequ'il faut redémarrer la console à chaque fois.


Je ne suis pas encore parvenu a isoler le problème mais d'après mes
tests ce serait la ligne de test :

if($_.Trim() -eq (get-date $strdate).Day)

qui n'est pas (toujours?) traitée correctement au premier démarrage.

J'ai par contre trouvé un "fix" qui consite a remplacer :

if($_.Trim() -eq (get-date $strdate).Day)
{
write-host ($_) `

par

if($_.Trim() -eq (get-date $strdate).Day)
{
write-host (''+$_) `

--
Jean - JMST
Belgium

Avatar
Jean
J'ai par contre trouvé un "fix"


Non ... ce n'était pas un fix.
L'aventure continue :-)

Amicalement,

--
Jean - JMST
Belgium

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


Voici une version 0.1 dans laquelle le bug de la mise en évidence du
jour courant a été fixé grâce à MOW sur le groupe
microsoft.public.windows.powershell dans le message d'id :


Encore merci à lui :-)

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

#=6/2006=====================================Version 0.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 -fullyear 2006|tee -file calendar.txt
# °outputs full year 2006 grid (3 x 4) and writes it
# to calendar.txt file
#
# Thanks to MOW to have fix a previous annoying bug "current
# day not always highlighted".
# see this message on microsoft.public.windows.powershell :
#
#======================================================#

param(
[string]$strdate=(get-date).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=(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(!$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).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

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

#=9/2006=====================================Version 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
#
# 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'
)

#### 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=(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}

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=[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
Avatar
Jean
#---8<---gadget-calendar.ps1---Jean-JMST-Belgium---8<---

#=9/2006=====================================Version 1=#
#=========== "human readable" grid calendar ============#


Voici donc la version 1 de gadget-calendar.ps1.

Ormis la résolution d'un petit bug d'affichage j'ai retiré
le paramètre -fullyear.
En lieu et place il suffit de passer un entier (représentant l'année)
en premier paramètre.

Par souci de cohésion le (premier) paramètre -strdate a été renommé
-vardate.

On peut donc taper :

.gadget-calendar.ps1 2008

pour afficher le calendrier complet de l'année 2008


.gadget-calendar.ps1 "1/2008"
ou
.gadget-calendar.ps1 "january 2008"
ou
.gadget-calendar.ps1 "janvier 2008"

pour afficher le mois de janvier 2008

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. Je n'ai pas le temps de débugger
ce soir, j'ai juste constaté que le problème apparaît avec la ligne suivante


Merci du retour.
C'est assez gênant en effet.
Je ne reproduis pas le problème ici avec la RC1.
Je pense que c'est une question de version (vous utilisez déjà la RC 2
je crois).
Dans l'affirmative, reste à voir si il sagit d'un bug ou d'une
différence de traitement dans la RC2.

Amicalement,


--
Jean - JMST
Belgium

Avatar
Jacques Barathon [MS]
"Jean" a écrit dans le message de news:

J'ai un problème d'affichage quand j'essaie le script. Une ligne vierge
s'intercale entre deux lignes du calendrier. Je n'ai pas le temps de
débugger ce soir, j'ai juste constaté que le problème apparaît avec la
ligne suivante


Merci du retour.
C'est assez gênant en effet.
Je ne reproduis pas le problème ici avec la RC1.
Je pense que c'est une question de version (vous utilisez déjà la RC 2 je
crois).
Dans l'affirmative, reste à voir si il sagit d'un bug ou d'une différence
de traitement dans la RC2.


J'utilise une version post-RC1, mais ce n'est pas la RC2. J'essaierai de
creuser le sujet dans la journée.

Jacques


Avatar
Jean
J'utilise une version post-RC1, mais ce n'est pas la RC2. J'essaierai de
creuser le sujet dans la journée.


C'est sympa :-)

Juste une idée avant d'incriminer la version.

Voir si ça ne se résoudrait pas en mettant au tout début du script :

$oldOFS=$OFS
$OFS=" "

et à la toute fin du script

$OFS=$oldOFS

Amicalement,

--
Jean - JMST
Belgium

Avatar
Jacques Barathon [MS]
"Jean" a écrit dans le message de news:

J'utilise une version post-RC1, mais ce n'est pas la RC2. J'essaierai de
creuser le sujet dans la journée.


C'est sympa :-)


Bon, manque de temps aujourd'hui, je m'y mettrai ce week-end :-)

Juste une idée avant d'incriminer la version.

Voir si ça ne se résoudrait pas en mettant au tout début du script :

$oldOFS=$OFS
$OFS=" "

et à la toute fin du script

$OFS=$oldOFS


J'avais déjà essayé de modifier $OFS dans mon test rapide en mode debug,
sans effet positif.

Jacques


1 2 3