Skip to content

Commit 1040bbe

Browse files
committed
feature: add buttons to increase/decrease visible lines in text diff view (#145)
1 parent 8d27690 commit 1040bbe

File tree

8 files changed

+77
-30
lines changed

8 files changed

+77
-30
lines changed

src/Commands/Diff.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public partial class Diff : Command
1212
private const string PREFIX_LFS_DEL = "-version https://git-lfs.github.com/spec/";
1313
private const string PREFIX_LFS_MODIFY = " version https://git-lfs.github.com/spec/";
1414

15-
public Diff(string repo, Models.DiffOption opt)
15+
public Diff(string repo, Models.DiffOption opt, int unified)
1616
{
1717
WorkingDirectory = repo;
1818
Context = repo;
19-
Args = $"diff --ignore-cr-at-eol --unified=4 {opt}";
19+
Args = $"diff --ignore-cr-at-eol --unified={unified} {opt}";
2020
}
2121

2222
public Models.DiffResult Result()

src/Converters/IntConverters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public static class IntConverters
77
public static readonly FuncValueConverter<int, bool> IsGreaterThanZero =
88
new FuncValueConverter<int, bool>(v => v > 0);
99

10+
public static readonly FuncValueConverter<int, bool> IsGreaterThanFour =
11+
new FuncValueConverter<int, bool>(v => v > 4);
12+
1013
public static readonly FuncValueConverter<int, bool> IsZero =
1114
new FuncValueConverter<int, bool>(v => v == 0);
1215

src/Resources/Icons.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@
9595
<StreamGeometry x:Key="Icons.GitFlow.Feature">M939 94v710L512 998 85 805V94h-64A21 21 0 010 73v-0C0 61 10 51 21 51h981c12 0 21 10 21 21v0c0 12-10 21-21 21h-64zm-536 588L512 624l109 58c6 3 13 4 20 3a32 32 0 0026-37l-21-122 88-87c5-5 8-11 9-18a32 32 0 00-27-37l-122-18-54-111a32 32 0 00-57 0l-54 111-122 18c-7 1-13 4-18 9a33 33 0 001 46l88 87-21 122c-1 7-0 14 3 20a32 32 0 0043 14z</StreamGeometry>
9696
<StreamGeometry x:Key="Icons.GitFlow.Hotfix">M236 542a32 32 0 109 63l86-12a180 180 0 0022 78l-71 47a32 32 0 1035 53l75-50a176 176 0 00166 40L326 529zM512 16C238 16 16 238 16 512s222 496 496 496 496-222 496-496S786 16 512 16zm0 896c-221 0-400-179-400-400a398 398 0 0186-247l561 561A398 398 0 01512 912zm314-154L690 622a179 179 0 004-29l85 12a32 32 0 109-63l-94-13v-49l94-13a32 32 0 10-9-63l-87 12a180 180 0 00-20-62l71-47A32 32 0 10708 252l-75 50a181 181 0 00-252 10l-115-115A398 398 0 01512 112c221 0 400 179 400 400a398 398 0 01-86 247z</StreamGeometry>
9797
<StreamGeometry x:Key="Icons.GitFlow.Release">M884 159l-18-18a43 43 0 00-38-12l-235 43a166 166 0 00-101 60L400 349a128 128 0 00-148 47l-120 171a21 21 0 005 29l17 12a128 128 0 00178-32l27-38 124 124-38 27a128 128 0 00-32 178l12 17a21 21 0 0029 5l171-120a128 128 0 0047-148l117-92A166 166 0 00853 431l43-235a43 43 0 00-12-38zm-177 249a64 64 0 110-90 64 64 0 010 90zm-373 312a21 21 0 010 30l-139 139a21 21 0 01-30 0l-30-30a21 21 0 010-30l139-139a21 21 0 0130 0z</StreamGeometry>
98+
<StreamGeometry x:Key="Icons.Lines.Incr">M408 232C408 210 426 192 448 192h416a40 40 0 110 80H448a40 40 0 01-40-40zM408 512c0-22 18-40 40-40h416a40 40 0 110 80H448A40 40 0 01408 512zM448 752A40 40 0 00448 832h416a40 40 0 100-80H448zM32 480l132 0 0-128 64 0 0 128 132 0 0 64-132 0 0 128-64 0 0-128-132 0Z</StreamGeometry>
99+
<StreamGeometry x:Key="Icons.Lines.Decr">M408 232C408 210 426 192 448 192h416a40 40 0 110 80H448a40 40 0 01-40-40zM408 512c0-22 18-40 40-40h416a40 40 0 110 80H448A40 40 0 01408 512zM448 752A40 40 0 00448 832h416a40 40 0 100-80H448zM32 480l328 0 0 64-328 0Z</StreamGeometry>
98100
</ResourceDictionary>

src/Resources/Locales/en_US.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
<x:String x:Key="Text.Diff.SideBySide" xml:space="preserve">Side-By-Side Diff</x:String>
160160
<x:String x:Key="Text.Diff.SyntaxHighlight" xml:space="preserve">Syntax Highlighting</x:String>
161161
<x:String x:Key="Text.Diff.UseMerger" xml:space="preserve">Open In Merge Tool</x:String>
162+
<x:String x:Key="Text.Diff.VisualLines.Decr" xml:space="preserve">Decrease Number of Visible Lines</x:String>
163+
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">Increase Number of Visible Lines</x:String>
162164
<x:String x:Key="Text.Diff.Welcome" xml:space="preserve">SELECT FILE TO VIEW CHANGES</x:String>
163165
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">Open In Merge Tool</x:String>
164166
<x:String x:Key="Text.Discard" xml:space="preserve">Discard Changes</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
<x:String x:Key="Text.Diff.SideBySide" xml:space="preserve">分列对比</x:String>
160160
<x:String x:Key="Text.Diff.SyntaxHighlight" xml:space="preserve">语法高亮</x:String>
161161
<x:String x:Key="Text.Diff.UseMerger" xml:space="preserve">使用外部合并工具查看</x:String>
162+
<x:String x:Key="Text.Diff.VisualLines.Decr" xml:space="preserve">减少可见的行数</x:String>
163+
<x:String x:Key="Text.Diff.VisualLines.Incr" xml:space="preserve">增加可见的行数</x:String>
162164
<x:String x:Key="Text.Diff.Welcome" xml:space="preserve">请选择需要对比的文件</x:String>
163165
<x:String x:Key="Text.DiffWithMerger" xml:space="preserve">使用外部比对工具查看</x:String>
164166
<x:String x:Key="Text.Discard" xml:space="preserve">放弃更改确认</x:String>

src/Resources/Styles.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@
245245
<Style Selector="Button.icon_button:pointerover /template/ ContentPresenter#PART_ContentPresenter, RepeatButton.icon_button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
246246
<Setter Property="Opacity" Value="1"/>
247247
</Style>
248+
<Style Selector="Button.icon_button:disabled Path">
249+
<Setter Property="Fill" Value="{DynamicResource Brush.FG2}"/>
250+
</Style>
248251

249252
<Style Selector="Button.flat">
250253
<Setter Property="BorderThickness" Value="0"/>

src/ViewModels/DiffContext.cs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Threading.Tasks;
45

@@ -63,6 +64,16 @@ public Vector SyncScrollOffset
6364
set => SetProperty(ref _syncScrollOffset, value);
6465
}
6566

