Skip to content

Commit

Permalink
Refactor screen text trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Jan 21, 2025
1 parent 0aa7a6b commit b7a547d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions firmware/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,24 @@ fn footer<D: DrawTarget<Color = BinaryColor>>(
Ok(())
}

/// Trim text if it's too long
fn trim(text: &str, max_len: usize) -> &str {
if text.len() > max_len {
&text[..max_len]
} else {
text
}
}

/// Draw user greeting (top 10 lines, 0..10)
fn greeting<D: DrawTarget<Color = BinaryColor>>(
random: u32,
name: &str,
target: &mut D,
) -> Result<(), Error<D::Error>> {
let greeting = GREETINGS[random as usize % GREETINGS.len()];
let greeting_len = greeting.len() + 1;

// Trim name if it's too long to display
let name = if name.len() + greeting_len > MEDIUM_CHARS_PER_LINE as usize {
&name[..(MEDIUM_CHARS_PER_LINE as usize - greeting_len)]
} else {
name
};

let name = trim(name, MEDIUM_CHARS_PER_LINE as usize - greeting.len() - 1);
centered(&MEDIUM_FONT, 8, format_args!("{greeting} {name}"), target)
}

Expand Down

0 comments on commit b7a547d

Please sign in to comment.