Skip to content

Commit 6e9939a

Browse files
committed
Improve "written" message for small (<1024B) files
Using a float when the file size is in bytes is confusing. Instead we should show "123B" for <1024B and use floats only for KiB and above.
1 parent b08aba8 commit 6e9939a

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

helix-term/src/application.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,41 @@ impl Application {
580580
doc.set_last_saved_revision(doc_save_event.revision, doc_save_event.save_time);
581581

582582
let lines = doc_save_event.text.len_lines();
583-
let mut sz = doc_save_event.text.len_bytes() as f32;
583+
let size = doc_save_event.text.len_bytes();
584584

585-
const SUFFIX: [&str; 4] = ["B", "KiB", "MiB", "GiB"];
586-
let mut i = 0;
587-
while i < SUFFIX.len() - 1 && sz >= 1024.0 {
588-
sz /= 1024.0;
589-
i += 1;
585+
enum Size {
586+
Bytes(u16),
587+
HumanReadable(f32, &'static str),
590588
}
591589

590+
impl std::fmt::Display for Size {
591+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
592+
match self {
593+
Self::Bytes(bytes) => write!(f, "{bytes}B"),
594+
Self::HumanReadable(size, suffix) => write!(f, "{size:.1}{suffix}"),
595+
}
596+
}
597+
}
598+
599+
let size = if size < 1024 {
600+
Size::Bytes(size as u16)
601+
} else {
602+
const SUFFIX: [&str; 4] = ["B", "KiB", "MiB", "GiB"];
603+
let mut size = size as f32;
604+
let mut i = 0;
605+
while i < SUFFIX.len() - 1 && size >= 1024.0 {
606+
size /= 1024.0;
607+
i += 1;
608+
}
609+
Size::HumanReadable(size, SUFFIX[i])
610+
};
611+
592612
self.editor
593613
.set_doc_path(doc_save_event.doc_id, &doc_save_event.path);
594614
// TODO: fix being overwritten by lsp
595615
self.editor.set_status(format!(
596-
"'{}' written, {}L {:.1}{}",
616+
"'{}' written, {lines}L {size}",
597617
get_relative_path(&doc_save_event.path).to_string_lossy(),
598-
lines,
599-
sz,
600-
SUFFIX[i],
601618
));
602619
}
603620

0 commit comments

Comments
 (0)