Skip to content

Commit b230c97

Browse files
JMjohn-michaelburke
JM
authored andcommitted
Prevent second terminal on Windows and attach console.
1 parent 4e71e74 commit b230c97

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

Cargo.lock

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console_backend/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ mimalloc = { version = "0.1", default-features = false }
4646
[target.'cfg(any(target_os = "macos", target_os = "windows"))'.dependencies]
4747
serialport = "4.0.1"
4848

49+
[target.'cfg(target_os = "windows")'.dependencies]
50+
windows = { version = ">=0.24", features = [
51+
"Win32_System_Console",
52+
"Win32_Foundation",
53+
] }
54+
4955
[dev-dependencies]
5056
logtest = "2.0.0"
5157
serial_test = "0.5.1"

console_backend/src/server.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ use crate::server_recv_thread::server_recv_thread;
1414
use crate::shared_state::SharedState;
1515
use crate::utils::{refresh_connection_frontend, refresh_loggingbar};
1616

17+
pub(crate) fn attach_console() {
18+
#[cfg(target_os = "windows")]
19+
{
20+
use windows::Win32::System::Console::AttachConsole;
21+
unsafe { AttachConsole(u32::MAX).as_bool() }
22+
}
23+
}
24+
1725
/// The backend server
1826
#[pyclass]
1927
struct Server {
@@ -101,6 +109,7 @@ impl Server {
101109

102110
#[text_signature = "($self, /)"]
103111
pub fn start(&mut self) -> PyResult<ServerEndpoint> {
112+
attach_console();
104113
let (client_send, client_recv) = channel::unbounded();
105114
let (server_send, server_recv) = channel::unbounded();
106115
let client_send = ChannelSender::boxed(client_send);

entrypoint/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ edition = "2018"
1212
name = "console"
1313
path = "src/main.rs"
1414
bench = false
15+
16+
[target.'cfg(target_os = "windows")'.dependencies]
17+
windows = { version = ">=0.24", features = [
18+
"Win32_System_Console",
19+
"Win32_Foundation",
20+
] }

entrypoint/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]
12
use std::env;
23
use std::path::{Path, PathBuf};
34
use std::process::Command;
45

56
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
67

8+
pub(crate) fn attach_console() {
9+
#[cfg(target_os = "windows")]
10+
{
11+
use windows::Win32::System::Console::AttachConsole;
12+
unsafe { AttachConsole(u32::MAX).as_bool() }
13+
}
14+
}
15+
716
#[cfg(target_os = "windows")]
817
fn find_py(dir: &Path) -> PathBuf {
918
dir.parent().expect("no parent dir").join("pythonw.exe")
@@ -47,5 +56,6 @@ fn main() -> Result<()> {
4756
let me = env::current_exe()?;
4857
let parent = me.parent().ok_or("no parent directory")?;
4958
let py = find_py(parent);
59+
attach_console();
5060
start(&py)
5161
}

0 commit comments

Comments
 (0)