generated from GarandPLG/rust-flake-template
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:
+23
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user