67+
public int Unified
68+
{
69+
get => _unified;
70+
set
71+
{
72+
if (SetProperty(ref _unified, value))
73+
LoadDiffContent();
74+
}
75+
}
76+
6677
public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null)
6778
{
6879
_repo = repo;
@@ -74,9 +85,40 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous =
7485
_content = previous._content;
7586
}
7687

88+
LoadDiffContent();
89+
}
90+
91+
public void IncrUnified()
92+
{
93+
Unified = _unified + 1;
94+
}
95+
96+
public void DecrUnified()
97+
{
98+
Unified = Math.Max(4, _unified - 1);
99+
}
100+
101+
public void OpenExternalMergeTool()
102+
{
103+
var type = Preference.Instance.ExternalMergeToolType;
104+
var exec = Preference.Instance.ExternalMergeToolPath;
105+
106+
var tool = Models.ExternalMerger.Supported.Find(x => x.Type == type);
107+
if (tool == null || !File.Exists(exec))
108+
{
109+
App.RaiseException(_repo, "Invalid merge tool in preference setting!");
110+
return;
111+
}
112+
113+
var args = tool.Type != 0 ? tool.DiffCmd : Preference.Instance.ExternalMergeToolDiffCmd;
114+
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
115+
}
116+
117+
private void LoadDiffContent()
118+
{
77119
Task.Run(() =>
78120
{
79-
var latest = new Commands.Diff(repo, option).Result();
121+
var latest = new Commands.Diff(_repo, _option, _unified).Result();
80122
var rs = null as object;
81123

82124
if (latest.TextDiff != null)
@@ -92,31 +134,31 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous =
92134
if (IMG_EXTS.Contains(ext))
93135
{
94136
var imgDiff = new Models.ImageDiff();
95-
if (option.Revisions.Count == 2)
137+
if (_option.Revisions.Count == 2)
96138
{
97-
imgDiff.Old = BitmapFromRevisionFile(repo, option.Revisions[0], oldPath);
98-
imgDiff.New = BitmapFromRevisionFile(repo, option.Revisions[1], oldPath);
139+
imgDiff.Old = BitmapFromRevisionFile(_repo, _option.Revisions[0], oldPath);
140+
imgDiff.New = BitmapFromRevisionFile(_repo, _option.Revisions[1], oldPath);
99141
}
100142
else
101143
{
102-
var fullPath = Path.Combine(repo, _option.Path);
103-
imgDiff.Old = BitmapFromRevisionFile(repo, "HEAD", oldPath);
144+
var fullPath = Path.Combine(_repo, _option.Path);
145+
imgDiff.Old = BitmapFromRevisionFile(_repo, "HEAD", oldPath);
104146
imgDiff.New = File.Exists(fullPath) ? new Bitmap(fullPath) : null;
105147
}
106148
rs = imgDiff;
107149
}
108150
else
109151
{
110152
var binaryDiff = new Models.BinaryDiff();
111-
if (option.Revisions.Count == 2)
153+
if (_option.Revisions.Count == 2)
112154
{
113-
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, option.Revisions[0]).Result();
114-
binaryDiff.NewSize = new Commands.QueryFileSize(repo, _option.Path, option.Revisions[1]).Result();
155+
binaryDiff.OldSize = new Commands.QueryFileSize(_repo, oldPath, _option.Revisions[0]).Result();
156+
binaryDiff.NewSize = new Commands.QueryFileSize(_repo, _option.Path, _option.Revisions[1]).Result();
115157
}
116158
else
117159
{
118-
var fullPath = Path.Combine(repo, _option.Path);
119-
binaryDiff.OldSize = new Commands.QueryFileSize(repo, oldPath, "HEAD").Result();
160+
var fullPath = Path.Combine(_repo, _option.Path);
161+
binaryDiff.OldSize = new Commands.QueryFileSize(_repo, oldPath, "HEAD").Result();
120162
binaryDiff.NewSize = File.Exists(fullPath) ? new FileInfo(fullPath).Length : 0;
121163
}
122164
rs = binaryDiff;
@@ -146,22 +188,6 @@ public DiffContext(string repo, Models.DiffOption option, DiffContext previous =
146188
});
147189
}
148190

149-
public void OpenExternalMergeTool()
150-
{
151-
var type = Preference.Instance.ExternalMergeToolType;
152-
var exec = Preference.Instance.ExternalMergeToolPath;
153-
154-
var tool = Models.ExternalMerger.Supported.Find(x => x.Type == type);
155-
if (tool == null || !File.Exists(exec))
156-
{
157-
App.RaiseException(_repo, "Invalid merge tool in preference setting!");
158-
return;
159-
}
160-
161-
var args = tool.Type != 0 ? tool.DiffCmd : Preference.Instance.ExternalMergeToolDiffCmd;
162-
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo, exec, args, _option));
163-
}
164-
165191
private Bitmap BitmapFromRevisionFile(string repo, string revision, string file)
166192
{
167193
var stream = Commands.QueryFileContent.Run(repo, revision, file);
@@ -181,5 +207,6 @@ private Bitmap BitmapFromRevisionFile(string repo, string revision, string file)
181207
private bool _isTextDiff = false;
182208
private object _content = null;
183209
private Vector _syncScrollOffset = Vector.Zero;
210+
private int _unified = 4;
184211
}
185212
}

src/Views/DiffView.axaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434

3535
<!-- Toolbar Buttons -->
3636
<StackPanel Grid.Column="3" Margin="8,0,0,0" Orientation="Horizontal" VerticalAlignment="Center">
37+
<Button Classes="icon_button" Width="32" Command="{Binding IncrUnified}" IsVisible="{Binding IsTextDiff}" ToolTip.Tip="{DynamicResource Text.Diff.VisualLines.Incr}">
38+
<Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Lines.Incr}"/>
39+
</Button>
40+
41+
<Button Classes="icon_button" Width="32" Command="{Binding DecrUnified}" IsVisible="{Binding IsTextDiff}" ToolTip.Tip="{DynamicResource Text.Diff.VisualLines.Decr}" IsEnabled="{Binding Unified, Converter={x:Static c:IntConverters.IsGreaterThanFour}}">
42+
<Path Width="12" Height="12" Stretch="Uniform" Margin="0,6,0,0" Data="{StaticResource Icons.Lines.Decr}"/>
43+
</Button>
44+
3745
<ToggleButton Classes="line_path"
3846
Width="32" Height="18"
3947
Background="Transparent"

0 commit comments

Comments
 (0)