Skip to content

Commit

Permalink
see changelog for v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Oct 1, 2018
1 parent bc757d8 commit b4ea746
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 6 deletions.
6 changes: 3 additions & 3 deletions PSCalendar.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RootModule = 'PSCalendar.psm1'

# Version number of this module.
ModuleVersion = '1.2.0'
ModuleVersion = '1.3.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -64,7 +64,7 @@ PowerShellVersion = '5.1'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Get-Calendar','Show-Calendar'
FunctionsToExport = 'Get-Calendar','Show-Calendar','Show-GuiCalendar'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = ''
Expand All @@ -73,7 +73,7 @@ CmdletsToExport = ''
VariablesToExport = ''

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = 'cal','scal'
AliasesToExport = 'cal','scal','gcal'

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ This command is in essence a "wrapper" function for `Get-Calendar`.

![show calendar with highlights](assets/show-calendar-2.png)

*last updated 9/28/2018*
## Show-GUICalendar

Finally, you can display a graphical calendar using a WPF-based script. The function runs the calendar-related code in a runspace so it does not block your prompt. You can display up to 3 months and specify dates to highlight.

```powershell
PS C:\> Show-GuiCalendar 12/2018 2/2019 -highlight 12/24/18,12/25/18,12/31/18,1/1/19,1/18/19,2/14/19,2/22/19
```

![show-guicalendar](assets/show-guicalendar.png)

The calendar form is transparent. But you should be able to click on it to drag it around your screen. You can also use the + and - keys to increase or decrease the calendar's opacity.

