fix handling empty files
This commit is contained in:
+6
-1
@@ -183,10 +183,15 @@ impl App {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// returns 0 if empty
|
||||
pub const fn max_contents_index(&self) -> usize {
|
||||
self.contents.len().saturating_sub(1)
|
||||
}
|
||||
}
|
||||
|
||||
fn nybble_from_hex(hex: char) -> Option<u8> {
|
||||
if !hex.is_ascii() { return None }
|
||||
if !hex.is_ascii() { return None; }
|
||||
|
||||
match hex {
|
||||
'0'..='9' => Some(u8::try_from(hex).unwrap() - u8::try_from('0').unwrap()),
|
||||
|
||||
+12
-4
@@ -33,6 +33,10 @@ impl Widget for &App {
|
||||
let hex_area = Rect::new(area.x, area.y, area.width, area.height - 1);
|
||||
hex_text.render(hex_area, buf);
|
||||
|
||||
if self.contents.is_empty() {
|
||||
Line::from("empty file").render(area, buf);
|
||||
}
|
||||
|
||||
let status_line_area = Rect::new(area.x, area.bottom() - 1, area.width, 1);
|
||||
self.render_status_line().render(status_line_area, buf);
|
||||
|
||||
@@ -416,14 +420,18 @@ mod extra_statuses {
|
||||
|
||||
impl App {
|
||||
pub fn render_extra_statuses(&self) -> Line<'_> {
|
||||
#[allow(clippy::cast_precision_loss)]
|
||||
let percentage = self.cursor.head as f64 / (self.contents.len() - 1) as f64 * 100.0;
|
||||
|
||||
let partial_action = self.partial_action
|
||||
.as_ref()
|
||||
.map_or("", |partial_action| partial_action.label());
|
||||
|
||||
format!("{partial_action} {percentage:.0}% ").into()
|
||||
if self.contents.is_empty() {
|
||||
format!("{partial_action} ").into()
|
||||
} else {
|
||||
#[allow(clippy::cast_precision_loss)]
|
||||
let percentage = self.cursor.head as f64 / self.max_contents_index() as f64 * 100.0;
|
||||
|
||||
format!("{partial_action} {percentage:.0}% ").into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user