forked from mchav/with
-
Notifications
You must be signed in to change notification settings - Fork 2
/
posh-with.psm1
113 lines (100 loc) · 2.39 KB
/
posh-with.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
function Remove-With()
{
param(
[string]
$command
)
$splitted = $command -split ' '
if (!($splitted.Length -eq 1))
{
$splitted = $splitted[0..($splitted.Length - 2)]
}
return "$splitted"
}
function Start-With()
{
param(
[string]
$command
)
while($true)
{
Write-Host ''
Write-WithPrompt -command $command
$input = [Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($host.Runspace, $ExecutionContext)
if ($input.ToLower() -eq ':q')
{
return
}
elseif ($input.StartsWith(':'))
{
if ($input.Length -eq 1)
{
continue
}
Invoke-Expression "$($input.Substring(1))"
}
elseif ($input.StartsWith($command)) {
# let the user know the command is duplicated
Write-Host "Your expression starts with '$command'"
Write-Host 'When using with, make sure to not duplicate the expression.'
Write-Host "Continuing assuming you meant '$($input.Substring($command.Length).Trim())'"
Invoke-Expression "$input"
}
elseif ($input.StartsWith('>'))
{
$addition = $input.Substring(1).Trim()
if (!($addition -eq ''))
{
$command += " $($input.Substring(1).Trim())"
}
}
elseif ($input.StartsWith('<'))
{
$command = Remove-With -command $command
}
else
{
Invoke-Expression "$command $input"
}
}
}
function global:Write-WithPrompt()
{
param(
[string]
$command
)
Write-ClassicPrompt -command $command
}
function Write-ClassicPrompt()
{
param(
[string]
$command
)
Write-Host "PS $pwd " -NoNewline
Write-Host "$($command.ToUpper())" -ForegroundColor Yellow -NoNewline
Write-Host "> " -NoNewline
}
function Invoke-With()
{
$command = $args
if (Test-Command -command $command[0])
{
Start-With -command ($command.Trim())
}
else
{
Write-Host "$($command[0]): command not found"
}
}
function Test-Command()
{
param(
[string]
$command
)
return [bool](Get-Command -Name $command -ErrorAction SilentlyContinue)
}
Set-Alias with Invoke-With