-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach-with.psm1
134 lines (114 loc) · 2.9 KB
/
attach-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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function Erase-With()
{
param(
[string]
$command
)
$splitted = $command -split ' '
if (!($splitted.Length -eq 1))
{
$splitted = $splitted[0..($splitted.Length - 2)]
}
return "$splitted"
}
function Move-With()
{
param(
[string]
$command
)
while($true)
{
Write-Host ''
Write-WithPrompt -command $command
$input_shell = (Read-Host -WarningAction SilentlyContinue)
if ($input_shell.ToLower() -eq 'quit' -or $input_shell.ToLower() -eq 'q')
{
return
}
elseif ($input_shell.StartsWith(':'))
{
if ($input_shell.Length -eq 1)
{
continue
}
Invoke-Expression "$($input_shell.Substring(1))"
}
elseif ($input_shell.StartsWith($command)) {
Write-Host " Your primary command is '$command'." -ForegroundColor DarkRed
Write-Host " Make sure to not duplicate the command." -ForegroundColor DarkRed
}
elseif ($input_shell.StartsWith('>'))
{
$addition = $input_shell.Substring(1).Trim()
if (!($addition -eq ''))
{
$command += " $($input_shell.Substring(1).Trim())"
}
}
elseif ($input_shell.StartsWith('<'))
{
$command = Erase-With -command $command
}
else
{
Invoke-Expression "$command $input_shell"
}
}
}
function global:Write-WithPrompt()
{
param(
[string]
$command
)
Write-ClassicPrompt -command $command
}
function Write-ClassicPrompt()
{
param(
[string]
$command
)
$colors = @(
'DarkGreen'
'DarkCyan'
'DarkRed'
'DarkMagenta'
'DarkYellow'
'Green'
'Cyan'
'Red'
'Magenta'
'Yellow'
'White'
)
$random_index_ps = Get-Random -Minimum 0 -Maximum ($colors.Count - 1)
$random_index_command = Get-Random -Minimum 0 -Maximum ($colors.Count - 1)
$random_color_command = $colors[$random_index_command]
$random_color_shell = $colors[$random_index_ps]
Write-Host " PS $pwd " -ForegroundColor $random_color_shell -NoNewline
Write-Host "$($command)" -ForegroundColor $random_color_command -NoNewline
Write-Host " " -NoNewline
}
function Start-With()
{
$command = $args
if (Validate-Command -command $command[0])
{
Move-With -command ($command.Trim())
}
else
{
Write-Host " $($command[0]): command not found" -ForegroundColor DarkRed
}
}
function Validate-Command()
{
param(
[string]
$command
)
return [bool](Get-Command -Name $command -ErrorAction SilentlyContinue)
}
Set-Alias attach Start-With