*last updated 1 October 2018*
Binary file added assets/show-guicalendar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
240 changes: 238 additions & 2 deletions calendar-functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Function Get-Calendar {
[Alias("cal")]

Param(
[Parameter(Position = 1,ParameterSetName = "month")]
[Parameter(Position = 1, ParameterSetName = "month")]
[ValidateNotNullorEmpty()]
[ValidateScript( {
$names = Get-MonthsByCulture
Expand All @@ -33,6 +33,15 @@ Function Get-Calendar {

[Parameter(Mandatory, HelpMessage = "Enter an ending date for the month like 2/1/2019", ParameterSetName = "span")]
[ValidateNotNullOrEmpty()]
[ValidateScript({
if ($_ -ge $Start) {
$True
}
else {
Throw "The end date ($_) must be later than the start date ($start)"
$False
}
})]
[DateTime]$End,

[string[]]$HighlightDate = (Get-Date).date.toString()
Expand Down Expand Up @@ -166,7 +175,7 @@ Function Show-Calendar {
})]
[string]$Month = (Get-Date -format MMMM),

[Parameter(Position = 2,ParameterSetName = "month")]
[Parameter(Position = 2, ParameterSetName = "month")]
[ValidatePattern('^\d{4}$')]
[int]$Year = (Get-Date).Year,

Expand All @@ -176,6 +185,8 @@ Function Show-Calendar {
[consolecolor]$HighlightColor = "Green"
)

Write-Verbose "Starting $($myinvocation.mycommand)"

#add default values if not bound
$params = "Month", "Year", "HighlightDate"
foreach ($param in $params) {
Expand Down Expand Up @@ -227,8 +238,233 @@ Function Show-Calendar {
Write-host $line
}
} #foreach line in calarray

Write-Verbose "Ending $($myinvocation.mycommand)"

} #end Show-Calendar

#create a WPF-based calendar
Function Show-GuiCalendar {
[cmdletbinding()]
[OutputType("None")]
[Alias("gcal")]

Param(
[Parameter(Position = 1, HelpMessage = "Enter the first month to display by date, like 1/1/2019.")]
[ValidateNotNullOrEmpty()]
[datetime]$Start = [datetime]::new([datetime]::now.year, [datetime]::now.month, 1),

[Parameter(Position = 2, HelpMessage = "Enter the last month to display by date, like 3/1/2019. You cannot display more than 3 months.")]
[ValidateNotNullOrEmpty()]
[ValidateScript({
if ($_ -ge $Start) {
$True
}
else {
Throw "The end date ($_) must be later than the start date ($start)"
$False
}
})]
[datetime]$End = [datetime]::new([datetime]::now.year, [datetime]::now.month, 1),

[Parameter(HelpMessage = "Enter an array of dates to highlight like 12/25/2019.")]
[datetime[]]$HighlightDate,

[Parameter(HelpMessage = "Select a font family for your calendar" )]
[ValidateSet("Segoi UI", "QuickType", "Tahoma", "Lucida Console", "Century Gothic")]
[string]$Font = "Segoi UI",


[Parameter(HelpMessage = "Select a font style for your calendar." )]
[ValidateSet("Normal", "Italic", "Oblique")]
[string]$FontStyle = "Normal",

[Parameter(HelpMessage = "Select a font weight for your calendar." )]
[ValidateSet("Normal", "DemiBold", "Light", "Bold")]
[string]$FontWeight = "Normal"
)

Write-Verbose "Starting $($myinvocation.mycommand)"

if ($psedition -eq 'Core') {
Write-Warning "This function requires Windows PowerShell."
#bail out
Return
}

$months = do {
$start
$start = $start.AddMonths(1)
} while ($start -le $end)

if ($months.count -gt 3) {
Write-Warning "You can't display more than 3 months at a time with this command."
#bail out
Return
}

#add the necessary type library and bail out if there are errors

Try {
Add-Type -AssemblyName PresentationFramework -ErrorAction Stop
Add-Type –assemblyName PresentationCore -ErrorAction Stop
Add-Type –assemblyName WindowsBase -ErrorAction Stop
}
Catch {
Write-Warning "Failed to load a required type library. $($_.exception.message)"
#bail out
Return
}

$myParams=@{
Months = $months
Height = (200 * $months.count)
Title = "My Calendar"
HighlightDate = $HighlightDate
Font = $Font
FontStyle = $FontStyle
FontWeight = $FontWeight
}

Write-Verbose "Using these parameters"
$myparams | Out-String | Write-Verbose

$newRunspace = [RunspaceFactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()

Write-Verbose "Defining runspace script"
$psCmd = [PowerShell]::Create().AddScript( {

Param (
[datetime[]]$HighlightDate,
[string]$Font,
[string]$FontStyle,
[string]$FontWeight,
[int]$Height,
[string]$Title,
[datetime[]]$Months
)

#create a window form.
$form = New-Object System.Windows.Window

$form.AllowsTransparency = $True
$form.WindowStyle = "none"
#the title won't be shown when window style is set to none
$form.Title = $Title
$form.Height = $height
$form.Width = 200

$bg = new-object System.Windows.Media.SolidColorBrush

$form.Background = $bg
#color is set for development purposes. It won't be seen normally.
$form.Background.Color = "green"
$form.background.Opacity = 0

$form.Add_MouseLeftButtonDown( {$form.DragMove()})

#add event handlers to adjust opacity by using the +/- keys
$form.add_KeyDown( {

switch ($_.key) {
{'Add', 'OemPlus' -contains $_} {
foreach ($cal in $myCals) {
If ($cal.Opacity -lt 1) {
$cal.Opacity = $cal.opacity + .1
$cal.UpdateLayout()
}
}
}
{'Subtract', 'OemMinus' -contains $_} {
foreach ($cal in $myCals) {
If ($cal.Opacity -gt .2) {
$cal.Opacity = $cal.Opacity - .1
$cal.UpdateLayout()
}
}
}
}
})

$stack = $stack = New-object System.Windows.Controls.StackPanel
$stack.Width = $form.Width
$stack.Height = $form.Height
$stack.HorizontalAlignment = "center"
$stack.VerticalAlignment = "top"

#create an array to store calendars so that opacity can be
#set for multiple calendars in unison
$myCals = @()
foreach ($month in $months) {
$cal = New-Object System.Windows.Controls.Calendar
$cal.DisplayMode = "Calendar"

$cal.Opacity = 1
$cal.FontFamily = $font
$cal.FontSize = 24
$cal.FontWeight = $FontWeight
$cal.FontStyle = $fontStyle

$cal.DisplayDateStart = $month

$cal.HorizontalAlignment = "center"
$cal.VerticalAlignment = "top"

$cal.SelectionMode = "multipleRange"
if ($highlightdate) {
foreach ($d in $HighlightDate) {
if ($d.month -eq $month.Month) {
$cal.SelectedDates.add($d)

}
}
}

$cal.add_DisplayDateChanged( {
# add the selected days for the currently displayed month
$cal | out-string | write-host
[datetime]$month = $cal.Displaydate
if ($highlightdate) {
foreach ($d in $HighlightDate) {
if ($d.month -eq $month.Month) {
$cal.SelectedDates.add($d)
}
}
}
$cal.UpdateLayout()
})

$stack.addchild($cal)
$myCals+=$cal
}

$btn = New-Object System.Windows.Controls.Button
$btn.Content = "_Close"
$btn.Width = 75
$btn.VerticalAlignment = "Bottom"
$btn.HorizontalAlignment = "Center"
$btn.Opacity = 1
$btn.Add_click( {$form.close()})

$stack.AddChild($btn)

$form.AddChild($stack)
$form.ShowDialog() | out-null
})


$pscmd.AddParameters($myparams) | Out-Null
$psCmd.Runspace = $newRunspace
Write-Verbose "Invoking calendar runspace"
$psCmd.BeginInvoke() | Out-Null
Write-Verbose "Ending $($myinvocation.mycommand)"

} #close Show-GuiCalendar


#a helper function to retrieve names
function Get-MonthsbyCulture {
[cmdletbinding()]
Expand Down
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log for PSCalendar

## v1.3.0

+ Added WPF calendar function `Show-GuiCalendar` (Issue #3)
+ Added alias `gcal` for `Show-GuiCalendar`
+ Added parameter validation to `-End` to make sure it is after `-Start`
+ Updated `README.md`

## v1.2.0

+ Added validation for `-Month` (Issue #1)
Expand Down
1 change: 1 addition & 0 deletions docs/Get-Calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,4 @@ Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell
[Show-Calendar]()
[Show-GuiCalendar]()
2 changes: 2 additions & 0 deletions docs/Show-Calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell
## RELATED LINKS
[Get-Calendar]()
[Show-GuiCalendar]()
Loading

0 comments on commit b4ea746

Please sign in to comment.