Skip to content

Commit

Permalink
Add string column builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nh43de committed Mar 13, 2023
1 parent fc83368 commit fc39e04
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
70 changes: 70 additions & 0 deletions DataToolChain.Ui/StringColumnBuilder/StringColumnBuilder.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<Window x:Class="DataToolChain.StringColumnBuilder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataToolChain"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:StringColumnBuilderViewModel, IsDesignTimeCreatable=True}"
Title="StringColumnBuilder" Height="650" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="395*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="424*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="424*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<StackPanel Background="Beige" Grid.ColumnSpan="5">
<TextBlock TextWrapping="Wrap" Margin="5">(Top) Enter List of Regexes on the Left, and their corresponding replacements on the right (if no replacement value specified then will remove). (Bottom) Enter text on left, new text appears on right.</TextBlock>
<StackPanel Margin="5">

<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsCaseSensitive}">Match Case</CheckBox>
<CheckBox IsChecked="{Binding Path=RemoveEmpty}" Margin="10,0,0,0">Remove empty entries</CheckBox>
</StackPanel>

<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=UseRegex}">Use Regex</CheckBox>
<CheckBox IsChecked="{Binding Path=Multiline}" Margin="10,0,0,0">Match newline</CheckBox>
</StackPanel>
</StackPanel>
</StackPanel>

<GridSplitter Grid.Column="1" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>

<Grid Grid.Column="0" Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="410*"/>
</Grid.RowDefinitions>
<TextBox AcceptsReturn="False" AcceptsTab="True" TextWrapping="NoWrap" MaxLines="1" Padding="5" Text="{Binding Path=Separator, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<GridSplitter Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
<TextBox AcceptsReturn="True" Grid.Row="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringInput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</Grid>

<TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="2" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=ExistingData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

<GridSplitter Grid.Column="3" Grid.Row="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>

<TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="4" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StringOutput, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

<!--<Grid Grid.Column="0" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="410*"/>
</Grid.RowDefinitions>
<TextBox AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=RegexMatchInputs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<GridSplitter Grid.Column="1" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
</Grid>-->

</Grid>
</Window>
131 changes: 131 additions & 0 deletions DataToolChain.Ui/StringColumnBuilder/StringColumnBuilder.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Windows;

namespace DataToolChain
{
/// <summary>
/// Interaction logic for RegexMatcher.xaml
/// </summary>
public partial class StringColumnBuilder : Window
{
public StringColumnBuilderViewModel _viewModel { get; set; } = new StringColumnBuilderViewModel();

public StringColumnBuilder()
{
InitializeComponent();

this.DataContext = _viewModel;
}
}


public class StringColumnBuilderViewModel : INotifyPropertyChanged
{
private string _stringOutput;
private string _stringInput = @"red
blue
cyan
magenta";
private string _existingData = @"apple
dog
cat
fruit";
private string _separator = @"\t";

private bool _useRegex = true;
private bool _isCaseSensitive = false;
private bool _multiline;
private bool _removeEmpty;
public event PropertyChangedEventHandler PropertyChanged;

public bool UseRegex
{
get { return _useRegex; }
set
{
_useRegex = value;
UpdateOutput();
}
}

public string Separator
{
get { return _separator; }
set
{
_separator = value;
UpdateOutput();
}
}

public string StringInput
{
get { return _stringInput; }
set
{
_stringInput = value;
UpdateOutput();
}
}

public string ExistingData
{
get { return _existingData; }
set
{
_existingData = value;
UpdateOutput();
}
}

public string StringOutput
{
get { return _stringOutput; }
set
{
_stringOutput = value;
OnPropertyChanged();
}
}

public StringColumnBuilderViewModel()
{
UpdateOutput();
}

private static readonly Regex LinesRegex = new Regex("\r\n?", RegexOptions.Compiled);

public void UpdateOutput()
{
var matchesInput = Separator;

try
{
//loop through existing data and append input into output

var inputLines = LinesRegex.Split(StringInput);
var dataLines = LinesRegex.Split(ExistingData);

var sepString = UseRegex ? Regex.Unescape(Separator) : Separator;

StringOutput = string.Join("\r\n", inputLines.Zip(dataLines, (input, data) => input + sepString + data));
}
catch (Exception)
{
StringOutput = "Error in regex.";
return;
}
}

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

}
}

0 comments on commit fc39e04

Please sign in to comment.