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) & translated
  • Loading branch information
pmiossec committed May 24, 2024
1 parent 0b42bff commit c15e105
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/app/GitUI/Editor/FileViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public partial class FileViewer : GitModuleControl

private readonly TranslationString _largeFileSizeWarning = new("This file is {0:N1} MB. Showing large files can be slow. Click to show anyway.");
private readonly TranslationString _cannotViewImage = new("Cannot view image {0}");
private readonly TranslationString _fileSizeInMb = new("MB");
private readonly TranslationString _bytes = new("bytes");
private readonly TranslationString _binaryFile = new("Binary file: {0}");
private readonly TranslationString _binaryFileDetected = new("Binary file: {0} (Detected)");

public event EventHandler<SelectedLineEventArgs>? SelectedLineChanged;
public event EventHandler? HScrollPositionChanged;
Expand Down Expand Up @@ -579,21 +583,11 @@ 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(_binaryFile.Text, fileName, text, openWithDifftool);
}
catch
{
internalFileViewer.SetText($"Binary file: {fileName} (Detected)", openWithDifftool);
internalFileViewer.SetText(string.Format(_binaryFileDetected.Text, fileName), openWithDifftool);
}
}
else
Expand All @@ -611,6 +605,26 @@ public Task ViewGrepAsync(FileStatusItem item, string text)
});
}

private void DisplayAsHexDump(string fileNameFormat, string filename, string data, Action? openWithDifftool)
{
StringBuilder summary = new StringBuilder()
.AppendLine(string.Format(fileNameFormat, filename))
.AppendLine();

double mb = data.Length / (1024d * 1024);
if (mb >= 0.1)
{
summary.Append($"{mb:N1} {_fileSizeInMb.Text} / ");
}

summary.AppendLine($"{data.Length:N0} {_bytes.Text}:")
.AppendLine();

string hexData = ToHexDump(data, summary);

internalFileViewer.SetText(hexData, openWithDifftool);
}

public Task ViewGitItemAsync(FileStatusItem item, int? line, Action? openWithDifftool)
{
return ViewGitItemAsync(item.Item, item.SecondRevision.ObjectId, item, line, openWithDifftool);
Expand Down Expand Up @@ -671,7 +685,7 @@ private Task ViewGitItemAsync(GitItemStatus file, ObjectId? objectId, FileStatus

string GetFileTextIfBlobExists()
{
FilePreamble = new byte[] { };
FilePreamble = [];
return file.TreeGuid is not null ? Module.GetFileText(file.TreeGuid, Encoding) : string.Empty;
}

Expand Down Expand Up @@ -1166,17 +1180,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(_cannotViewImage.Text, fileName, text, openWithDifftool);
return;
}
Expand Down
16 changes: 16 additions & 0 deletions src/app/GitUI/Translation/English.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1535,10 +1535,26 @@ The primary difftool can still be selected by clicking the main menu entry.</sou
</file>
<file datatype="plaintext" original="FileViewer" source-language="en">
<body>
<trans-unit id="_binaryFile.Text">
<source>Binary file: {0}</source>
<target />
</trans-unit>
<trans-unit id="_binaryFileDetected.Text">
<source>Binary file: {0} (Detected)</source>
<target />
</trans-unit>
<trans-unit id="_bytes.Text">
<source>bytes</source>
<target />
</trans-unit>
<trans-unit id="_cannotViewImage.Text">
<source>Cannot view image {0}</source>
<target />
</trans-unit>
<trans-unit id="_fileSizeInMb.Text">
<source>MB</source>
<target />
</trans-unit>
<trans-unit id="_largeFileSizeWarning.Text">
<source>This file is {0:N1} MB. Showing large files can be slow. Click to show anyway.</source>
<target />
Expand Down

0 comments on commit c15e105

Please sign in to comment.