Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tab history #4

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions src/engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<Info: TabInfo> Tab<Info> {

pub struct Tabs<Info: TabInfo> {
tabs: Vec<Tab<Info>>,
current: u32,
history: Vec<u32>,
}

impl<Info: TabInfo> Default for Tabs<Info> {
Expand All @@ -110,7 +110,7 @@ impl<Info: TabInfo> Tabs<Info> {
pub fn new() -> Self {
Self {
tabs: Vec::new(),
current: 0,
history: Vec::new(),
}
}

Expand All @@ -131,11 +131,14 @@ impl<Info: TabInfo> Tabs<Info> {
}

pub fn get_current_id(&self) -> u32 {
self.current
self.history
.last()
.expect("No tab in history to get current from")
.to_owned()
}

pub fn set_current_id(&mut self, id: u32) {
self.current = id
self.history.push(id)
}

pub fn tabs(&self) -> &Vec<Tab<Info>> {
Expand All @@ -150,26 +153,18 @@ impl<Info: TabInfo> Tabs<Info> {

/// Returns the newly active tab
pub fn remove(&mut self, id: u32) -> u32 {
// TODO: have list of prevous tabs instead
if self.current == id {
for tab in self.tabs.iter().rev() {
if tab.id != id {
self.current = tab.id;
break;
}
}
}
self.history.retain(|tab_id| *tab_id != id);

self.tabs.retain(|tab| tab.id != id);
self.current
self.get_current_id()
}

pub fn get_current(&self) -> &Tab<Info> {
self.get(self.current)
self.get(self.get_current_id())
}

pub fn get_current_mut(&mut self) -> &mut Tab<Info> {
self.get_mut(self.current)
self.get_mut(self.get_current_id())
}

pub fn get(&self, id: u32) -> &Tab<Info> {
Expand Down
Loading