2929def win_to_wsl_path (p : str ) -> str :
3030 """
3131 Convert a Windows path (e.g., C:\\ cases\\ demo) into a WSL path (/mnt/c/cases/demo).
32- This is intentionally minimal and does not resolve symlinks.
3332 """
3433 p = str (p ).replace ("\\ " , "/" )
3534 if len (p ) >= 2 and p [1 ] == ":" :
@@ -48,30 +47,13 @@ def wsl_to_win_path(p: str) -> str:
4847 return p
4948
5049
51- # Terminal detection
52- def _detect_terminal () -> Tuple [str , list [str ]]:
53- """
54- Decide how to open a *new* console window on Windows.
55- Returns (launcher_name, argv_prefix).
56-
57- We prefer Windows Terminal (wt.exe). If not present, fall back to classic console
58- via `cmd.exe /c start`.
59- """
60- wt = shutil .which ("wt.exe" )
61- if wt :
62- # Use Windows Terminal. We'll append: -w 0 nt (open a new tab in window 0)
63- return "wt" , [wt , "-w" , "0" , "nt" ]
64- # Fallback: classic console via `start`.
65- return "cmd" , ["cmd.exe" , "/c" , "start" , "" ]
66-
67-
6850# Core runner
6951def run_wsl_console (
7052 command : str ,
7153 * ,
7254 cwd_wsl : Optional [str ] = None ,
7355 foam_bashrc : Optional [str ] = "/opt/openfoam10/etc/bashrc" ,
74- distro : Optional [str ] = None , # e.g. "Ubuntu-22 .04" (match your WT profile name if you want the same icon/theme)
56+ distro : Optional [str ] = None , # e.g. "Ubuntu-20 .04"
7557 log_rel : Optional [str ] = None , # e.g. "system/blockMesh.run.log"
7658 timeout : Optional [int ] = None ,
7759 keep_open : bool = True ,
@@ -96,10 +78,9 @@ def run_wsl_console(
9678 if cwd_wsl :
9779 prefix .append (f'cd { shlex .quote (cwd_wsl )} ' )
9880
99- # Build the core command with real-time logging behavior when requested.
81+ # Build the core command with real-time logging behavior when requested
10082 core = command
10183 if log_rel :
102- # Prefer `script` (PTY) for true progressive output; otherwise force line-buffering.
10384 quoted_cmd = shlex .quote (command )
10485 quoted_log = shlex .quote (log_rel )
10586 core = (
@@ -110,38 +91,26 @@ def run_wsl_console(
11091 )
11192
11293 if keep_open :
113- # Keep the console window around so users can read the tail/end of output.
114- core += r"; echo; echo '[Done] Press any key to close...'; read -n1 -s -r"
94+ # Keep the console window to read the output
95+ core += r" && echo && echo '[Done] Press any key to close...' && read -n1 -s -r </dev/tty "
11596
11697 inner = core
117- # Source OpenFOAM bashrc if provided. Silence errors so missing files won't break execution.
98+ # Source OpenFOAM bashrc if provided
11899 if foam_bashrc :
119100 inner = f'source "{ foam_bashrc } " >/dev/null 2>&1 || true; ' + inner
120101
121102 if prefix :
122- inner = "; " .join (prefix ) + "; " + inner
103+ inner = " && " .join (prefix ) + " && " + inner
123104
124- # Build the WSL invocation that actually runs our bash payload.
105+ # Build the WSL invocation
125106 wsl_argv = ["wsl.exe" ]
126107 if distro :
127108 wsl_argv += ["-d" , distro ]
128- wsl_argv += ["bash" , "-lc" , inner ]
129-
130- # Decide which terminal to use.
131- launcher , prefix_argv = _detect_terminal ()
132-
133- if launcher == "wt" :
134- wt_args = list (prefix_argv ) # [wt.exe, -w, 0, nt]
135- if distro :
136- wt_args += ["-p" , distro ]
137- wt_args += ["-e" ] + wsl_argv # run wsl.exe ... bash -lc "<inner>"
109+ wsl_argv += ["--" , "bash" , "-lc" , inner ]
138110
139- # Launch a new tab/window
140- proc = subprocess .Popen (wt_args , creationflags = CREATE_NEW_CONSOLE )
141- else :
142- # Classic console fallback: `cmd.exe /c start "" wsl.exe ...`
143- argv = prefix_argv + wsl_argv
144- proc = subprocess .Popen (argv , creationflags = CREATE_NEW_CONSOLE )
111+ # `cmd.exe /c start "" wsl.exe ...`
112+ argv = ["cmd.exe" , "/c" , "start" , "" , * wsl_argv ]
113+ proc = subprocess .Popen (argv , creationflags = CREATE_NEW_CONSOLE )
145114
146115 try :
147116 return proc .wait (timeout = timeout )
0 commit comments