pub struct Term {
pub cursor: Cursor,
pub matrix: Matrix,
pub terminfo: TermInfo,
pub xlimit: Option<usize>,
pub fg: Vec<(u8, u8, u8)>,
pub bg: Vec<(u8, u8, u8)>,
/* private fields */
}
Expand description
The main struct of cursormatrix crate
Example usage:
use cursormatrix::{Direction, Event, Input, Term};
fn handle_event(ev: &Event, term: &mut Term) -> bool {
match ev {
&Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
"C" => return false,
_ => (),
},
&Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
&Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
&Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
&Event::Raw(Input::Chars(ref s)) => term.print(&s).unwrap(),
&Event::TermSize(w, h) => term.matrix.refresh(w, h),
_ => (),
}
true
}
fn main() {
let (mut term, erx) = Term::with_input(true).expect("term");
term.print("edit").unwrap();
loop {
if match erx.recv() {
Ok(ev) => !handle_event(&ev, &mut term),
Err(_) => false,
} {
break;
}
}
}
Fields§
§cursor: Cursor
§matrix: Matrix
§terminfo: TermInfo
§xlimit: Option<usize>
§fg: Vec<(u8, u8, u8)>
§bg: Vec<(u8, u8, u8)>
Implementations§
Source§impl Term
impl Term
pub fn from_cjk(cjk: bool) -> Result<Self, Error>
Sourcepub fn with_input(cjk: bool) -> Result<(Self, Receiver<Event>), Error>
pub fn with_input(cjk: bool) -> Result<(Self, Receiver<Event>), Error>
Examples found in repository?
examples/debug.rs (line 38)
37fn main() {
38 let (mut term, erx) = Term::with_input(true).expect("term");
39
40 term.print("edit").unwrap();
41 loop {
42 if match erx.recv() {
43 Ok(ev) => !handle_event(&ev, &mut term),
44 Err(_) => false,
45 } {
46 break;
47 }
48 }
49}
More examples
Sourcepub fn clear(&mut self) -> Result<(), Error>
pub fn clear(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 7)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
pub fn width_char(&self, c: char) -> usize
Sourcepub fn width_str(&self, s: &str) -> usize
pub fn width_str(&self, s: &str) -> usize
Examples found in repository?
examples/filter.rs (line 105)
77 pub fn draw(&mut self) -> Result<(), std::io::Error> {
78 let width = self.term.matrix.width;
79 let blank = " ".repeat(width);
80 self.view = self.data
81 .clone()
82 .into_iter()
83 .enumerate()
84 .filter(|(_, e)| e.data.find(&self.query).is_some())
85 .collect();
86 self.term.cursor.hide()?;
87 self.term.xlimit = Some(width);
88 self.term.bg.push((33, 33, 33));
89 self.term.move_to(0, 0)?;
90 self.term.print(&format!("> {}{}", self.query, blank))?;
91 for l in 0..self.term.matrix.height - 1 {
92 self.term.move_to(0, l + 1)?;
93 match self.view.get(l) {
94 Some((_, e)) => {
95 let bg = if l == self.line as usize {
96 Some(if e.selected { (154, 205, 50) } else { (0, 128, 128) })
97 } else {
98 Some(if e.selected { Some((255, 99, 71)) } else { None }).flatten()
99 };
100 self.term.cprint(&format!("{}{}", e.data, blank), None, bg)?
101 },
102 None => self.term.print(&blank)?,
103 }
104 }
105 self.term.move_to(self.term.width_str(&self.query) + 2, 0)?;
106 self.term.cursor.show()
107 }
Sourcepub fn print(&mut self, s: &str) -> Result<(), Error>
pub fn print(&mut self, s: &str) -> Result<(), Error>
Examples found in repository?
examples/filter.rs (line 90)
77 pub fn draw(&mut self) -> Result<(), std::io::Error> {
78 let width = self.term.matrix.width;
79 let blank = " ".repeat(width);
80 self.view = self.data
81 .clone()
82 .into_iter()
83 .enumerate()
84 .filter(|(_, e)| e.data.find(&self.query).is_some())
85 .collect();
86 self.term.cursor.hide()?;
87 self.term.xlimit = Some(width);
88 self.term.bg.push((33, 33, 33));
89 self.term.move_to(0, 0)?;
90 self.term.print(&format!("> {}{}", self.query, blank))?;
91 for l in 0..self.term.matrix.height - 1 {
92 self.term.move_to(0, l + 1)?;
93 match self.view.get(l) {
94 Some((_, e)) => {
95 let bg = if l == self.line as usize {
96 Some(if e.selected { (154, 205, 50) } else { (0, 128, 128) })
97 } else {
98 Some(if e.selected { Some((255, 99, 71)) } else { None }).flatten()
99 };
100 self.term.cprint(&format!("{}{}", e.data, blank), None, bg)?
101 },
102 None => self.term.print(&blank)?,
103 }
104 }
105 self.term.move_to(self.term.width_str(&self.query) + 2, 0)?;
106 self.term.cursor.show()
107 }
More examples
examples/debug.rs (line 11)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
36
37fn main() {
38 let (mut term, erx) = Term::with_input(true).expect("term");
39
40 term.print("edit").unwrap();
41 loop {
42 if match erx.recv() {
43 Ok(ev) => !handle_event(&ev, &mut term),
44 Err(_) => false,
45 } {
46 break;
47 }
48 }
49}
pub fn push_colors(&mut self, fg: (u8, u8, u8), bg: (u8, u8, u8))
pub fn pop_colors( &mut self, fg: bool, bg: bool, ) -> (Option<(u8, u8, u8)>, Option<(u8, u8, u8)>)
Sourcepub fn cprint(
&mut self,
s: &str,
fg: Option<(u8, u8, u8)>,
bg: Option<(u8, u8, u8)>,
) -> Result<(), Error>
pub fn cprint( &mut self, s: &str, fg: Option<(u8, u8, u8)>, bg: Option<(u8, u8, u8)>, ) -> Result<(), Error>
Examples found in repository?
examples/filter.rs (line 100)
77 pub fn draw(&mut self) -> Result<(), std::io::Error> {
78 let width = self.term.matrix.width;
79 let blank = " ".repeat(width);
80 self.view = self.data
81 .clone()
82 .into_iter()
83 .enumerate()
84 .filter(|(_, e)| e.data.find(&self.query).is_some())
85 .collect();
86 self.term.cursor.hide()?;
87 self.term.xlimit = Some(width);
88 self.term.bg.push((33, 33, 33));
89 self.term.move_to(0, 0)?;
90 self.term.print(&format!("> {}{}", self.query, blank))?;
91 for l in 0..self.term.matrix.height - 1 {
92 self.term.move_to(0, l + 1)?;
93 match self.view.get(l) {
94 Some((_, e)) => {
95 let bg = if l == self.line as usize {
96 Some(if e.selected { (154, 205, 50) } else { (0, 128, 128) })
97 } else {
98 Some(if e.selected { Some((255, 99, 71)) } else { None }).flatten()
99 };
100 self.term.cprint(&format!("{}{}", e.data, blank), None, bg)?
101 },
102 None => self.term.print(&blank)?,
103 }
104 }
105 self.term.move_to(self.term.width_str(&self.query) + 2, 0)?;
106 self.term.cursor.show()
107 }
Sourcepub fn move_to(&mut self, x: usize, y: usize) -> Result<(), Error>
pub fn move_to(&mut self, x: usize, y: usize) -> Result<(), Error>
Examples found in repository?
examples/filter.rs (line 89)
77 pub fn draw(&mut self) -> Result<(), std::io::Error> {
78 let width = self.term.matrix.width;
79 let blank = " ".repeat(width);
80 self.view = self.data
81 .clone()
82 .into_iter()
83 .enumerate()
84 .filter(|(_, e)| e.data.find(&self.query).is_some())
85 .collect();
86 self.term.cursor.hide()?;
87 self.term.xlimit = Some(width);
88 self.term.bg.push((33, 33, 33));
89 self.term.move_to(0, 0)?;
90 self.term.print(&format!("> {}{}", self.query, blank))?;
91 for l in 0..self.term.matrix.height - 1 {
92 self.term.move_to(0, l + 1)?;
93 match self.view.get(l) {
94 Some((_, e)) => {
95 let bg = if l == self.line as usize {
96 Some(if e.selected { (154, 205, 50) } else { (0, 128, 128) })
97 } else {
98 Some(if e.selected { Some((255, 99, 71)) } else { None }).flatten()
99 };
100 self.term.cprint(&format!("{}{}", e.data, blank), None, bg)?
101 },
102 None => self.term.print(&blank)?,
103 }
104 }
105 self.term.move_to(self.term.width_str(&self.query) + 2, 0)?;
106 self.term.cursor.show()
107 }
Sourcepub fn move_up(&mut self) -> Result<(), Error>
pub fn move_up(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 13)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
Sourcepub fn move_down(&mut self) -> Result<(), Error>
pub fn move_down(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 14)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
Sourcepub fn move_left(&mut self) -> Result<(), Error>
pub fn move_left(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 15)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
Sourcepub fn move_right(&mut self) -> Result<(), Error>
pub fn move_right(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 16)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
Sourcepub fn move_home(&mut self) -> Result<(), Error>
pub fn move_home(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 8)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
Sourcepub fn move_end(&mut self) -> Result<(), Error>
pub fn move_end(&mut self) -> Result<(), Error>
Examples found in repository?
examples/debug.rs (line 9)
3fn handle_event(ev: &Event, term: &mut Term) -> bool {
4 match ev {
5 &Event::Ctrl(Input::Chars(ref s)) => match s.as_str() {
6 "C" => return false,
7 "L" => term.clear().unwrap(),
8 "A" => term.move_home().unwrap(),
9 "E" => term.move_end().unwrap(),
10 "D" => term.cursor.delete_char().unwrap(),
11 k => term.print(&format!("Ctrl+{}", k)).unwrap(),
12 },
13 &Event::Raw(Input::Arrow(Direction::Up)) => term.move_up().unwrap(),
14 &Event::Raw(Input::Arrow(Direction::Down)) => term.move_down().unwrap(),
15 &Event::Raw(Input::Arrow(Direction::Left)) => term.move_left().unwrap(),
16 &Event::Raw(Input::Arrow(Direction::Right)) => term.move_right().unwrap(),
17 &Event::Raw(Input::Return) => {
18 term.print("↩︎").unwrap();
19 term.move_home().unwrap();
20 term.move_down().unwrap();
21 },
22 &Event::Raw(Input::Delete) => term.cursor.backspace().unwrap(),
23 &Event::Raw(Input::BackSpace) => term.cursor.backspace().unwrap(),
24 &Event::Raw(Input::Chars(ref s)) => {
25 let cs: Vec<String> = s.chars().map(|c| format!("{:02x}", c as usize)).collect();
26 term.print(&format!("{}:[{}]", s, cs.join(", "))).unwrap();
27 },
28 &Event::TermSize(w, h) => term.matrix.refresh(w, h),
29 e => {
30 let pos = term.cursor.get_pos();
31 term.print(format!("e: {:?}, pos{:?}", e, pos).as_str()).unwrap();
32 },
33 }
34 true
35}
pub fn move_top(&mut self) -> Result<(), Error>
pub fn move_bottom(&mut self) -> Result<(), Error>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Term
impl RefUnwindSafe for Term
impl Send for Term
impl Sync for Term
impl Unpin for Term
impl UnwindSafe for Term
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more