-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke-Terminal.psm1
91 lines (69 loc) · 2.23 KB
/
Invoke-Terminal.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
<#
.Synopsis
Launch Windows Terminal with a set of configuration options
.Description
.Parameter Config
Either a JSON configuration file held in Env:$profiledir (without extension) or a .\ local configuration file with extension
.Example
# Launch Windows Terminal from global config file.
Invoke-Terminal sample # Launch from global
.Example
# Launch Windows Terminal from local config file.
Invoke-Terminal .\sample.jsonc # Launch from local file
#>
function Invoke-Terminal {
param(
[string] $config
)
if ($config -match '^.\\') {
# Then it's a local config file.
$fullConfig = $config
} else {
$profiledir = $Env:invoketerminal
if (-Not $profiledir) {
Write-Error "Env:invoketerminal not configured, exiting."
Return
}
$fullConfig = -join((Join-Path -Path $profiledir $config), ".json")
}
# Test file exists
if (-not (Test-Path -Path $fullConfig -PathType Leaf)) {
if (-not (Test-Path -Path (-join($fullConfig, "c")) -PathType Leaf)) {
Write-Error "config file $fullConfig(c) not found, exiting."
Return
} else {
$fullConfig = -join($fullConfig, "c") # Handle jsonc
}
}
# Load json
$configo = Get-Content $fullConfig | ConvertFrom-Json
# Build windows terminal exec string
$psExec = ""
For($i = 0; $i -lt $configo.exec.Length; $i++) {
$exec = $configo.exec[$i];
$line = " "
if ($exec.split) {
$line = " split-pane -$($exec.split) "
}
if ($exec.movefocus) {
$line = " move-focus $($exec.movefocus) "
}
$line += "-p `"$($configo.env)`" "
if ($exec.dir) {
$dir = Join-Path -Path $configo.root -ChildPath $exec.dir
} else {
$dir = $configo.root
}
$line += " -d `"$dir`""
if ($exec.cmd) {
$line += " pwsh -noExit -Command `"$($exec.cmd)`""
}
$psExec += " $line "
if ($i -lt $configo.exec.Length - 1) {
$psExec += ';'
}
}
Write-Host $psExec
Start-Process wt.exe $psExec
}
Export-ModuleMember -Function Invoke-Terminal