-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_tab_sameWindow.exs
More file actions
executable file
·77 lines (60 loc) · 1.71 KB
/
launch_tab_sameWindow.exs
File metadata and controls
executable file
·77 lines (60 loc) · 1.71 KB
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
#!/usr/bin/env elixir
# Launch a new tab in the current Windows Terminal window.
# Opens WSL to a specified directory.
defmodule LaunchTabSameWindow do
def run do
wt_path = locate!("wt.exe")
# Get the directory from command line args, default to ~/p/g/n
dir = parse_directory(System.argv())
IO.puts("Opening new tab in current window...")
IO.puts("Directory: #{dir}")
# Build the command to open a new tab
args = [
# Target the last active window
"-w",
"last",
"new-tab",
"--startingDirectory",
dir
]
IO.puts("Command: #{format_invocation(wt_path, args)}")
{output, status} = System.cmd(wt_path, args, stderr_to_stdout: true)
if status == 0 do
IO.puts("✓ Tab opened successfully")
else
IO.puts(:stderr, "✗ Failed to open tab (exit status #{status})")
if String.trim(output) != "" do
IO.puts(:stderr, "Output: #{String.trim(output)}")
end
System.halt(1)
end
end
defp parse_directory([]), do: "~/p/g/n"
defp parse_directory([dir | _]), do: dir
defp locate!(executable) do
case System.find_executable(executable) do
nil ->
IO.puts(
:stderr,
"#{executable} not found on PATH. Install Windows Terminal or expose #{executable} to WSL."
)
System.halt(1)
path ->
path
end
end
defp format_invocation(cmd, args) do
[cmd | args]
|> Enum.map("e_for_display/1)
|> Enum.join(" ")
end
defp quote_for_display(arg) when is_binary(arg) do
if Regex.match?(~r/[\s"]/u, arg) do
escaped = String.replace(arg, "\"", "\\\"")
~s("#{escaped}")
else
arg
end
end
end
LaunchTabSameWindow.run()