Struct Cell

Source
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

Source

pub fn new(ch: char, fg: Color, bg: Color, attrs: Attr) -> Cell

Creates a new Cell with the given char, Colors 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);
Source

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);
Source

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(), ' ');
Source

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');
Source

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
Hide additional 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}
Source

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);
Source

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);
Source

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);
Source

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}
Source

pub fn attrs(&self) -> Attr

Source

pub fn set_attrs(&mut self, newattrs: Attr) -> &mut Cell

Trait Implementations§

Source§

impl Clone for Cell

Source§

fn clone(&self) -> Cell

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cell

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Cell

Source§

fn default() -> Cell

Constructs a new Cell with a blank char and default Colors.

§Examples
use rustty::{Cell, Color};

let mut cell = Cell::default();
assert_eq!(cell.ch(), ' ');
assert_eq!(cell.fg(), Color::Default);
assert_eq!(cell.bg(), Color::Default);
Source§

impl PartialEq for Cell

Source§

fn eq(&self, other: &Cell) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Cell

Source§

impl Eq for Cell

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.