Commit 640f37db authored by Ticki's avatar Ticki

;w command

parent 41e4be8f
......@@ -48,10 +48,10 @@ pub trait Buffer<'a> {
/// Give a hint on where the operations are most frequent (i.e. where the cursor is). Y value.
fn focus_hint_y(&mut self, y: usize);
/// Get the number of lines in the buffer
/// Get the number of lines in the buffer.
fn len(&self) -> usize;
/// Get an iterator over the lines in the buffer
/// Get an iterator over the lines in the buffer.
fn lines(&'a self) -> Self::LineIter;
/// Get the leading whitespaces of the nth line. Used for autoindenting.
......@@ -88,6 +88,13 @@ impl SplitBuffer {
}
}
// TODO remove
impl SplitBuffer {
pub fn to_string(&self) -> String {
self.lines().map(|x| x.to_owned() + "\n").collect()
}
}
impl<'a> Buffer<'a> for SplitBuffer {
type Line = String;
type LineIter = SplitBufIter<'a>;
......
use buffer::{Buffer, SplitBuffer};
use editor::Editor;
use std::fs::File;
use std::io::Read;
use std::io::{Read, Write};
pub enum OpenStatus {
pub enum FileStatus {
Ok,
NotFound,
Other,
}
impl Editor {
/// Open a file
pub fn open(&mut self, path: &str) -> OpenStatus {
/// Open a file.
pub fn open(&mut self, path: &str) -> FileStatus {
self.status_bar.file = path.to_string();
if let Some(mut file) = File::open(path).ok() {
let mut con = String::new();
......@@ -18,9 +19,23 @@ impl Editor {
self.buffer = SplitBuffer::from_str(&con);
self.hint();
OpenStatus::Ok
FileStatus::Ok
} else {
OpenStatus::NotFound
FileStatus::NotFound
}
}
/// Write the file.
pub fn write(&mut self, path: &str) -> FileStatus {
self.status_bar.file = path.to_string();
if let Some(mut file) = File::create(path).ok() {
if file.write(self.buffer.to_string().as_bytes()).is_ok() {
FileStatus::Ok
} else {
FileStatus::Other
}
} else {
FileStatus::NotFound
}
}
}
#![feature(stmt_expr_attributes)]
#![deny(warnings)]
#![deny(missing_docs)]
#[cfg(feature = "orbital")]
extern crate orbclient;
......@@ -12,6 +13,7 @@ pub mod cursor;
pub mod delete;
pub mod editor;
pub mod exec;
pub mod file;
pub mod graphics;
pub mod insert;
pub mod invert;
......@@ -20,7 +22,6 @@ pub mod key_state;
pub mod mode;
pub mod motion;
pub mod movement;
pub mod open;
pub mod options;
pub mod parse;
pub mod position;
......
use editor::Editor;
use open::OpenStatus;
use file::FileStatus;
use std::process::exit;
......@@ -38,8 +38,16 @@ impl Editor {
},
"o" | "open" => {
self.status_bar.msg = match self.open(sec_cmd) {
OpenStatus::NotFound => format!("File {} could not be opened", sec_cmd),
OpenStatus::Ok => format!("File {} opened", sec_cmd),
FileStatus::NotFound => format!("File {} could not be opened", sec_cmd),
FileStatus::Ok => format!("File {} opened", sec_cmd),
_ => unreachable!(),
}
},
"w" | "write" => {
self.status_bar.msg = match self.write(sec_cmd) {
FileStatus::NotFound => format!("File {} could not be opened", sec_cmd),
FileStatus::Ok => format!("File {} written", sec_cmd),
FileStatus::Other => format!("Couldn't write {}", sec_cmd),
}
},
"help" => {
......
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