Skip to content

Commit

Permalink
refactor display binary as hex dump
Browse files Browse the repository at this point in the history
* DRY
* add display in MB (more human friendly)
  • Loading branch information
pmiossec committed May 23, 2024
1 parent b803cb9 commit ea49c0d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions GitUI/Editor/FileViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,17 +579,7 @@ public Task ViewGrepAsync(FileStatusItem item, string text)
{
try
{
StringBuilder summary = new StringBuilder()
.AppendLine("Binary file:")
.AppendLine()
.AppendLine(fileName)
.AppendLine()
.AppendLine($"{text.Length:N0} bytes:")
.AppendLine();
internalFileViewer.SetText(summary.ToString(), openWithDifftool);
ToHexDump(text, summary);
internalFileViewer.SetText(summary.ToString(), openWithDifftool);
DisplayAsHexDump("Binary file: " + fileName, text, openWithDifftool);
}
catch
{
Expand All @@ -611,6 +601,25 @@ public Task ViewGrepAsync(FileStatusItem item, string text)
});
}

private void DisplayAsHexDump(string header, string text, Action? openWithDifftool)
{
StringBuilder summary = new StringBuilder()
.AppendLine(header)
.AppendLine();

double mb = text.Length / (1024d * 1024);
if (mb > 1)
{
summary.Append($"{mb:N1} MB / ");
}

summary.AppendLine($"{text.Length:N0} bytes:")
.AppendLine();

ToHexDump(text, summary);
internalFileViewer.SetText(summary.ToString(), openWithDifftool);
}

public Task ViewGitItemAsync(FileStatusItem item, int? line, Action? openWithDifftool)
{
return ViewGitItemAsync(item.Item, item.SecondRevision.ObjectId, item, line, openWithDifftool);
Expand Down Expand Up @@ -1166,17 +1175,9 @@ private Task ViewItemAsync(string fileName, bool isSubmodule, Func<Image?> getIm
{
if (image is null)
{
ResetView(ViewMode.Text, null);
ResetView(ViewMode.Text, fileName, item);
string text = getFileText();
StringBuilder summary = new StringBuilder()
.AppendLine(string.Format(_cannotViewImage.Text, fileName))
.AppendLine()
.AppendLine($"{text.Length:N0} bytes:")
.AppendLine();
ToHexDump(text, summary);
internalFileViewer.SetText(summary.ToString(), openWithDifftool);
DisplayAsHexDump(string.Format(_cannotViewImage.Text, fileName), text, openWithDifftool);
return;
}
Expand Down

0 comments on commit ea49c0d

Please sign in to comment.