Skip to main content

Cell

Struct Cell 

Source
pub struct Cell {
    pub ch: char,
    pub colors: ColorPair,
    pub bold: bool,
    pub dim: bool,
    pub italic: bool,
    pub underline: bool,
}
Expand description

A styled terminal cell.

Fields§

§ch: char§colors: ColorPair§bold: bool§dim: bool§italic: bool§underline: bool

Implementations§

Source§

impl Cell

Source

pub fn new(ch: char) -> Self

Examples found in repository?
examples/loaders.rs (line 346)
345fn colored_cell(ch: char, fg: Color, bg: Option<Color>, bold: bool, dim: bool) -> Cell {
346    let mut cell = Cell::new(ch);
347    cell.colors.fg = Some(fg);
348    cell.colors.bg = bg;
349    cell.bold = bold;
350    cell.dim = dim;
351    cell
352}
More examples
Hide additional examples
examples/new_loaders.rs (line 193)
192fn colored_cell(ch: char, fg: Color, bg: Option<Color>, bold: bool, dim: bool) -> Cell {
193    let mut cell = Cell::new(ch);
194    cell.colors.fg = Some(fg);
195    cell.colors.bg = bg;
196    cell.bold = bold;
197    cell.dim = dim;
198    cell
199}
examples/loader_lab.rs (line 966)
963fn fill_rect(screen: &mut Frame, area: Rect, bg: Option<Color>) {
964    for y in area.y..(area.y + area.h).min(screen.height()) {
965        for x in area.x..(area.x + area.w).min(screen.width()) {
966            let mut cell = Cell::new(' ');
967            cell.colors.bg = bg;
968            screen.set(x, y, cell);
969        }
970    }
971}
972
973fn draw_frame(screen: &mut Frame, x: usize, y: usize, frame: &Frame, max_w: usize, max_h: usize) {
974    for fy in 0..frame.height().min(max_h) {
975        for fx in 0..frame.width().min(max_w) {
976            if let Some(cell) = frame.cell(fx, fy) {
977                if cell.ch != ' ' || cell.has_style() {
978                    screen.set(x + fx, y + fy, cell.clone());
979                }
980            }
981        }
982    }
983}
984
985fn put_text(
986    screen: &mut Frame,
987    x: usize,
988    y: usize,
989    text: &str,
990    fg: Color,
991    bg: Option<Color>,
992    bold: bool,
993) {
994    put_text_clipped(screen, x, y, text, usize::MAX, fg, bg, bold);
995}
996
997fn put_text_clipped(
998    screen: &mut Frame,
999    x: usize,
1000    y: usize,
1001    text: &str,
1002    max_width: usize,
1003    fg: Color,
1004    bg: Option<Color>,
1005    bold: bool,
1006) {
1007    if y >= screen.height() || max_width == 0 {
1008        return;
1009    }
1010    for (offset, ch) in text.chars().take(max_width).enumerate() {
1011        let px = x + offset;
1012        if px >= screen.width() {
1013            break;
1014        }
1015        put_cell(screen, px, y, ch, fg, bg, bold, false);
1016    }
1017}
1018
1019fn put_text_right(
1020    screen: &mut Frame,
1021    right_x: usize,
1022    y: usize,
1023    text: &str,
1024    fg: Color,
1025    bg: Option<Color>,
1026    bold: bool,
1027) {
1028    let x = right_x.saturating_sub(text.chars().count());
1029    put_text(screen, x, y, text, fg, bg, bold);
1030}
1031
1032fn put_cell(
1033    screen: &mut Frame,
1034    x: usize,
1035    y: usize,
1036    ch: char,
1037    fg: Color,
1038    bg: Option<Color>,
1039    bold: bool,
1040    dim: bool,
1041) {
1042    if x >= screen.width() || y >= screen.height() {
1043        return;
1044    }
1045    let mut cell = Cell::new(ch);
1046    cell.colors.fg = Some(fg);
1047    cell.colors.bg = bg;
1048    cell.bold = bold;
1049    cell.dim = dim;
1050    screen.set(x, y, cell);
1051}
examples/story.rs (line 856)
853fn fill_rect(screen: &mut Frame, area: Rect, bg: Option<Color>) {
854    for y in area.y..(area.y + area.h).min(screen.height()) {
855        for x in area.x..(area.x + area.w).min(screen.width()) {
856            let mut cell = Cell::new(' ');
857            cell.colors.bg = bg;
858            screen.set(x, y, cell);
859        }
860    }
861}
862
863fn draw_quote(screen: &mut Frame, area: Rect, quote: &str, accent: Color) {
864    let wrapped = wrap_text(quote, area.w);
865    put_text(
866        screen,
867        area.x,
868        area.y,
869        "SOURCE FROM test.sh:",
870        accent,
871        Some(Color::rgb(7, 12, 18)),
872        true,
873    );
874    for (line_index, line) in wrapped.lines().take(area.h.saturating_sub(1)).enumerate() {
875        put_text(
876            screen,
877            area.x,
878            area.y + line_index + 1,
879            line,
880            Color::rgb(186, 204, 214),
881            Some(Color::rgb(7, 12, 18)),
882            false,
883        );
884    }
885}
886
887fn draw_frame_centered(screen: &mut Frame, area: Rect, frame: &Frame) {
888    let x = area.x + area.w.saturating_sub(frame.width()) / 2;
889    let y = area.y + area.h.saturating_sub(frame.height()) / 2;
890    draw_frame(screen, x, y, frame, area.w, area.h);
891}
892
893fn draw_frame(screen: &mut Frame, x: usize, y: usize, frame: &Frame, max_w: usize, max_h: usize) {
894    for fy in 0..frame.height().min(max_h) {
895        for fx in 0..frame.width().min(max_w) {
896            if let Some(cell) = frame.cell(fx, fy) {
897                if cell.ch != ' ' || cell.has_style() {
898                    screen.set(x + fx, y + fy, cell.clone());
899                }
900            }
901        }
902    }
903}
904
905fn draw_progress_strip(screen: &mut Frame, area: Rect, ratio: f32, fill: Color, empty: Color) {
906    let active = (ratio.clamp(0.0, 1.0) * area.w as f32).round() as usize;
907    for x in 0..area.w {
908        let done = x < active;
909        put_cell(
910            screen,
911            area.x + x,
912            area.y,
913            if done { '#' } else { '-' },
914            if done { fill } else { empty },
915            Some(Color::rgb(7, 12, 18)),
916            done,
917            !done,
918        );
919    }
920}
921
922fn put_text(
923    screen: &mut Frame,
924    x: usize,
925    y: usize,
926    text: &str,
927    fg: Color,
928    bg: Option<Color>,
929    bold: bool,
930) {
931    if y >= screen.height() {
932        return;
933    }
934    for (offset, ch) in text.chars().enumerate() {
935        let px = x + offset;
936        if px >= screen.width() {
937            break;
938        }
939        put_cell(screen, px, y, ch, fg, bg, bold, false);
940    }
941}
942
943fn put_text_right(
944    screen: &mut Frame,
945    right_x: usize,
946    y: usize,
947    text: &str,
948    fg: Color,
949    bg: Option<Color>,
950    bold: bool,
951) {
952    let width = text.chars().count();
953    let x = right_x.saturating_sub(width);
954    put_text(screen, x, y, text, fg, bg, bold);
955}
956
957fn put_cell(
958    screen: &mut Frame,
959    x: usize,
960    y: usize,
961    ch: char,
962    fg: Color,
963    bg: Option<Color>,
964    bold: bool,
965    dim: bool,
966) {
967    if x >= screen.width() || y >= screen.height() {
968        return;
969    }
970    let mut cell = Cell::new(ch);
971    cell.colors.fg = Some(fg);
972    cell.colors.bg = bg;
973    cell.bold = bold;
974    cell.dim = dim;
975    screen.set(x, y, cell);
976}
Source

