Example #1
0
        public void ReadModel(string oldText, string newText)
        {
            _oldText = oldText ?? "";
            _newText = newText ?? "";

            _diff = SideBySideDiffBuilder.Diff(_oldText, _newText, true, false);
            _invalidate();
        }
Example #2
0
        private async Task <int> CompareFileContents(KVPair pair)
        {
            var filePath = Path.Combine(Common.BaseDirectoryFullPath, pair.Key);

            if (!File.Exists(filePath))
            {
                await Console.Error.WriteLineAsync($"File not exist: {filePath}");

                return(1);
            }

            var table = new Table();

            table.Expand();
            table.HorizontalBorder();
            table.BorderColor(Color.Grey74);
            table.AddColumn(new TableColumn("Consul".ToSilver(isBold: true)));
            table.AddColumn(new TableColumn("Local".ToSilver(isBold: true)));

            var fileContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8);

            var consulContent = Encoding.UTF8.GetString(pair.Value ?? Array.Empty <byte>());
            var diffModel     = SideBySideDiffBuilder.Diff(consulContent, fileContent);

            var oldLines = diffModel.OldText.Lines;
            var newLines = diffModel.NewText.Lines;
            var maxRow   = Math.Max(newLines.Count, oldLines.Count);

            for (var i = 0; i < maxRow; i++)
            {
                var oldLine         = oldLines[i];
                var oldDiffPosition = oldLine.Position.HasValue ? oldLine.Position.Value.ToString() : "  ";
                var oldDiffRow      = GenerateDiff(oldLine);
                oldDiffRow = $"{oldDiffPosition.ToGrey()} {oldDiffRow}";

                var newLine         = newLines[i];
                var newDiffPosition = newLine.Position.HasValue ? newLine.Position.Value.ToString() : "  ";
                var newDiffRow      = GenerateDiff(newLine);
                newDiffRow = $"{newDiffPosition.ToGrey()} {newDiffRow}";

                table.AddRow(oldDiffRow, newDiffRow);
            }

            AnsiConsole.Render(table);
            if (diffModel.NewText.HasDifferences || diffModel.OldText.HasDifferences)
            {
                return(2);
            }

            return(0);
        }
Example #3
0
        /// <summary>
        /// Populate the bottom left and right rich text box with a side-by-side compare.
        /// </summary>
        private void PerformSideBySideDiff()
        {
            var builder = SideBySideDiffBuilder.Diff(OldText, NewText);

            if (builder.OldText.HasDifferences || builder.NewText.HasDifferences)
            {
                foreach (var line in builder.OldText.Lines)
                {
                    if (!string.IsNullOrWhiteSpace(richTextBoxSideLeft.Text))
                    {
                        richTextBoxSideLeft.AppendText(Environment.NewLine);
                    }
                    if (line.SubPieces.Any())
                    {
                        foreach (var piece in line.SubPieces)
                        {
                            AddText(richTextBoxSideLeft, piece);
                        }
                    }
                    else
                    {
                        AddText(richTextBoxSideLeft, line);
                    }
                }

                foreach (var line in builder.NewText.Lines)
                {
                    if (!string.IsNullOrWhiteSpace(richTextBoxSideRight.Text))
                    {
                        richTextBoxSideRight.AppendText(Environment.NewLine);
                    }
                    if (line.SubPieces.Any())
                    {
                        foreach (var piece in line.SubPieces)
                        {
                            AddText(richTextBoxSideRight, piece);
                        }
                    }
                    else
                    {
                        AddText(richTextBoxSideRight, line);
                    }
                }
            }
            else
            {
                richTextBoxSideLeft.AppendText(OldText);
                richTextBoxSideRight.AppendText(NewText);
            }
        }
Example #4
0
        public static void Run()
        {
            var txt1 = Resources.oldState;

            var txt2 = Resources.newState;

            var diff       = InlineDiffBuilder.Diff(txt1, txt2);
            var diff2      = SideBySideDiffBuilder.Diff(txt1, txt2);
            var savedColor = Console.ForegroundColor;

            _printLines(diff2.OldText.Lines);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("-------------------------------------------");
            _printLines(diff2.NewText.Lines);

            Console.ForegroundColor = savedColor;
        }
 public static SideBySideDiffModel DiffText(string t1, string t2)
 {
     return(SideBySideDiffBuilder.Diff(t1, t2));
 }
