Skip to content

Commit 3280ee7

Browse files
committed
enhance: tweak ToFileSize for readability
1 parent d3d1377 commit 3280ee7

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

SourceGit.sln

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,44 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{C54D
8686
build\scripts\package.windows.sh = build\scripts\package.windows.sh
8787
EndProjectSection
8888
EndProject
89+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
90+
EndProject
91+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGit.Tests", "tests\SourceGit.Tests.csproj", "{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}"
92+
EndProject
8993
Global
9094
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9195
Debug|Any CPU = Debug|Any CPU
96+
Debug|x64 = Debug|x64
97+
Debug|x86 = Debug|x86
9298
Release|Any CPU = Release|Any CPU
99+
Release|x64 = Release|x64
100+
Release|x86 = Release|x86
93101
EndGlobalSection
94102
GlobalSection(ProjectConfigurationPlatforms) = postSolution
95103
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
96104
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
105+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|x64.ActiveCfg = Debug|Any CPU
106+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|x64.Build.0 = Debug|Any CPU
107+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|x86.ActiveCfg = Debug|Any CPU
108+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|x86.Build.0 = Debug|Any CPU
97109
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
98110
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|Any CPU.Build.0 = Release|Any CPU
111+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|x64.ActiveCfg = Release|Any CPU
112+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|x64.Build.0 = Release|Any CPU
113+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|x86.ActiveCfg = Release|Any CPU
114+
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|x86.Build.0 = Release|Any CPU
115+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
116+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
117+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|x64.ActiveCfg = Debug|Any CPU
118+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|x64.Build.0 = Debug|Any CPU
119+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|x86.ActiveCfg = Debug|Any CPU
120+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|x86.Build.0 = Debug|Any CPU
121+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
122+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|Any CPU.Build.0 = Release|Any CPU
123+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|x64.ActiveCfg = Release|Any CPU
124+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|x64.Build.0 = Release|Any CPU
125+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|x86.ActiveCfg = Release|Any CPU
126+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|x86.Build.0 = Release|Any CPU
99127
EndGlobalSection
100128
GlobalSection(SolutionProperties) = preSolution
101129
HideSolutionNode = FALSE
@@ -116,6 +144,7 @@ Global
116144
{7802CD7A-591B-4EDD-96F8-9BF3F61692E4} = {9BA0B044-0CC9-46F8-B551-204F149BF45D}
117145
{5D125DD9-B48A-491F-B2FB-D7830D74C4DC} = {FD384607-ED99-47B7-AF31-FB245841BC92}
118146
{C54D4001-9940-477C-A0B6-E795ED0A3209} = {773082AC-D9C8-4186-8521-4B6A7BEE6158}
147+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
119148
EndGlobalSection
120149
GlobalSection(ExtensibilityGlobals) = postSolution
121150
SolutionGuid = {7FF1B9C6-B5BF-4A50-949F-4B407A0E31C9}

src/Converters/LongConverters.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ public static class LongConverters
66
{
77
public static readonly FuncValueConverter<long, string> ToFileSize = new(bytes =>
88
{
9+
var full = $"{bytes:N0} B";
910
if (bytes < KB)
10-
return $"{bytes} B";
11+
return full;
1112

1213
if (bytes < MB)
13-
return $"{(bytes / KB):F3} KB ({bytes} B)";
14+
return $"{(bytes / KB):G3} KB ({full})";
1415

1516
if (bytes < GB)
16-
return $"{(bytes / MB):F3} MB ({bytes} B)";
17+
return $"{(bytes / MB):G3} MB ({full})";
1718

18-
return $"{(bytes / GB):F3} GB ({bytes} B)";
19+
if (bytes < 1000*GB)
20+
return $"{(bytes / GB):G3} GB ({full})";
21+
22+
return $"{(bytes / GB):N0} GB ({full})";
1923
});
2024

2125
private const double KB = 1024;
22-
private const double MB = 1024 * 1024;
23-
private const double GB = 1024 * 1024 * 1024;
26+
private const double MB = 1024 * KB;
27+
private const double GB = 1024 * MB;
2428
}
2529
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using SourceGit.Converters;
2+
using System.Globalization;
3+
4+
namespace SourceGit.Tests.Converters
5+
{
6+
public class LongConvertersTests
7+
{
8+
[Theory]
9+
[InlineData(4, "4 B")]
10+
[InlineData(44, "44 B")]
11+
[InlineData(444, "444 B")]
12+
[InlineData(4444, "4.34 KB (4,444 B)")]
13+
[InlineData(44444, "43.4 KB (44,444 B)")]
14+
[InlineData(444444, "434 KB (444,444 B)")]
15+
[InlineData(4444444, "4.24 MB (4,444,444 B)")]
16+
[InlineData(44444444, "42.4 MB (44,444,444 B)")]
17+
[InlineData(444444444, "424 MB (444,444,444 B)")]
18+
[InlineData(4444444444, "4.14 GB (4,444,444,444 B)")]
19+
[InlineData(44444444444, "41.4 GB (44,444,444,444 B)")]
20+
[InlineData(444444444444, "414 GB (444,444,444,444 B)")]
21+
[InlineData(4444444444444, "4,139 GB (4,444,444,444,444 B)")]
22+
[InlineData(long.MinValue, "-9,223,372,036,854,775,808 B")]
23+
[InlineData(0, "0 B")]
24+
[InlineData(long.MaxValue, "8,589,934,592 GB (9,223,372,036,854,775,807 B)")]
25+
public void ToFileSize_ShouldReturnCorrectFormat(long bytes, string expected)
26+
{
27+
var actual = LongConverters.ToFileSize.Convert(bytes, typeof(string), null, CultureInfo.CurrentCulture) as string;
28+
Assert.Equal(expected, actual);
29+
}
30+
}
31+
}

tests/SourceGit.Tests.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
8+
<PackageReference Include="xunit" Version="2.9.3" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<Using Include="Xunit" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\src\SourceGit.csproj" />
18+
</ItemGroup>
19+
</Project>

0 commit comments

Comments
 (0)