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
use crate::variables::session::session::{SESSION_VARS, SESSION_VARS_SEPARATOR};
use crate::Error;
use crate::ListSessions;
use crate::Session;
use std::ops::Index;
#[derive(Default, Clone, PartialEq, Debug)]
pub struct Sessions(pub Vec<Session>);
impl IntoIterator for Sessions {
type Item = Session;
type IntoIter = ::std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Index<usize> for Sessions {
type Output = Session;
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
impl Sessions {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn push(&mut self, session: Session) {
self.0.push(session);
}
pub fn get(bitflags: u32) -> Result<Self, Error> {
let ls_format = SESSION_VARS
.iter()
.filter(|t| bitflags & t.1 == t.1)
.map(|t| format!("#{{{}}}", t.0))
.collect::<Vec<String>>()
.join(SESSION_VARS_SEPARATOR);
let output = ListSessions::new().format(&ls_format).output()?;
let sessions_str = String::from_utf8_lossy(&output.0.stdout.as_slice());
Sessions::from_str(&sessions_str, bitflags)
}
pub fn from_str(sessions_str: &str, bitflags: u32) -> Result<Self, Error> {
let mut sessions = Sessions::new();
for line in sessions_str.lines() {
sessions.push(Session::from_str(line, bitflags)?);
}
Ok(sessions)
}
}