Example #6
0
        private async void BarView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0 || Bar1 == null || Bar2 == null || barComparer == null)
            {
                return;
            }


            //   e.Handled = true;
            try
            {
                ImageViewer1.Visibility = Visibility.Collapsed;
                ImageViewer2.Visibility = Visibility.Collapsed;
                XMLViewer1.Visibility   = Visibility.Collapsed;
                XMLViewer1.Text         = "";

                XMLViewer2.Visibility = Visibility.Collapsed;
                XMLViewer2.Text       = "";

                BarComparerEntry entry = (BarComparerView.SelectedItem as BarComparerEntry);
                if (entry == null)
                {
                    return;
                }

                BarEntry entryOld = entry.entryOld;
                BarEntry entryNew = entry.entryNew;

                await Bar1.readFile(entryOld);

                await Bar2.readFile(entryNew);


                if (Bar1.Preview != null && Bar2.Preview != null)
                {
                    try
                    {
                        IEnumerable <string> Old = new List <string>();
                        IEnumerable <string> New = new List <string>();
                        await Task.Run(() =>
                        {
                            var diff = SideBySideDiffBuilder.Diff(Bar1.Preview.Text, Bar2.Preview.Text);


                            Old = diff.OldText.Lines.Select(x =>
                            {
                                if (x.Type == ChangeType.Inserted || x.SubPieces.Any(x => x.Type == ChangeType.Inserted))
                                {
                                    return("!+ " + x.Text);
                                }
                                if (x.Type == ChangeType.Deleted || x.SubPieces.Any(x => x.Type == ChangeType.Deleted))
                                {
                                    return("!- " + x.Text);
                                }
                                return("   " + x.Text);
                            }
                                                            );

                            New = diff.NewText.Lines.Select(x =>
                            {
                                if (x.Type == ChangeType.Inserted || x.SubPieces.Any(x => x.Type == ChangeType.Inserted))
                                {
                                    return("!+ " + x.Text);
                                }
                                if (x.Type == ChangeType.Deleted || x.SubPieces.Any(x => x.Type == ChangeType.Deleted))
                                {
                                    return("!- " + x.Text);
                                }
                                return("   " + x.Text);
                            });
                        });

                        XMLViewer1.Text = string.Join("\n", Old);
                        XMLViewer2.Text = string.Join("\n", New);
                    }
                    catch { }
                }

                if (entryNew == null)
                {
                    if (Bar1.Preview != null)
                    {
                        XMLViewer1.Text = Bar1.Preview.Text;
                    }
                }


                if (entryOld == null)
                {
                    if (Bar2.Preview != null)
                    {
                        XMLViewer2.Text = Bar2.Preview.Text;
                    }
                }
                if (entryOld != null)
                {
                    if (entryOld.Extension == ".DDT")
                    {
                        ImagePreview1.Source    = Bar1.PreviewDdt.Bitmap;
                        XMLViewer1.Visibility   = Visibility.Collapsed;
                        ImageViewer1.Visibility = Visibility.Visible;
                    }
                    else
                    if (entryOld.Extension == ".BMP" || entryOld.Extension == ".PNG" || entryOld.Extension == ".CUR" || entryOld.Extension == ".JPG")
                    {
                        ImagePreview1.Source    = Bar1.PreviewImage;
                        XMLViewer1.Visibility   = Visibility.Collapsed;
                        ImageViewer1.Visibility = Visibility.Visible;
                    }
                    else
                    if (entryOld.Extension == ".XMB" || entryOld.Extension == ".XML" || entryOld.Extension == ".SHP" || entryOld.Extension == ".LGT" || entryOld.Extension == ".XS" || entryOld.Extension == ".TXT" || entryOld.Extension == ".CFG" || entryOld.Extension == ".XAML" || entryNew.Extension == ".PY")
                    {
                        ImageViewer1.Visibility = Visibility.Collapsed;
                        XMLViewer1.Visibility   = Visibility.Visible;
                    }
                    else
                    {
                        ImageViewer1.Visibility = Visibility.Collapsed;
                        XMLViewer1.Visibility   = Visibility.Collapsed;
                    }
                }

                if (entryNew != null)
                {
                    if (entryNew.Extension == ".DDT")
                    {
                        ImagePreview2.Source    = Bar2.PreviewDdt.Bitmap;
                        XMLViewer2.Visibility   = Visibility.Collapsed;
                        ImageViewer2.Visibility = Visibility.Visible;
                    }
                    else
                    if (entryNew.Extension == ".BMP" || entryNew.Extension == ".PNG" || entryNew.Extension == ".CUR" || entryNew.Extension == ".JPG")
                    {
                        ImagePreview2.Source    = Bar2.PreviewImage;
                        XMLViewer2.Visibility   = Visibility.Collapsed;
                        ImageViewer2.Visibility = Visibility.Visible;
                    }
                    else
                    if (entryNew.Extension == ".XMB" || entryNew.Extension == ".XML" || entryNew.Extension == ".SHP" || entryNew.Extension == ".LGT" || entryNew.Extension == ".XS" || entryNew.Extension == ".TXT" || entryNew.Extension == ".CFG" || entryNew.Extension == ".XAML" || entryNew.Extension == ".PY")
                    {
                        ImageViewer2.Visibility = Visibility.Collapsed;
                        XMLViewer2.Visibility   = Visibility.Visible;
                    }
                    else
                    {
                        ImageViewer2.Visibility = Visibility.Collapsed;
                        XMLViewer2.Visibility   = Visibility.Collapsed;
                    }
                }
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }