Cat it

alphons <alphons@heijden.com> 13 May 2025, 13:19
3d546a203b2d045f88b0ff84a07d8eb31476a8f2
4 files changed
  • GrokCatter/Data/mustexist.txt
  • GrokCatter/Form1.Designer.cs
  • GrokCatter/Form1.cs
  • GrokCatter/GrokCatter.csproj
diff --git a/GrokCatter/Data/mustexist.txt b/GrokCatter/Data/mustexist.txt
new file mode 100644
index 0000000..5f28270
--- /dev/null
+++ b/GrokCatter/Data/mustexist.txt
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/GrokCatter/Form1.Designer.cs b/GrokCatter/Form1.Designer.cs
index 8cb0e16..7ffbb2c 100644
--- a/GrokCatter/Form1.Designer.cs
+++ b/GrokCatter/Form1.Designer.cs
@@ -36,26 +36,33 @@
button3 = new Button();
checkBox1 = new CheckBox();
button4 = new Button();
+ splitContainer1 = new SplitContainer();
+ listView2 = new ListView();
+ comboBox1 = new ComboBox();
+ button5 = new Button();
+ ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
+ splitContainer1.Panel1.SuspendLayout();
+ splitContainer1.Panel2.SuspendLayout();
+ splitContainer1.SuspendLayout();
SuspendLayout();
//
// button1
//
- button1.Location = new Point(12, 41);
+ button1.Location = new Point(12, 40);
button1.Name = "button1";
button1.Size = new Size(79, 23);
button1.TabIndex = 0;
button1.Text = "List Files";
button1.UseVisualStyleBackColor = true;
- button1.Click += button1_Click;
+ button1.Click += Button1_Click;
//
// textBox1
//
textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBox1.Location = new Point(12, 12);
textBox1.Name = "textBox1";
- textBox1.Size = new Size(723, 23);
+ textBox1.Size = new Size(657, 23);
textBox1.TabIndex = 1;
- textBox1.Text = "D:\\moneywise\\git\\2_ControllersCore\\MongoDb";
//
// textBox2
//
@@ -63,84 +70,145 @@
textBox2.AcceptsTab = true;
textBox2.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
textBox2.Font = new Font("Courier New", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
- textBox2.Location = new Point(247, 70);
+ textBox2.Location = new Point(159, 3);
textBox2.Multiline = true;
textBox2.Name = "textBox2";
textBox2.ScrollBars = ScrollBars.Both;
- textBox2.Size = new Size(488, 344);
+ textBox2.Size = new Size(320, 384);
textBox2.TabIndex = 2;
//
// listView1
//
- listView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
listView1.CheckBoxes = true;
+ listView1.Dock = DockStyle.Fill;
listView1.GridLines = true;
- listView1.Location = new Point(12, 70);
+ listView1.HeaderStyle = ColumnHeaderStyle.None;
+ listView1.Location = new Point(0, 0);
listView1.Name = "listView1";
- listView1.Size = new Size(227, 344);
+ listView1.Size = new Size(150, 390);
listView1.TabIndex = 3;
listView1.UseCompatibleStateImageBehavior = false;
listView1.View = View.Details;
+ listView1.ItemChecked += ListView1_ItemChecked;
//
// button2
//
- button2.Anchor = AnchorStyles.Top | AnchorStyles.Right;
- button2.Location = new Point(247, 41);
+ button2.Location = new Point(396, 40);
button2.Name = "button2";
- button2.Size = new Size(79, 23);
+ button2.Size = new Size(73, 23);
button2.TabIndex = 4;
button2.Text = "Cat";
button2.UseVisualStyleBackColor = true;
- button2.Click += button2_Click;
+ button2.Click += Button2_Click;
//
// button3
//
button3.Anchor = AnchorStyles.Top | AnchorStyles.Right;
- button3.Location = new Point(534, 41);
+ button3.Location = new Point(485, 40);
button3.Name = "button3";
button3.Size = new Size(79, 23);
button3.TabIndex = 5;
button3.Text = "Clear";
button3.UseVisualStyleBackColor = true;
- button3.Click += button3_Click;
+ button3.Click += Button3_Click;
//
// checkBox1
//
checkBox1.AutoSize = true;
- checkBox1.Location = new Point(108, 44);
+ checkBox1.Location = new Point(108, 43);
checkBox1.Name = "checkBox1";
checkBox1.Size = new Size(38, 19);
checkBox1.TabIndex = 6;
checkBox1.Text = "all";
checkBox1.UseVisualStyleBackColor = true;
- checkBox1.CheckedChanged += checkBox1_CheckedChanged;
+ checkBox1.CheckedChanged += CheckBox1_CheckedChanged;
//
// button4
//
button4.Anchor = AnchorStyles.Top | AnchorStyles.Right;
- button4.Location = new Point(656, 41);
+ button4.Location = new Point(590, 40);
button4.Name = "button4";
button4.Size = new Size(79, 23);
button4.TabIndex = 7;
button4.Text = "Copy";
button4.UseVisualStyleBackColor = true;
- button4.Click += button4_Click;
+ button4.Click += Button4_Click;
+ //
+ // splitContainer1
+ //
+ splitContainer1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
+ splitContainer1.FixedPanel = FixedPanel.Panel1;
+ splitContainer1.Location = new Point(12, 69);
+ splitContainer1.Name = "splitContainer1";
+ //
+ // splitContainer1.Panel1
+ //
+ splitContainer1.Panel1.Controls.Add(listView1);
+ //
+ // splitContainer1.Panel2
+ //
+ splitContainer1.Panel2.Controls.Add(listView2);
+ splitContainer1.Panel2.Controls.Add(textBox2);
+ splitContainer1.Size = new Size(660, 390);
+ splitContainer1.SplitterDistance = 150;
+ splitContainer1.SplitterWidth = 8;
+ splitContainer1.TabIndex = 8;
+ //
+ // listView2
+ //
+ listView2.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
+ listView2.CheckBoxes = true;
+ listView2.GridLines = true;
+ listView2.HeaderStyle = ColumnHeaderStyle.None;
+ listView2.Location = new Point(3, 3);
+ listView2.Name = "listView2";
+ listView2.Size = new Size(150, 384);
+ listView2.TabIndex = 4;
+ listView2.UseCompatibleStateImageBehavior = false;
+ listView2.View = View.Details;
+ //
+ // comboBox1
+ //
+ comboBox1.FormattingEnabled = true;
+ comboBox1.Items.AddRange(new object[] { "example1" });
+ comboBox1.Location = new Point(172, 39);
+ comboBox1.Name = "comboBox1";
+ comboBox1.Size = new Size(151, 23);
+ comboBox1.TabIndex = 9;
+ comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
+ //
+ // button5
+ //
+ button5.Location = new Point(329, 40);
+ button5.Name = "button5";
+ button5.Size = new Size(47, 23);
+ button5.TabIndex = 10;
+ button5.Text = "Save";
+ button5.UseVisualStyleBackColor = true;
+ button5.Click += Button5_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(747, 426);
+ ClientSize = new Size(684, 471);
+ Controls.Add(button5);
+ Controls.Add(comboBox1);
+ Controls.Add(splitContainer1);
Controls.Add(button4);
Controls.Add(checkBox1);
Controls.Add(button3);
Controls.Add(button2);
- Controls.Add(listView1);
- Controls.Add(textBox2);
Controls.Add(textBox1);
Controls.Add(button1);
+ MinimumSize = new Size(700, 0);
Name = "Form1";
- Text = "Form1";
+ Text = "Cat the files";
+ splitContainer1.Panel1.ResumeLayout(false);
+ splitContainer1.Panel2.ResumeLayout(false);
+ splitContainer1.Panel2.PerformLayout();
+ ((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();
+ splitContainer1.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
@@ -155,5 +223,9 @@
private Button button3;
private CheckBox checkBox1;
private Button button4;
+ private SplitContainer splitContainer1;
+ private ListView listView2;
+ private ComboBox comboBox1;
+ private Button button5;
}
}
diff --git a/GrokCatter/Form1.cs b/GrokCatter/Form1.cs
index b189a9b..2e31bbf 100644
--- a/GrokCatter/Form1.cs
+++ b/GrokCatter/Form1.cs
@@ -1,26 +1,40 @@
+using System.Diagnostics;
+using System.Text.Json;
+
namespace GrokCatter
{
public partial class Form1 : Form
{
+ private readonly string Data = Path.Combine(AppContext.BaseDirectory, "Data");
public Form1()
{
InitializeComponent();
- listView1.Columns.Add("Name", 200);
+ listView1.Columns.Add("Name", 150);
+
+ listView2.Columns.Add("Name", 150);
+
+ FillComboBox();
+ }
+
+ private void FillComboBox()
+ {
+ this.comboBox1.Items.Clear();
+ var files = Directory.GetFiles(Data, "*.json");
+ this.comboBox1.Items.AddRange([.. files.Select(x => Path.GetFileNameWithoutExtension(x))]);
}
- private void button1_Click(object sender, EventArgs e)
+ private void Button1_Click(object sender, EventArgs e)
{
- Doit();
+ ShowFiles(sender, e);
}
- private void Doit()
+ private void ShowFiles(object sender, EventArgs e)
{
try
{
this.listView1.Items.Clear();
-
if (!Directory.Exists(this.textBox1.Text))
{
MessageBox.Show("Invalid directory path!", "Error",
@@ -28,7 +42,6 @@ namespace GrokCatter
return;
}
-
var files = Directory.GetFiles(textBox1.Text);
foreach (var file in files)
@@ -40,6 +53,8 @@ namespace GrokCatter
listView1.Items.Add(item);
}
+
+ CheckBox1_CheckedChanged(sender, e);
}
catch (Exception ex)
{
@@ -48,14 +63,14 @@ namespace GrokCatter
}
}
- private void button3_Click(object sender, EventArgs e)
+ private void Button3_Click(object sender, EventArgs e)
{
this.textBox2.Clear();
}
- private async void button2_Click(object sender, EventArgs e)
+ private async void Button2_Click(object sender, EventArgs e)
{
- var selectedItems = listView1.CheckedItems.Cast<ListViewItem>().ToList();
+ var selectedItems = listView2.Items.Cast<ListViewItem>().ToList();
foreach (var selectedItem in selectedItems)
{
var file = selectedItem?.Tag?.ToString();
@@ -69,7 +84,7 @@ namespace GrokCatter
}
}
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
+ private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewItem lvi in this.listView1.Items)
{
@@ -77,9 +92,130 @@ namespace GrokCatter
}
}
- private void button4_Click(object sender, EventArgs e)
+ private void Button4_Click(object sender, EventArgs e)
{
Clipboard.SetText(this.textBox2.Text);
}
+
+ private void ListView1_ItemChecked(object sender, ItemCheckedEventArgs e)
+ {
+ var item = this.listView2
+ .Items
+ .Cast<ListViewItem>()
+ .Where(x => x.Tag?.ToString() == e.Item.Tag?.ToString())
+ .FirstOrDefault();
+
+ if (e.Item.Checked)
+ {
+ if (item == null)
+ this.listView2.Items.Add(new ListViewItem(e.Item.Text)
+ {
+ Tag = e.Item.Tag,
+ Checked = true
+ });
+ }
+ else
+ {
+ if (item != null)
+ this.listView2.Items.Remove(item);
+ }
+ }
+
+
+ private async Task<bool> LoadExistingAsync(string name)
+ {
+ try
+ {
+ this.listView2.Items.Clear();
+
+ var path = Path.Combine(Data, name + ".json");
+ if (!File.Exists(path))
+ return false;
+
+ using var stream = File.OpenRead(path);
+ var list = await JsonSerializer.DeserializeAsync<List<string>>(stream) ?? [];
+ this.listView2.Items.AddRange(
+ [.. list.Select(x => new ListViewItem(Path.GetFileName(x)) { Tag = x, Checked = true })]);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Er gaat iets goed foutig " + ex.Message);
+ return false;
+ }
+ }
+
+ private async Task BackupSaveAndDeleteBackupAsync(string newName)
+ {
+ var path = Path.Combine(Data, newName + ".json");
+ var backup = Path.Combine(newName + ".json.bak");
+ try
+ {
+ if (File.Exists(path))
+ {
+ if (File.Exists(backup))
+ File.Delete(backup);
+ File.Move(path, backup);
+ }
+
+ path = Path.Combine(Data, newName + ".json");
+
+ var items = this.listView2
+ .Items
+ .Cast<ListViewItem>()
+ .Where(x => x.Tag != null && x.Checked)
+ .Select(x => x.Tag?.ToString())
+ .ToList();
+
+ if (items.Count > 0)
+ {
+ using var stream = File.OpenWrite(path);
+ await JsonSerializer.SerializeAsync(stream, items);
+ }
+
+ var todelete = this.listView2
+ .Items
+ .Cast<ListViewItem>()
+ .Where(x => x.Checked == false)
+ .ToList();
+ foreach (var item in todelete)
+ this.listView2.Items.Remove(item);
+
+ if (File.Exists(backup))
+ {
+ File.Delete(backup);
+ Debug.WriteLine($"Save {path} deleted {backup}");
+ }
+ else
+ {
+ Debug.WriteLine($"Save {path} (no backup)");
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Er gaat wat foutig " + ex.Message);
+ }
+ }
+
+ private async void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
+ {
+ await LoadExistingAsync(this.comboBox1.Text);
+ }
+
+ private async void Button5_Click(object sender, EventArgs e)
+ {
+ if (string.IsNullOrWhiteSpace(this.comboBox1.Text))
+ return;
+
+ await BackupSaveAndDeleteBackupAsync(this.comboBox1.Text);
+
+ for(int i=0;i<this.comboBox1.Items.Count;i++)
+ {
+ if (this.comboBox1.Items[i]?.ToString() == this.comboBox1.Text)
+ return;
+ }
+ this.comboBox1.Items.Add(this.comboBox1.Text);
+
+ }
}
}
diff --git a/GrokCatter/GrokCatter.csproj b/GrokCatter/GrokCatter.csproj
index 663fdb8..8b4e868 100644
--- a/GrokCatter/GrokCatter.csproj
+++ b/GrokCatter/GrokCatter.csproj
@@ -8,4 +8,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
+ <ItemGroup>
+ <None Update="Data\mustexist.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+
</Project>
\ No newline at end of file