Show cell coordinates with column letters

Introduce `col_to_letters` to convert a zero‑based column index into
Excel‑style
letters. Add `display_coords` that returns the column letters followed
by the
row number. Update `render` to use this formatted coordinate as the
block
title instead of the previous row/col placeholder.
This commit is contained in:
2026-03-29 21:07:05 +02:00
parent 6f17406d98
commit d35d803a78
+23 -2
View File
@@ -24,13 +24,34 @@ impl CellWidget {
pub fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
fn col_to_letters(&self) -> String {
let mut col: usize = self.col + 1;
let mut letters: Vec<char> = Vec::new();
while col > 0 {
letters.push((b'A' + ((col - 1) % 26) as u8) as char);
col = (col - 1) / 26;
}
letters.iter().rev().collect()
}
pub fn display_coords(&self) -> String {
format!("{}{}", self.col_to_letters(), self.row)
}
}
impl Widget for CellWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
Paragraph::new(format!("{}/{}", self.row, self.col))
Paragraph::new("")
.alignment(Alignment::Center)
.block(Block::default().borders(Borders::ALL).white())
.block(
Block::default()
.borders(Borders::ALL)
.white()
.title(self.display_coords().green()),
)
.render(area, buf);
}
}