Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f36040d

Browse files
committedMar 2, 2025
refactor: save data size as number
1 parent 390b012 commit f36040d

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed
 

‎src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl LogViewerApp {
725725
(false, _, total_len) => as_string_with_separators(total_len),
726726
};
727727
ui.label(format!("# Rows: {row_count_text}"));
728-
ui.label(format!("Size: {}", data.file_size));
728+
ui.label(format!("Size: {}", data.file_size_display()));
729729
}
730730
});
731731
}

‎src/app/data.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Data {
2525
rows: Vec<LogRow>,
2626
filtered_rows: Option<Vec<usize>>,
2727
applied_filter: Option<FilterConfig>,
28-
pub file_size: String,
28+
pub file_size_as_bytes: usize,
2929
}
3030

3131
#[derive(serde::Deserialize, serde::Serialize, Default, Debug, PartialEq, Eq, Clone)]
@@ -136,6 +136,10 @@ impl LogRow {
136136
}
137137

138138
impl Data {
139+
pub fn file_size_display(&self) -> String {
140+
SizeUnits::Auto.convert_trimmed(self.file_size_as_bytes)
141+
}
142+
139143
pub fn rows_iter(&self) -> impl Iterator<Item = &'_ LogRow> {
140144
DataIter::new(self)
141145
}
@@ -415,7 +419,7 @@ impl TryFrom<(&DataDisplayOptions, usize, &str)> for LogRow {
415419
if let Some(config) = data_display_options.row_size_config.as_ref() {
416420
result.or_insert(
417421
config.field_name.clone(),
418-
config.units.convert(row_size_in_bytes),
422+
config.units.convert(row_size_in_bytes).into(),
419423
);
420424
}
421425
Ok(result)
@@ -465,15 +469,8 @@ impl TryFrom<(&DataDisplayOptions, &str)> for Data {
465469
fn try_from(
466470
(data_display_options, value): (&DataDisplayOptions, &str),
467471
) -> Result<Self, Self::Error> {
468-
let file_size = SizeUnits::Auto.convert(value.len());
469-
let file_size = file_size
470-
.as_str()
471-
.map(|x| x.to_string())
472-
.unwrap_or_else(|| file_size.to_string())
473-
.trim_matches('0')
474-
.to_string();
475472
let mut result = Data {
476-
file_size,
473+
file_size_as_bytes: value.len(),
477474
..Default::default()
478475
};
479476
for (i, line) in value.lines().enumerate() {

‎src/app/data_display_options.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ impl SizeUnits {
116116
}
117117
}
118118

119-
/// Output as a string because includes the unit
120-
pub(crate) fn convert(&self, row_size_in_bytes: usize) -> serde_json::Value {
119+
pub(crate) fn convert(&self, row_size_in_bytes: usize) -> String {
121120
let concrete_unit = self.to_concrete(row_size_in_bytes);
122121
let scalar = concrete_unit.scalar();
123122
let result = row_size_in_bytes as f64 / scalar;
124-
format!("{result:0>9.4} {concrete_unit}").into()
123+
format!("{result:0>9.4} {concrete_unit}")
124+
}
125+
126+
pub fn convert_trimmed(&self, row_size_in_bytes: usize) -> String {
127+
self.convert(row_size_in_bytes)
128+
.trim_matches('0')
129+
.to_string()
125130
}
126131

127132
pub fn as_str(&self) -> &'static str {

0 commit comments

Comments
 (0)
Please sign in to comment.