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: boolImplementations§
Source§impl Cell
impl Cell
Sourcepub fn new(ch: char) -> Self
pub fn new(ch: char) -> Self
Examples found in repository?
More examples
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}pub fn styled( ch: char, fg: impl Into<Option<Color>>, bg: impl Into<Option<Color>>, ) -> Self
pub fn with_fg(self, color: Color) -> Self
pub fn with_bg(self, color: Color) -> Self
Sourcepub fn has_style(&self) -> bool
pub fn has_style(&self) -> bool
Examples found in repository?
More examples
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§
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 UnsafeUnpin 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