-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSQuizmaster.psm1
110 lines (92 loc) · 4.18 KB
/
PSQuizmaster.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#load functions
$FunFolder =Join-Path -path $PSScriptRoot -ChildPath functions
Get-ChildItem -Path $FunFolder -filter *.ps1 |
ForEach-Object { . $_.FullName}
#this is a private variable not exposed to the user
$PSQuizSettingsFile = Join-Path -Path $HOME -ChildPath '.psquizsettings.json'
if (Test-Path -path $PSQuizSettingsFile) {
$PSQuizSettings = Get-Content -path $PSQuizSettingsFile | ConvertFrom-Json
Set-Variable -name PSQuizPath -value $PSQuizSettings.PSQuizPath
}
else {
Set-Variable -name PSQuizPath -value (Join-Path -Path $PSScriptRoot -ChildPath "quizzes")
}
#Path to the JSON schema file
#this is an internal variable
$PSQuizSchema = "https://raw.githubusercontent.com/jdhitsolutions/PSQuizMaster/main/psquiz.schema.json"
#for local testing
# "file:///c://scripts//psquizmaster//psquiz.schema.json"
Register-ArgumentCompleter -CommandName Invoke-PSQuiz -ParameterName Path -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#PowerShell code to populate $WordToComplete
Get-ChildItem -path $PSQuizPath -filter *quiz.json |
Where-Object { $_.Name -like "$wordToComplete*" } |
ForEach-Object {
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new($_.fullName, $_.name, 'ParameterValue', $_.FullName)
}
}
#add a VSCode menu item to insert the UTC date time into a quiz file
If ($host.Name -eq 'Visual Studio Code Host') {
$sb = {
$utc = "{0:u}" -f (Get-Date).ToUniversalTime()
$Context = $psEditor.GetEditorContext()
$context.CurrentFile.InsertText($utc)
}
Register-EditorCommand -Name QuizDate -DisplayName "Insert PSQuiz date" -ScriptBlock $sb
<#
#TODO fix these commands and create ISE equivalents
Function MaskQuizAnswer {
Param()
Function _hideAnswer {
Param([string]$Answer)
[regex]$word = '\S+'
#write-host "private $answer" -fore green
foreach ($part in $Answer) {
$word.Matches($part).Value.ForEach({
$_.toCharArray().Foreach({ '{0:d3}' -f ($_ -as [int]) }) -join ''
}) -join ' '
}
}
$Context = $psEditor.GetEditorContext()
$selected = [Microsoft.PowerShell.EditorServices.Extensions.FileRange, Microsoft.PowerShell.EditorServices]::new($Context.SelectedRange.Start, $Context.SelectedRange.end)
[string]$text = $Context.CurrentFile.GetText($selected)
write-host "masking $text [$($text.Length)]"
[string]$mask = _hideAnswer $text
write-host "with $mask"
$context.CurrentFile.InsertText($mask)
}
Register-EditorCommand -Name MaskQuizAnswer -DisplayName "Mask Quiz Answer" -Function MaskQuizAnswer
Function UnMaskQuizAnswer {
Param()
Function _showAnswer {
Param([string]$ProtectedAnswer)
[regex]$number = '\d{3}'
$out = foreach ($part in $ProtectedAnswer.split()) {
$number.Matches($part).Value.ForEach({
([int]$_ -as [string][char])
}) -join ''
}
$out -join ' '
}
$Context = $psEditor.GetEditorContext()
$selected = [Microsoft.PowerShell.EditorServices.Extensions.FileRange, Microsoft.PowerShell.EditorServices]::new($Context.SelectedRange.Start, $Context.SelectedRange.end)
[string]$text = $Context.CurrentFile.GetText($selected)
$global:sel = $selected
write-host "unmasking $text [$($text.Length)]"
[string]$plain = _showAnswer $text
write-host "with $plain"
$context.CurrentFile.InsertText($plain)
}
Register-EditorCommand -Name UnmaskQuizAnswer -DisplayName "Unmask Quiz Answer" -Function UnMaskQuizAnswer
}
elseif ($host.name -eq 'Windows PowerShell ISE Host') {
#add an ISE menu shortcut
$sb = {
$utc = "{0:u}" -f (Get-Date).ToUniversalTime()
$PSIse.CurrentFile.Editor.InsertText($utc)
}
[void]$PSIse.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Insert Quiz UTC Date",$sb,$null)
} #>
}
Export-ModuleMember -Variable PSQuizPath -Alias "Start-PSQuiz","Make-PSQuiz"