1extern crate rustty;
2
3use std::time::Duration;
4
5use rustty::{Terminal, Event, Color};
6
7struct Cursor {
8 pos: Position,
9 lpos: Position,
10 color: Color,
11}
12
13#[derive(Copy, Clone)]
14struct Position {
15 x: usize,
16 y: usize,
17}
18
19fn main() {
20 let mut cursor = Cursor {
21 pos: Position { x: 0, y: 0 },
22 lpos: Position { x: 0, y: 0 },
23 color: Color::Red,
24 };
25 let mut term = Terminal::new().unwrap();
26 term[(cursor.pos.x, cursor.pos.y)].set_bg(cursor.color);
27 term.swap_buffers().unwrap();
28 loop {
29 let evt = term.get_event(Duration::from_millis(100)).unwrap();
30 if let Some(Event::Key(ch)) = evt {
31 match ch {
32 '`' => {
33 break;
34 }
35 '\x7f' => {
36 cursor.lpos = cursor.pos;
37 if cursor.pos.x == 0 {
38 cursor.pos.y = cursor.pos.y.saturating_sub(1);
39 } else {
40 cursor.pos.x -= 1;
41 }
42 term[(cursor.pos.x, cursor.pos.y)].set_ch(' ');
43 }
44 '\r' => {
45 cursor.lpos = cursor.pos;
46 cursor.pos.x = 0;
47 cursor.pos.y += 1;
48 }
49 c @ _ => {
50 term[(cursor.pos.x, cursor.pos.y)].set_ch(c);
51 cursor.lpos = cursor.pos;
52 cursor.pos.x += 1;
53 }
54 }
55 if cursor.pos.x >= term.cols() - 1 {
56 term[(cursor.lpos.x, cursor.lpos.y)].set_bg(Color::Default);
57 cursor.lpos = cursor.pos;
58 cursor.pos.x = 0;
59 cursor.pos.y += 1;
60 }
61 if cursor.pos.y >= term.rows() - 1 {
62 term[(cursor.lpos.x, cursor.lpos.y)].set_bg(Color::Default);
63 cursor.lpos = cursor.pos;
64 cursor.pos.x = 0;
65 cursor.pos.y = 0;
66 }
67 term[(cursor.lpos.x, cursor.lpos.y)].set_bg(Color::Default);
68 term[(cursor.pos.x, cursor.pos.y)].set_bg(cursor.color);
69 term.swap_buffers().unwrap();
70 }
71 }
72}