Board: make string formatting stuff const
This commit is contained in:
@@ -88,22 +88,22 @@ pub struct Board {
|
||||
impl fmt::Display for Board {
|
||||
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let horiz_sep_line = "-".repeat((Self::SIZE * 2 + 1) as usize);
|
||||
const HORIZ_SEP_LINE: &str = const_str::repeat!("-", (Board::SIZE * 2 + 1) as usize);
|
||||
|
||||
// basically calculates the # of digits BOARD_SIZE needs
|
||||
const PADDING: usize = (Board::SIZE - 1).ilog10() as usize + 1;
|
||||
|
||||
let space_padding = " ".repeat(PADDING);
|
||||
const SPACE_PADDING: &str = const_str::repeat!(" ", PADDING);
|
||||
|
||||
// Print numbers at top so the board can be read more easier
|
||||
write!(f, "{} ", space_padding)?;
|
||||
write!(f, "{} ", SPACE_PADDING)?;
|
||||
for j in (0..Self::SIZE).rev() {
|
||||
write!(f, "{:0PADDING$} ", j)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
|
||||
for i in (0..Self::SIZE).rev() {
|
||||
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
|
||||
writeln!(f, "{}{}", SPACE_PADDING, HORIZ_SEP_LINE)?;
|
||||
|
||||
write!(f, "{:0PADDING$}|", i)?;
|
||||
for j in (0..Self::SIZE).rev() {
|
||||
@@ -120,7 +120,7 @@ impl fmt::Display for Board {
|
||||
}
|
||||
|
||||
// put a line at the bottom of the board too
|
||||
writeln!(f, " {}", horiz_sep_line)?;
|
||||
writeln!(f, " {}", HORIZ_SEP_LINE)?;
|
||||
|
||||
// Print the current score
|
||||
write!(
|
||||
|
||||
Reference in New Issue
Block a user