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

View file

@ -392,16 +392,24 @@
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"/>
<Border Grid.Column="2" BorderThickness="1" Margin="2,0" BorderBrush="{StaticResource Brush.Border2}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBlock FontSize="10pt"
FontFamily="Consolas"
Padding="8"
Opacity="0.8"
Background="{StaticResource Brush.BG2}"
Foreground="{StaticResource Brush.FG}"
x:Name="filePreview"/>
</ScrollViewer>
</Border>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBlock
FontSize="10pt"
FontFamily="Consolas"
Padding="8"
Opacity="0.8"
Background="{StaticResource Brush.BG2}"
Foreground="{StaticResource Brush.FG}"
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>
</TabItem>
</TabControl>

View file

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