Change the way to preview binary in CommitViewer

This commit is contained in:
leo 2020-07-13 20:41:17 +08:00
parent 261b4d635c
commit 689b02cd61
3 changed files with 34 additions and 17 deletions

View file

@ -190,19 +190,19 @@ namespace SourceGit.Git {
/// <param name="repo"></param> /// <param name="repo"></param>
/// <param name="file"></param> /// <param name="file"></param>
/// <returns></returns> /// <returns></returns>
public string GetTextFileContent(Repository repo, string file) { public string GetTextFileContent(Repository repo, string file, out bool isBinary) {
var data = new List<string>(); var data = new List<string>();
var isBinary = false;
var count = 0; var count = 0;
var binary = false;
var errs = repo.RunCommand($"show {SHA}:\"{file}\"", line => { var errs = repo.RunCommand($"show {SHA}:\"{file}\"", line => {
if (isBinary) return; if (binary) return;
count++; count++;
if (data.Count >= 1000) return; if (data.Count >= 1000) return;
if (line.IndexOf('\0') >= 0) { if (line.IndexOf('\0') >= 0) {
isBinary = true; binary = true;
data.Clear(); data.Clear();
data.Add("BINARY FILE PREVIEW NOT SUPPORTED!"); data.Add("BINARY FILE PREVIEW NOT SUPPORTED!");
return; return;
@ -211,11 +211,13 @@ namespace SourceGit.Git {
data.Add(line); data.Add(line);
}); });
if (!isBinary && count > 1000) { if (!binary && count > 1000) {
data.Add("..."); data.Add("...");
data.Add($"Total {count} lines. Hide {count-1000} lines."); data.Add($"Total {count} lines. Hide {count-1000} lines.");
} }
isBinary = binary;
if (errs != null) App.RaiseError(errs); if (errs != null) App.RaiseError(errs);
return string.Join("\n", data); return string.Join("\n", data);
} }

View file

@ -392,16 +392,24 @@
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"/> <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"/>
<Border Grid.Column="2" BorderThickness="1" Margin="2,0" BorderBrush="{StaticResource Brush.Border2}"> <Border Grid.Column="2" BorderThickness="1" Margin="2,0" BorderBrush="{StaticResource Brush.Border2}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Grid>
<TextBlock FontSize="10pt" <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
FontFamily="Consolas" <TextBlock
Padding="8" FontSize="10pt"
Opacity="0.8" FontFamily="Consolas"
Background="{StaticResource Brush.BG2}" Padding="8"
Foreground="{StaticResource Brush.FG}" Opacity="0.8"
x:Name="filePreview"/> Background="{StaticResource Brush.BG2}"
</ScrollViewer> Foreground="{StaticResource Brush.FG}"
</Border> x:Name="filePreview"/>
</ScrollViewer>
<StackPanel x:Name="maskPreviewNotSupported" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Collapsed">
<Path Width="64" Height="64" Style="{StaticResource Style.Icon}" Data="{StaticResource Icon.Info}" Fill="{StaticResource Brush.FG2}"/>
<Label Margin="0,16,0,0" Content="BINARY FILE DETECTED" FontFamily="Consolas" FontSize="18" FontWeight="UltraBold" HorizontalAlignment="Center" Foreground="{StaticResource Brush.FG2}"/>
</StackPanel>
</Grid>
</Border>
</Grid> </Grid>
</TabItem> </TabItem>
</TabControl> </TabControl>

View file

@ -345,13 +345,20 @@ namespace SourceGit.UI {
private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs<object> e) { private async void FileTreeItemSelected(object sender, RoutedPropertyChangedEventArgs<object> e) {
filePreview.Text = ""; filePreview.Text = "";
maskPreviewNotSupported.Visibility = Visibility.Collapsed;
var node = e.NewValue as Node; var node = e.NewValue as Node;
if (node == null || !node.IsFile) return; if (node == null || !node.IsFile) return;
await Task.Run(() => { await Task.Run(() => {
var data = commit.GetTextFileContent(repo, node.FilePath); var isBinary = false;
Dispatcher.Invoke(() => filePreview.Text = data); var data = commit.GetTextFileContent(repo, node.FilePath, out isBinary);
if (isBinary) {
Dispatcher.Invoke(() => maskPreviewNotSupported.Visibility = Visibility.Visible);
} else {
Dispatcher.Invoke(() => filePreview.Text = data);
}
}); });
} }
#endregion #endregion