This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
How to make a grid #607
Decodetalkers
started this conversation in
General
Replies: 2 comments
-
I'm also trying to made a grid and I would find this helpful. |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is my basic table function, it makes a grid based on the width and hight, rect is the area it is drawn in. pub fn table(
width: i32,
height: i32,
rect: Rect,
frame: &mut Frame<CrosstermBackend<Stdout>>,
) {
let column_constraints = std::iter::repeat(Constraint::Percentage((100 / width) as u16))
.take((width) as usize)
.collect::<Vec<_>>();
let row_constraints = std::iter::repeat(Constraint::Percentage((100 / height) as u16))
.take((height) as usize)
.collect::<Vec<_>>();
let row_rects = Layout::default()
.direction(Direction::Vertical)
.constraints(row_constraints)
.margin(2)
.split(rect);
for i in 0..height {
let column_rects = Layout::default()
.direction(Direction::Horizontal)
.constraints(column_constraints.clone())
.split(row_rects[i as usize]);
for j in 0..width {
let text = Text::from(format!(
"{}{}", i, j
));
let cell_text = Paragraph::new(text).block(
Block::default().borders(Borders::NONE).style(
Style::default()
.bg(Color::Rgb(0, 0, 0)),
),
);
frame.render_widget(cell_text, column_rects[j as usize]);
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I use layout and set margin to zero, then I get this
Beta Was this translation helpful? Give feedback.
All reactions