[Bf-blender-cvs] [857d5f3] asset-experiments: Be much smarter with file size display...

Bastien Montagne noreply at git.blender.org
Sun Jun 28 00:16:47 CEST 2015


Commit: 857d5f35806f7a9c517eee96324908e099b5005c
Author: Bastien Montagne
Date:   Sun Jun 28 00:16:14 2015 +0200
Branches: asset-experiments
https://developer.blender.org/rB857d5f35806f7a9c517eee96324908e099b5005c

Be much smarter with file size display...

===================================================================

M	source/blender/blenlib/intern/BLI_filelist.c
M	source/blender/editors/space_file/filesel.c

===================================================================

diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c
index 37361cd..da605da 100644
--- a/source/blender/blenlib/intern/BLI_filelist.c
+++ b/source/blender/blenlib/intern/BLI_filelist.c
@@ -244,6 +244,9 @@ void BLI_filelist_entry_size_to_string(
 {
 	double size;
 	const char *fmt;
+	const char *units[] = {"KiB", "MiB", "GiB", "TiB", NULL};
+	const char *units_compact[] = {"K", "M", "G", "T", NULL};
+	const char *unit = "B";
 
 	/*
 	 * Seems st_size is signed 32-bit value in *nix and Windows.  This
@@ -252,27 +255,17 @@ void BLI_filelist_entry_size_to_string(
 	 */
 	size = (double)(st ? st->st_size : sz);
 
-	if (size > 1024.0 * 1024.0 * 1024.0 * 1024.0) {
-		size /= (1024.0 * 1024.0 * 1024.0 * 1024.0);
-		fmt = compact ? "%.0f T" : "%.2f TiB";
-	}
-	else if (size > 1024.0 * 1024.0 * 1024.0) {
-		size /= (1024.0 * 1024.0 * 1024.0);
-		fmt = compact ? "%.0f G" : "%.2f GiB";
-	}
-	else if (size > 1024.0 * 1024.0) {
-		size /= (1024.0 * 1024.0);
-		fmt = compact ? "%.0f M" : "%.2f MiB";
-	}
-	else if (size > 1024.0) {
-		size /= 1024.0;
-		fmt = compact ? "%.0f K" : "%.2f KiB";
+	if (size > 1024.0) {
+		const char **u;
+		for (u = compact ? units_compact : units, size /= 1024.0; size > 1024.0 && *(u + 1); u++, size /= 1024.0);
+		fmt =  size > 100.0 ? "%.0f %s" : (size > 10.0 ? "%.1f %s" : "%.2f %s");
+		unit = *u;
 	}
 	else {
-		fmt = "%.0f B";
+		fmt = "%.0f %s";
 	}
 
-	BLI_snprintf(r_size, sizeof(*r_size) * FILELIST_DIRENTRY_SIZE_LEN, fmt, size);
+	BLI_snprintf(r_size, sizeof(*r_size) * FILELIST_DIRENTRY_SIZE_LEN, fmt, size, unit);
 }
 
 /**
diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c
index d7369c3..b3f5282 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -457,7 +457,7 @@ static void column_widths(FileSelectParams *params, struct FileLayout *layout)
 	/* Biggest possible reasonable values... */
 	layout->column_widths[COLUMN_DATE] = file_string_width(small_size ? "23/08/89" : "23-Dec-89");
 	layout->column_widths[COLUMN_TIME] = file_string_width("23:59");
-	layout->column_widths[COLUMN_SIZE] = file_string_width(small_size ? "987 M" : "987.64 MiB");
+	layout->column_widths[COLUMN_SIZE] = file_string_width(small_size ? "98.7 M" : "98.7 MiB");
 }
 
 void ED_fileselect_init_layout(struct SpaceFile *sfile, ARegion *ar)




More information about the Bf-blender-cvs mailing list