1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use super::target_session::TargetSession;
use std::fmt;
#[derive(Debug, Default)]
pub struct TargetWindowExt<'a> {
pub session: Option<&'a TargetSession<'a>>,
pub window: Option<TargetWindow<'a>>,
}
impl<'a> TargetWindowExt<'a> {
pub fn new(target_window: &'a str) -> Self {
TargetWindowExt {
session: None,
window: Some(TargetWindow::StartName(target_window)),
}
}
pub fn token(session: Option<&'a TargetSession<'a>>, token: TargetWindowToken) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::Token(token)),
}
}
pub fn index(session: Option<&'a TargetSession<'a>>, i: usize) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::Index(i)),
}
}
pub fn id(session: Option<&'a TargetSession<'a>>, id: usize) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::Id(id)),
}
}
pub fn exact_name(session: Option<&'a TargetSession<'a>>, name: &'a str) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::ExactName(name)),
}
}
pub fn start_name(session: Option<&'a TargetSession<'a>>, name: &'a str) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::StartName(name)),
}
}
pub fn fn_match(session: Option<&'a TargetSession<'a>>, name: &'a str) -> Self {
TargetWindowExt {
session,
window: Some(TargetWindow::FnMatch(name)),
}
}
pub fn raw(name: &'a str) -> Self {
TargetWindowExt {
session: None,
window: Some(TargetWindow::Raw(name)),
}
}
}
impl<'a> fmt::Display for TargetWindowExt<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = String::new();
let mut w = String::new();
if let Some(ref session) = self.session {
s = session.to_string();
}
if let Some(ref window) = self.window {
w = window.to_string();
}
write!(f, "{}{}", s, w)
}
}
#[derive(Debug)]
pub enum TargetWindow<'a> {
Token(TargetWindowToken),
Index(usize),
Id(usize),
ExactName(&'a str),
StartName(&'a str),
FnMatch(&'a str),
Raw(&'a str),
}
impl<'a> Default for TargetWindow<'a> {
fn default() -> Self {
TargetWindow::Raw("")
}
}
impl<'a> fmt::Display for TargetWindow<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TargetWindow::Token(token) => write!(f, ":{}", token),
TargetWindow::Index(i) => write!(f, ":{}", i),
TargetWindow::Id(id) => write!(f, ":@{}", id),
TargetWindow::ExactName(name) => write!(f, ":={}", name),
TargetWindow::StartName(name) => write!(f, ":{}", name),
TargetWindow::FnMatch(name) => write!(f, ":{}", name),
TargetWindow::Raw(raw_str) => write!(f, "{}", raw_str),
}
}
}
#[derive(Debug)]
pub enum TargetWindowToken {
Start,
End,
Last,
Next(Option<usize>),
Previous(Option<usize>),
}
const TARGET_WINDOW_TOKEN_START: &str = "^";
const TARGET_WINDOW_TOKEN_END: &str = "$";
const TARGET_WINDOW_TOKEN_LAST: &str = "!";
const TARGET_WINDOW_TOKEN_NEXT: &str = "+";
const TARGET_WINDOW_TOKEN_PREVIOUS: &str = "-";
impl fmt::Display for TargetWindowToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let a;
let s = match self {
TargetWindowToken::Start => TARGET_WINDOW_TOKEN_START,
TargetWindowToken::End => TARGET_WINDOW_TOKEN_END,
TargetWindowToken::Last => TARGET_WINDOW_TOKEN_LAST,
TargetWindowToken::Next(offset) => {
if let Some(n) = offset {
a = format!("{}{}", TARGET_WINDOW_TOKEN_NEXT, n);
&a
} else {
TARGET_WINDOW_TOKEN_NEXT
}
}
TargetWindowToken::Previous(offset) => {
if let Some(n) = offset {
a = format!("{}{}", TARGET_WINDOW_TOKEN_PREVIOUS, n);
&a
} else {
TARGET_WINDOW_TOKEN_PREVIOUS
}
}
};
f.write_str(s)
}
}