Skip to content

Commit 1e28062

Browse files
committed
feat: add Windows console title update functionality and VSCode configuration files
1 parent 34ef1a2 commit 1e28062

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "LLBot (F5)",
6+
"type": "lldb",
7+
"request": "launch",
8+
"preLaunchTask": "cargo build",
9+
"program": "${workspaceFolder}\\target\\debug\\llbot.exe",
10+
"args": [],
11+
"cwd": "${workspaceFolder}"
12+
}
13+
]
14+
}

.vscode/tasks.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "cargo build",
6+
"type": "shell",
7+
"command": "cargo",
8+
"args": ["build"],
9+
"problemMatcher": ["$rustc"],
10+
"group": "build"
11+
}
12+
]
13+
}

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tar = "0.4"
2121

2222
[target.'cfg(target_os = "windows")'.dependencies]
2323
winreg = "0.55"
24-
winapi = { version = "0.3", features = ["jobapi2", "winnt", "handleapi", "processthreadsapi"] }
24+
winapi = { version = "0.3", features = ["jobapi2", "winnt", "handleapi", "processthreadsapi", "wincon"] }
2525

2626
[target.'cfg(target_os = "windows")'.build-dependencies]
2727
winres = "0.1"

src/main.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ use std::sync::{Arc, Mutex};
1818
use std::thread;
1919
use std::time::Duration;
2020

21+
#[cfg(target_os = "windows")]
22+
use std::ffi::OsStr;
23+
24+
#[cfg(target_os = "windows")]
25+
use std::os::windows::ffi::OsStrExt;
26+
27+
#[cfg(target_os = "windows")]
28+
use winapi::um::wincon::SetConsoleTitleW;
29+
2130
const DEFAULT_PORT: u16 = 13000;
2231
const PORT_RANGE_END: u16 = 14000;
2332
const QQ_DOWNLOAD_URL: &str = "https://dldir1v6.qq.com/qqfile/qq/QQNT/c50d6326/QQ9.9.22.40768_x64.exe";
@@ -324,6 +333,9 @@ fn start_login_listener(
324333
thread::spawn(move || {
325334
let client = PMHQClient::new(port).with_timeout(Duration::from_secs(10));
326335

336+
#[cfg(target_os = "windows")]
337+
start_windows_selfinfo_title_thread(client.clone());
338+
327339
thread::sleep(Duration::from_secs(3));
328340

329341
let logged_in_refresh = logged_in.clone();
@@ -381,6 +393,40 @@ fn start_login_listener(
381393
});
382394
}
383395

396+
#[cfg(target_os = "windows")]
397+
fn start_windows_selfinfo_title_thread(client: PMHQClient) {
398+
thread::spawn(move || {
399+
// pmhq 在登录完成后仍可能需要一点时间才能返回完整 SelfInfo
400+
loop {
401+
match client.get_self_info() {
402+
Ok(info) => {
403+
if !info.uin.is_empty() && !info.nickname.is_empty() {
404+
let title = format!("LLBot - {}({})", info.nickname, info.uin);
405+
let _ = set_windows_console_title(&title);
406+
break;
407+
}
408+
}
409+
Err(_) => {
410+
// 忽略未就绪/临时错误,继续重试
411+
}
412+
}
413+
414+
thread::sleep(Duration::from_secs(1));
415+
}
416+
});
417+
}
418+
419+
#[cfg(target_os = "windows")]
420+
fn set_windows_console_title(title: &str) -> Result<(), String> {
421+
let wide: Vec<u16> = OsStr::new(title).encode_wide().chain(std::iter::once(0)).collect();
422+
let ok = unsafe { SetConsoleTitleW(wide.as_ptr()) };
423+
if ok == 0 {
424+
Err("SetConsoleTitleW 调用失败".to_string())
425+
} else {
426+
Ok(())
427+
}
428+
}
429+
384430
fn migrate_old_files(exe_dir: &Path) {
385431
// 迁移 data 目录
386432
let data_dir = exe_dir.join("data");

0 commit comments

Comments
 (0)