-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCan-It.psm1
142 lines (122 loc) · 5.07 KB
/
Can-It.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function Read-CannedResponse {
param($cannedResponses, $cannedResponseName)
if ($null -eq $cannedResponseName) {
Write-Output "`nPlease specify either the name or ID of a canned response.`n`nRun ``can-it ls`` to list all canned responses.`n"
Exit
}
try {
$cannedResponseIndex = [int] $cannedResponseName
if ($cannedResponseIndex -gt 0 -and $cannedResponses.Length -ge $cannedResponseIndex) {
return $cannedResponses[$cannedResponseIndex - 1]
}
}
catch {
foreach ($item in $cannedResponses) {
if ($item.name -eq $cannedResponseName) {
return $item
}
}
}
Write-Output "`nCanned response '$cannedResponseName' does not exist.`n"
Exit
}
function Write-CannedResponse {
param($cannedResponseName, $cannedResponseBody)
$marginLeft = " " * [math]::floor(($Host.UI.RawUI.WindowSize.Width - $cannedResponseName.Length) / 2)
return "`n`n$marginLeft$cannedResponseName`n" + (Build-HorizontalRule) + "`n`n$cannedResponseBody`n`n" + (Build-HorizontalRule) + "`n`n"
}
function Build-HorizontalRule {
return ("-" * $Host.UI.RawUI.WindowSize.Width)
}
function Write-CannedResponses {
param($cannedResponses);
if ($null -eq $cannedResponses -or $cannedResponses.Length -eq 0) {
Write-Output "`nNo canned responses yet.`n"
}
else {
$cannedResponses | Format-Table `
@{Label="Index"; Expression={$cannedResponses.IndexOf($_) + 1;}; Align="center"}, `
@{Label="Name"; Expression={$_.name}}
}
}
function Show-CannedResponse {
param($cannedResponse)
Write-Output (Write-CannedResponse $cannedResponse.name $cannedResponse.body)
}
function Use-CannedResponse {
param($cannedResponse)
Clear-Host
Write-Output (Write-CannedResponse $cannedResponse.name $cannedResponse.body)
foreach ($field in $cannedResponse.fields) {
$fieldPrompt = $field
if ($null -ne $cannedResponse.defaults.$field) {
$cannedResponse.defaults.$field = $cannedResponse.defaults.$field.Trim()
$fieldPrompt += (" [" + $cannedResponse.defaults.$field + "]")
}
$fieldValue = (Read-Host $fieldPrompt).Trim()
if ($null -ne $cannedResponse.defaults.$field -and $fieldValue -eq "") {
$fieldValue = $cannedResponse.defaults.$field
}
$cannedResponse.body = $cannedResponse.body.Replace("{{ " + $field + " }}", $fieldValue)
Clear-Host
Write-Output (Write-CannedResponse $cannedResponse.name $cannedResponse.body)
}
Set-Clipboard -Value $cannedResponse.body
Write-Output "`u{2705} Copied to clipboard`n"
Start-Sleep -Seconds 2
}
<#
.SYNOPSIS
Can-It is a tool for using canned responses.
.DESCRIPTION
Can-It stores canned responses and provides several functions for using them.
Supported Actions
ls
Prints the names and index numbers of all canned responses from the config file.
peek [canned response]
Prints the specified canned response.
Provide the name or index number of a canned response as a parameter.
use [canned response]
Uses the specified canned response.
Provide the name or index number of a canned response as a parameter.
The canned response will be printed to the console, and you will be prompted
to provide a replacement value for each placeholder in it. As you fill in each
placeholder, the canned response will update to include what you entered.
Once you fill in the last placeholder, the complete canned response
will be copied to your clipboard.
.NOTES
There is currently no facility for adding or removing canned responses
through the command line interface. To add, remove, or reorder canned
responses, edit the config file directly.
The config file is named can-it.json, and it should be located in the
same directly as this script.
This module contains an example config file, example-can-it.json.
#>
function Invoke-CanIt {
[CmdletBinding()]
param([string]$Action, [Parameter(ValueFromPipeline)][string]$CannedResponse)
begin {
$configFilePath = "$PSScriptRoot\can-it.json"
if (-not (Test-Path -Path $configFilePath -PathType Leaf)) {
$null = New-Item -ItemType File -Path $configFilePath -Value '{"cannedResponses":[]}' -Force
}
$config = Get-Content $configFilePath | ConvertFrom-Json
}
process {
switch ($Action) {
"ls" {
Write-CannedResponses $config.cannedResponses
}
"peek" {
Show-CannedResponse (Read-CannedResponse $config.cannedResponses $CannedResponse)
}
"use" {
Use-CannedResponse (Read-CannedResponse $config.cannedResponses $CannedResponse)
}
default {
Write-Output 'Invalid action specified. Run `Help can-it` for additional information.'
}
}
}
}
Set-Alias "can-it" Invoke-CanIt