use super::*;
pub struct NamedSession<S = DapSession> {
pub name: String,
pub session: S,
pub backlog: Vec<DapEvent>,
pub console: Vec<ConsoleStream>,
}
impl<S> NamedSession<S> {
pub fn console_stream(&mut self, category: &str) -> &mut ConsoleStream {
let at = match self.console.iter().position(|c| c.category == category) {
Some(at) => at,
None => {
self.console.push(ConsoleStream {
category: category.to_string(),
..ConsoleStream::default()
});
self.console.len() - 1
}
};
&mut self.console[at]
}
}
#[derive(Default)]
pub struct ConsoleStream {
pub category: String,
pub ansi: crate::ansi_text::AnsiStream,
pub shown_at: Option<usize>,
}
pub struct DebugSessions<S = DapSession> {
sessions: Vec<NamedSession<S>>,
focused: usize,
}
impl<S> Default for DebugSessions<S> {
fn default() -> Self {
Self {
sessions: Vec::new(),
focused: 0,
}
}
}
impl<S> DebugSessions<S> {
pub fn is_empty(&self) -> bool {
self.sessions.is_empty()
}
pub fn len(&self) -> usize {
self.sessions.len()
}
pub fn focused(&self) -> Option<&S> {
self.sessions.get(self.focused).map(|s| &s.session)
}
pub fn focused_name(&self) -> Option<&str> {
self.sessions.get(self.focused).map(|s| s.name.as_str())
}
pub fn focused_mut(&mut self) -> Option<&mut S> {
self.sessions.get_mut(self.focused).map(|s| &mut s.session)
}
pub fn replace_with(&mut self, name: impl Into<String>, session: S) {
self.sessions.clear();
self.sessions.push(NamedSession {
name: name.into(),
session,
backlog: Vec::new(),
console: Vec::new(),
});
self.focused = 0;
}
pub fn push(&mut self, name: impl Into<String>, session: S) {
self.sessions.push(NamedSession {
name: name.into(),
session,
backlog: Vec::new(),
console: Vec::new(),
});
self.focused = self.sessions.len() - 1;
}
pub fn focus(&mut self, index: usize) -> bool {
if index < self.sessions.len() {
self.focused = index;
true
} else {
false
}
}
pub fn names(&self) -> Vec<String> {
self.sessions.iter().map(|s| s.name.clone()).collect()
}
pub fn remove(&mut self, index: usize) -> Option<NamedSession<S>> {
if index >= self.sessions.len() {
return None;
}
let gone = self.sessions.remove(index);
if self.focused > index || self.focused >= self.sessions.len() {
self.focused = self.focused.saturating_sub(1);
}
Some(gone)
}
pub fn focused_index(&self) -> usize {
self.focused
}
pub fn iter_named_mut_indexed(
&mut self,
) -> impl Iterator<Item = (usize, &mut NamedSession<S>)> {
self.sessions.iter_mut().enumerate()
}
pub fn take_focused_backlog(&mut self) -> Vec<DapEvent> {
self.sessions
.get_mut(self.focused)
.map(|s| std::mem::take(&mut s.backlog))
.unwrap_or_default()
}
pub fn clear(&mut self) {
self.sessions.clear();
self.focused = 0;
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut S> {
self.sessions.iter_mut().map(|s| &mut s.session)
}
}