use super::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)
}
}