pub struct Cell { /* private fields */ }
Expand description
A single point on a terminal display.
A Cell
contains a character and style.
Implementations§
Source§impl Cell
impl Cell
Sourcepub fn new(ch: char, fg: Color, bg: Color, attrs: Attr) -> Cell
pub fn new(ch: char, fg: Color, bg: Color, attrs: Attr) -> Cell
Creates a new Cell
with the given char
, Color
s and Attr
.
§Examples
use rustty::{Cell, Color, Attr};
let cell = Cell::new('x', Color::Default, Color::Green, Attr::Default);
assert_eq!(cell.ch(), 'x');
assert_eq!(cell.fg(), Color::Default);
assert_eq!(cell.bg(), Color::Green);
assert_eq!(cell.attrs(), Attr::Default);
Sourcepub fn with_char(ch: char) -> Cell
pub fn with_char(ch: char) -> Cell
Creates a new Cell
with the given char
and default style.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::with_char('x');
assert_eq!(cell.ch(), 'x');
assert_eq!(cell.fg(), Color::Default);
assert_eq!(cell.bg(), Color::Default);
assert_eq!(cell.attrs(), Attr::Default);
Sourcepub fn with_style(fg: Color, bg: Color, attr: Attr) -> Cell
pub fn with_style(fg: Color, bg: Color, attr: Attr) -> Cell
Creates a new Cell
with the given style and a blank char
.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::with_style(Color::Default, Color::Red, Attr::Bold);
assert_eq!(cell.fg(), Color::Default);
assert_eq!(cell.bg(), Color::Red);
assert_eq!(cell.attrs(), Attr::Bold);
assert_eq!(cell.ch(), ' ');
Sourcepub fn ch(&self) -> char
pub fn ch(&self) -> char
Returns the Cell
’s character.
§Examples
use rustty::Cell;
let mut cell = Cell::with_char('x');
assert_eq!(cell.ch(), 'x');
Sourcepub fn set_ch(&mut self, newch: char) -> &mut Cell
pub fn set_ch(&mut self, newch: char) -> &mut Cell
Sets the Cell
’s character to the given char
§Examples
use rustty::Cell;
let mut cell = Cell::with_char('x');
assert_eq!(cell.ch(), 'x');
cell.set_ch('y');
assert_eq!(cell.ch(), 'y');
Examples found in repository?
examples/circle.rs (line 60)
26fn main() {
27 // Create our terminal, dialog window and main canvas
28 let mut term = Terminal::new().unwrap();
29 let mut optiondlg = create_optiondlg();
30 let mut canvas = Widget::new(term.size().0, term.size().1 - 4);
31
32 // Align canvas to top left, and dialog to bottom right
33 optiondlg.window_mut().align(&term, HorizontalAlign::Right, VerticalAlign::Bottom, 0);
34 canvas.align(&term, HorizontalAlign::Left, VerticalAlign::Top, 0);
35
36 let mut radius = 10u32;
37 'main: loop {
38 while let Some(Event::Key(ch)) = term.get_event(Duration::new(0, 0)).unwrap() {
39 match ch {
40 'q' => break 'main,
41 '+' => radius = radius.saturating_add(1),
42 '-' => radius = radius.saturating_sub(1),
43 _ => {}
44 }
45 }
46 // Grab the size of the canvas
47 let (cols, rows) = canvas.size();
48 let (cols, rows) = (cols as isize, rows as isize);
49
50 let (a, b) = (cols / 2, rows / 2);
51
52 // Main render loop, draws the circle to canvas
53 for i in 0..cols * rows {
54 let y = i as isize / cols;
55 let x = i as isize % cols;
56
57 let mut cell = canvas.get_mut(x as usize, y as usize).unwrap();
58
59 if ((x - a).pow(2) / 4 + (y - b).pow(2)) <= radius.pow(2) as isize {
60 cell.set_ch(BLOCK);
61 } else {
62 cell.set_ch(' ');
63 }
64 }
65
66 // draw the canvas, dialog window and swap buffers
67 canvas.draw_into(&mut term);
68 optiondlg.window().draw_into(&mut term);
69 term.swap_buffers().unwrap();
70 }
71}
More examples
examples/textedit.rs (line 42)
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}
Sourcepub fn fg(&self) -> Color
pub fn fg(&self) -> Color
Returns the Cell
’s foreground Color
.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::with_style(Color::Blue, Color::Default, Attr::Default);
assert_eq!(cell.fg(), Color::Blue);
Sourcepub fn set_fg(&mut self, newfg: Color) -> &mut Cell
pub fn set_fg(&mut self, newfg: Color) -> &mut Cell
Sets the Cell
’s foreground Color
to the given Color
.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::default();
assert_eq!(cell.fg(), Color::Default);
cell.set_fg(Color::White);
assert_eq!(cell.fg(), Color::White);
Sourcepub fn bg(&self) -> Color
pub fn bg(&self) -> Color
Returns the Cell
’s background Color
.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::with_style(Color::Default, Color::Green, Attr::Default);
assert_eq!(cell.bg(), Color::Green);
Sourcepub fn set_bg(&mut self, newbg: Color) -> &mut Cell
pub fn set_bg(&mut self, newbg: Color) -> &mut Cell
Sets the Cell
’s background Color
to the given Color
.
§Examples
use rustty::{Cell, Color, Attr};
let mut cell = Cell::default();
assert_eq!(cell.bg(), Color::Default);
cell.set_bg(Color::Black);
assert_eq!(cell.bg(), Color::Black);
Examples found in repository?
examples/textedit.rs (line 26)
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}
pub fn attrs(&self) -> Attr
pub fn set_attrs(&mut self, newattrs: Attr) -> &mut Cell
Trait Implementations§
impl Copy for Cell
impl Eq for Cell
impl StructuralPartialEq for Cell
Auto Trait Implementations§
impl Freeze for Cell
impl RefUnwindSafe for Cell
impl Send for Cell
impl Sync for Cell
impl Unpin for Cell
impl UnwindSafe for Cell
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