Commit e478d9c8 authored by Wesley Wiser's avatar Wesley Wiser

Allow switching between buffers and listing all buffers

parent a662d900
use io::file::FileStatus;
use io::redraw::RedrawTask;
use state::editor::Editor;
use edit::buffer::{Buffer, SplitBuffer};
use std::process::exit;
......@@ -71,6 +72,20 @@ impl Editor {
FileStatus::Other => format!("Couldn't write {}", sec_cmd),
}
},
"ls" => {
fn print_buffer(i: usize, b: &SplitBuffer) -> String {
let title = b.get_title().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();
self.buffers.push(SplitBuffer::from_str(&buffer_descriptions.join("\n"), "<Buffers>"));
self.current_buffer_index = self.buffers.len() - 1;
self.redraw_task = RedrawTask::Full;
},
"help" => {
self.open("/apps/sodium/help.txt");
},
......
......@@ -30,7 +30,7 @@ pub trait Buffer<'a> {
fn new() -> Self;
/// Convert a string to a split buffer
fn from_str(s: &str) -> Self;
fn from_str(s: &str, title: &str) -> Self;
/// Get the nth line in the buffer by option reference
fn get_line(&self, n: usize) -> Option<&Self::Line>;
......@@ -61,6 +61,9 @@ pub trait Buffer<'a> {
/// Get the leading whitespaces of the nth line. Used for autoindenting.
fn get_indent(&self, n: usize) -> &str;
/// Get the title of the buffer
fn get_title(&self) -> Option<&str>;
}
......@@ -75,6 +78,7 @@ pub trait Buffer<'a> {
pub struct SplitBuffer {
before: Vec<String>,
after: Vec<String>,
title: Option<String>,
#[cfg(debug)]
_hinted_since_edit: bool,
}
......@@ -109,13 +113,15 @@ impl<'a> Buffer<'a> for SplitBuffer {
SplitBuffer {
before: vec![String::new()],
after: Vec::new(),
title: None,
}
}
fn from_str(s: &str) -> Self {
fn from_str(s: &str, title: &str) -> Self {
SplitBuffer {
before: s.lines().map(ToOwned::to_owned).collect(),
after: Vec::new(),
title: Some(title.to_owned()),
}
}
......@@ -167,6 +173,7 @@ impl<'a> Buffer<'a> for SplitBuffer {
SplitBuffer {
before: ln.to_owned(),
after: Vec::new(),
title: None,
}
}
......@@ -211,6 +218,10 @@ impl<'a> Buffer<'a> for SplitBuffer {
""
}
}
fn get_title(&self) -> Option<&str> {
self.title.as_ref().map(|s| s.as_str())
}
}
......
......@@ -21,7 +21,7 @@ impl Editor {
let mut con = String::new();
let _ = file.read_to_string(&mut con);
self.buffers.push(SplitBuffer::from_str(&con));
self.buffers.push(SplitBuffer::from_str(&con, path));
self.current_buffer_index = self.buffers.len() - 1;
self.hint();
FileStatus::Ok
......
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