Super simple text editing interface!
No cursor yet, just always appends to the end of the current buffer. But still!
This commit is contained in:
parent
9c4e11bc9c
commit
b78501f983
|
@ -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);
|
||||
|
|
76
src/main.rs
76
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();
|
||||
|
||||
for _ in range(0i, 100) {
|
||||
tb.insert_text(args.arg_file.as_slice(), 0);
|
||||
rustbox::init();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
let mut iterz = tb.root_iter();
|
||||
|
||||
iterz.skip_chars(3);
|
||||
|
||||
for c in iterz {
|
||||
print!("{}", c);
|
||||
if line > height {
|
||||
break;
|
||||
}
|
||||
println!("");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
_ => { }
|
||||
}
|
||||
}
|
||||
rustbox::shutdown();
|
||||
}
|
Loading…
Reference in New Issue
Block a user