Skip to content

Commit 47943bd

Browse files
authored
feat: add thread sleep to prevent continuous retry (#46)
1 parent 0469a9c commit 47943bd

1 file changed

Lines changed: 52 additions & 36 deletions

File tree

src/get_active_window.rs

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use std::error::Error;
1313
use std::io::{BufRead, BufReader};
1414
use std::process::{Command, Stdio};
15+
use std::thread;
16+
use std::time::Duration;
1517

1618
enum WindowTitle {
1719
Code,
@@ -75,47 +77,61 @@ impl WindowTitle {
7577
}
7678

7779
pub fn get_active_window_process_and_title() -> Result<String, Box<dyn Error>> {
78-
let xprop_output = Command::new("xprop")
79-
.arg("-root")
80-
.arg("_NET_ACTIVE_WINDOW")
81-
.stdout(Stdio::piped())
82-
.spawn()?
83-
.stdout
84-
.ok_or("Failed to capture xprop stdout")?;
80+
let mut failure_count = 0;
8581

86-
let xprop_reader = BufReader::new(xprop_output);
87-
let mut window_id = String::new();
88-
for line in xprop_reader.lines() {
89-
let line = line?;
90-
if line.contains("_NET_ACTIVE_WINDOW(WINDOW)") {
91-
window_id = line.split_whitespace().nth(4).unwrap_or("").to_string();
92-
break;
82+
loop {
83+
let xprop_output = Command::new("xprop")
84+
.arg("-root")
85+
.arg("_NET_ACTIVE_WINDOW")
86+
.stdout(Stdio::piped())
87+
.spawn()?
88+
.stdout
89+
.ok_or("Failed to capture xprop stdout")?;
90+
91+
let xprop_reader = BufReader::new(xprop_output);
92+
let mut window_id = String::new();
93+
for line in xprop_reader.lines() {
94+
let line = line?;
95+
if line.contains("_NET_ACTIVE_WINDOW(WINDOW)") {
96+
window_id = line.split_whitespace().nth(4).unwrap_or("").to_string();
97+
break;
98+
}
9399
}
94-
}
95100

96-
if window_id.is_empty() {
97-
return Err("Failed to get active window ID".into());
98-
}
101+
if window_id.is_empty() {
102+
failure_count += 1;
103+
if failure_count >= 3 {
104+
thread::sleep(Duration::from_secs(1));
105+
failure_count = 0;
106+
}
107+
continue;
108+
}
99109

100-
let xprop_output = Command::new("xprop")
101-
.arg("-id")
102-
.arg(&window_id)
103-
.arg("WM_CLASS")
104-
.stdout(Stdio::piped())
105-
.spawn()?
106-
.stdout
107-
.ok_or("Failed to capture xprop stdout")?;
110+
let xprop_output = Command::new("xprop")
111+
.arg("-id")
112+
.arg(&window_id)
113+
.arg("WM_CLASS")
114+
.stdout(Stdio::piped())
115+
.spawn()?
116+
.stdout
117+
.ok_or("Failed to capture xprop stdout")?;
108118

109-
let xprop_reader = BufReader::new(xprop_output);
110-
for line in xprop_reader.lines() {
111-
let line = line?;
112-
if line.contains("WM_CLASS(STRING)") {
113-
let class_name = line.split('"').nth(1).unwrap_or("");
114-
println!("class_name: {}", class_name);
115-
let window_title_enum = WindowTitle::from_string(class_name);
116-
return Ok(window_title_enum.to_string());
119+
let xprop_reader = BufReader::new(xprop_output);
120+
for line in xprop_reader.lines() {
121+
let line = line?;
122+
if line.contains("WM_CLASS(STRING)") {
123+
let class_name = line.split('"').nth(1).unwrap_or("");
124+
println!("class_name: {}", class_name);
125+
let window_title_enum = WindowTitle::from_string(class_name);
126+
return Ok(window_title_enum.to_string());
127+
}
117128
}
118-
}
119129

120-
Err("Failed to get window class".into())
130+
failure_count += 1;
131+
if failure_count >= 3 {
132+
thread::sleep(Duration::from_secs(1));
133+
println!("It seems that the window is not found or it's not a valid window (Terminal).");
134+
failure_count = 0;
135+
}
136+
}
121137
}

0 commit comments

Comments
 (0)