|
12 | 12 | use std::error::Error; |
13 | 13 | use std::io::{BufRead, BufReader}; |
14 | 14 | use std::process::{Command, Stdio}; |
| 15 | +use std::thread; |
| 16 | +use std::time::Duration; |
15 | 17 |
|
16 | 18 | enum WindowTitle { |
17 | 19 | Code, |
@@ -75,47 +77,61 @@ impl WindowTitle { |
75 | 77 | } |
76 | 78 |
|
77 | 79 | 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; |
85 | 81 |
|
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 | + } |
93 | 99 | } |
94 | | - } |
95 | 100 |
|
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 | + } |
99 | 109 |
|
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")?; |
108 | 118 |
|
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 | + } |
117 | 128 | } |
118 | | - } |
119 | 129 |
|
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 | + } |
121 | 137 | } |
0 commit comments