Commit 9e50000e authored by Wesley Wiser's avatar Wesley Wiser

Add transient views which get deleted when no longer visible

parent 18fa5115
use io::file::FileStatus;
use io::redraw::RedrawTask;
use state::editor::{BufferInfo, Editor};
use state::editor::{BufferInfo, BufferManager, Editor};
use edit::buffer::{Buffer, SplitBuffer};
use std::process::exit;
......@@ -73,17 +73,10 @@ impl Editor {
}
},
"ls" => {
fn print_buffer(i: usize, b: &BufferInfo) -> String {
let title = b.title.as_ref().map(|s| s.as_str()).unwrap_or("<No Title>");
format!("b{}\t\t\t{}", i, title)
}
let buffer_descriptions : Vec<_> =
self.buffers.iter().enumerate().map(|(i, b)| print_buffer(i, b)).collect();
let mut new_buffer : BufferInfo = SplitBuffer::from_str(&buffer_descriptions.join("\n")).into();
let description = get_buffers_description(&self.buffers);
let mut new_buffer : BufferInfo = SplitBuffer::from_str(&description).into();
new_buffer.title = Some("<Buffers>".into());
new_buffer.is_transient = true; //delete the buffer when the user switches away
let new_buffer_index = self.buffers.new_buffer(new_buffer);
self.buffers.switch_to(new_buffer_index);
......@@ -117,3 +110,23 @@ impl Editor {
self.hint();
}
}
fn get_buffers_description(buffers: &BufferManager) -> String {
fn print_buffer(i: usize, b: &BufferInfo) -> String {
let title = b.title.as_ref().map(|s| s.as_str()).unwrap_or("<No Title>");
format!("b{}\t\t\t{}", i, title)
}
let descriptions =
buffers
.iter()
// don't include transient buffers like the one
// this is going to be shown in
.filter(|b| !b.is_transient)
.enumerate()
.map(|(i, b)| print_buffer(i, b))
.collect::<Vec<_>>();
descriptions.join("\n")
}
......@@ -27,6 +27,9 @@ pub struct BufferInfo {
pub scroll_y: usize,
/// The title of the document
pub title: Option<String>,
/// True if the buffer is transient and should be deleted when
/// it is no longer the current buffer.
pub is_transient: bool,
}
impl BufferInfo {
......@@ -39,6 +42,7 @@ impl BufferInfo {
scroll_x: 0,
scroll_y: 0,
title: None,
is_transient: false,
}
}
}
......@@ -109,8 +113,35 @@ impl BufferManager {
pub fn switch_to(&mut self, n: usize) {
assert!(n < self.buffers.len(), "Buffer index out of bounds");
// if the current view is transient, delete it
let mut n = n;
if self.current_buffer_info().is_transient {
let index = self.current_buffer_index;
self.delete_buffer(index);
// if the current view is less than the view to switch to
// then we need to account for the view we just removed
if index <= n {
n -= 1;
}
}
self.current_buffer_index = n;
}
/// Delete the specified buffer
pub fn delete_buffer(&mut self, n: usize) {
assert!(n < self.buffers.len(), "Buffer index out of bounds");
self.buffers.remove(n);
if self.buffers.len() == 0 {
self.buffers.push(BufferInfo::new());
self.current_buffer_index = 0;
} else if self.current_buffer_index <= n {
self.current_buffer_index -= 1;
}
}
}
/// The current state of the editor, including the file, the cursor, the scrolling info, etc.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment