From b78501f98396674f25e463c5b0bd6796be372837 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 14 Dec 2014 21:06:48 -0800 Subject: [PATCH] Super simple text editing interface! No cursor yet, just always appends to the end of the current buffer. But still! --- src/buffer.rs | 7 ++++- src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 5d3153a..43be6cd 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -40,6 +40,7 @@ fn char_and_newline_count(text: &str) -> (uint, uint) { fn char_pos_to_byte_pos(text: &str, pos: uint) -> uint { let mut i: uint = 0; + for (offset, _) in text.char_indices() { if i == pos { return offset; @@ -47,6 +48,10 @@ fn char_pos_to_byte_pos(text: &str, pos: uint) -> uint { i += 1; } + if i == pos { + return text.len(); + } + panic!("char_pos_to_byte_pos(): char position off the end of the string."); } @@ -85,7 +90,7 @@ impl TextBlock { /// Insert 'text' at char position 'pos'. pub fn insert_text(&mut self, text: &str, pos: uint) { // Find insertion position in bytes - let byte_pos = char_pos_to_byte_pos(text, pos); + let byte_pos = char_pos_to_byte_pos(self.as_str(), pos); // Grow data size self.data.grow(text.len(), 0); diff --git a/src/main.rs b/src/main.rs index 7e9d8a7..26fa589 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,10 @@ extern crate rustbox; extern crate docopt; extern crate serialize; +use std::char; use docopt::Docopt; use buffer::TextBuffer; +use rustbox::{Style,Color}; mod buffer; @@ -30,19 +32,75 @@ fn main() { // Get command-line arguments let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); + let mut width = rustbox::width(); + let mut height = rustbox::height(); + let mut tb = TextBuffer::new(); + + rustbox::init(); - for _ in range(0i, 100) { - tb.insert_text(args.arg_file.as_slice(), 0); + loop { + // Draw the text buffer to screen + rustbox::clear(); + { + let mut tb_iter = tb.root_iter(); + let mut line: uint = 0; + let mut column: uint = 0; + + loop { + if let Option::Some(c) = tb_iter.next() { + if c == '\n' { + line += 1; + column = 0; + continue; + } + rustbox::print(column, line, Style::Bold, Color::White, Color::Black, c.to_string()); + column += 1; + } + else { + break; + } + + if line > height { + break; + } + + if column > width { + tb_iter.next_line(); + line += 1; + column = 0; + } + } + } + rustbox::present(); + + // Handle events + match rustbox::poll_event() { + rustbox::Event::KeyEvent(_, _, ch) => { + match char::from_u32(ch) { + Some('q') => { break; }, + + Some('w') => { + let p = tb.len(); + tb.insert_text("\n", p); + }, + + Some(c) => { + let p = tb.len(); + tb.insert_text(c.to_string().as_slice(), p); + }, + + _ => {} + } + }, + + rustbox::Event::ResizeEvent(w, h) => { + width = w as uint; + height = h as uint; + } + + _ => { } + } } - - let mut iterz = tb.root_iter(); - - iterz.skip_chars(3); - - for c in iterz { - print!("{}", c); - } - println!(""); - -} \ No newline at end of file + rustbox::shutdown(); +}