pub fn styled( ch: char, fg: impl Into<Option<Color>>, bg: impl Into<Option<Color>>, ) -> Self

Source

pub fn with_fg(self, color: Color) -> Self

Source

pub fn with_bg(self, color: Color) -> Self

Source

pub fn has_style(&self) -> bool

Examples found in repository?
examples/loader_lab.rs (line 977)
973fn draw_frame(screen: &mut Frame, x: usize, y: usize, frame: &Frame, max_w: usize, max_h: usize) {
974    for fy in 0..frame.height().min(max_h) {
975        for fx in 0..frame.width().min(max_w) {
976            if let Some(cell) = frame.cell(fx, fy) {
977                if cell.ch != ' ' || cell.has_style() {
978                    screen.set(x + fx, y + fy, cell.clone());
979                }
980            }
981        }
982    }
983}
More examples
Hide additional examples
examples/story.rs (line 897)
893fn draw_frame(screen: &mut Frame, x: usize, y: usize, frame: &Frame, max_w: usize, max_h: usize) {
894    for fy in 0..frame.height().min(max_h) {
895        for fx in 0..frame.width().min(max_w) {
896            if let Some(cell) = frame.cell(fx, fy) {
897                if cell.ch != ' ' || cell.has_style() {
898                    screen.set(x + fx, y + fy, cell.clone());
899                }
900            }
901        }
902    }
903}
examples/new_loaders.rs (line 164)
160fn overlay_frame(screen: &mut Frame, x0: usize, y0: usize, src: &Frame) {
161    for y in 0..src.height() {
162        for x in 0..src.width() {
163            if let Some(cell) = src.cell(x, y) {
164                if cell.ch == ' ' && !cell.has_style() {
165                    continue;
166                }
167                if x + x0 < screen.width() && y + y0 < screen.height() {
168                    screen.set(x + x0, y + y0, cell.clone());
169                }
170            }
171        }
172    }
173}
examples/loaders.rs (line 312)
258fn draw_card(
259    screen: &mut Frame,
260    layout: &GridLayout,
261    x: usize,
262    y: usize,
263    width: usize,
264    height: usize,
265    kind: LoaderKind,
266    frame: &Frame,
267) {
268    if width == 0 || height == 0 {
269        return;
270    }
271    let pane_bg = Color::rgb(9, 14, 22);
272    let border = Color::rgb(48, 72, 96);
273    let title = loader_title_color(kind);
274    for py in 0..height {
275        for px in 0..width {
276            screen.set(
277                x + px,
278                y + py,
279                colored_cell(' ', Color::rgb(40, 48, 56), Some(pane_bg), false, false),
280            );
281        }
282    }
283    if height >= 3 && width >= 8 {
284        for px in 0..width {
285            screen.set(
286                x + px,
287                y,
288                colored_cell('-', border, Some(pane_bg), false, true),
289            );
290        }
291        screen.set(x, y, colored_cell('+', border, Some(pane_bg), false, false));
292        screen.set(
293            x + width - 1,
294            y,
295            colored_cell('+', border, Some(pane_bg), false, false),
296        );
297        put_text(screen, x + 2, y, kind.name(), title, true);
298    } else {
299        put_text(screen, x, y, kind.name(), title, true);
300    }
301    if height == 1 {
302        return;
303    }
304
305    let inner_y = y + 1;
306    let inner_x = if width > 2 { x + 1 } else { x };
307    let inner_width = width.saturating_sub(2).max(1);
308    let inner_height = height.saturating_sub(1).max(1);
309    for fy in 0..frame.height().min(inner_height) {
310        for fx in 0..frame.width().min(inner_width) {
311            if let Some(cell) = frame.cell(fx, fy) {
312                if cell.ch != ' ' || cell.has_style() {
313                    screen.set(inner_x + fx, inner_y + fy, cell.clone());
314                }
315            }
316        }
317    }
318
319    if layout.card_height <= 2 && width > 8 {
320        let percent_x = x + width.saturating_sub(5);
321        put_text(
322            screen,
323            percent_x,
324            y,
325            "    ",
326            Color::rgb(120, 132, 144),
327            false,
328        );
329    }
330}

Trait Implementations§

Source§

impl Clone for Cell

Source§

fn clone(&self) -> Cell

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Cell

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 (const: unstable) · 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 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 UnsafeUnpin 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.