remove keys, when not active

alphons <alphons@heijden.com> 9 Jan 2025, 17:57
8466afd325e866b669dd66e3be5fe67e9e0768e7
2 files changed
  • RedisGui/Form1.Designer.cs
  • RedisGui/Form1.cs
diff --git a/RedisGui/Form1.Designer.cs b/RedisGui/Form1.Designer.cs
index 18f8848..8ceb390 100644
--- a/RedisGui/Form1.Designer.cs
+++ b/RedisGui/Form1.Designer.cs
@@ -39,6 +39,7 @@
contextMenuStrip1 = new ContextMenuStrip(components);
addConnectionToolStripMenuItem = new ToolStripMenuItem();
imageList1 = new ImageList(components);
+ txtJson = new TextBox();
label3 = new Label();
label2 = new Label();
lblKey = new Label();
@@ -113,6 +114,7 @@
//
// splitContainer2.Panel2
//
+ splitContainer2.Panel2.Controls.Add(txtJson);
splitContainer2.Panel2.Controls.Add(label3);
splitContainer2.Panel2.Controls.Add(label2);
splitContainer2.Panel2.Controls.Add(lblKey);
@@ -154,6 +156,19 @@
imageList1.ImageSize = new Size(24, 24);
imageList1.TransparentColor = Color.Transparent;
//
+ // txtJson
+ //
+ txtJson.AcceptsReturn = true;
+ txtJson.AcceptsTab = true;
+ txtJson.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
+ txtJson.Location = new Point(12, 190);
+ txtJson.Multiline = true;
+ txtJson.Name = "txtJson";
+ txtJson.ReadOnly = true;
+ txtJson.ScrollBars = ScrollBars.Both;
+ txtJson.Size = new Size(507, 129);
+ txtJson.TabIndex = 5;
+ //
// label3
//
label3.AutoSize = true;
@@ -206,12 +221,12 @@
// columnHeader1
//
columnHeader1.Text = "Field";
- columnHeader1.Width = 150;
+ columnHeader1.Width = 75;
//
// columnHeader2
//
columnHeader2.Text = "Value";
- columnHeader2.Width = 250;
+ columnHeader2.Width = 300;
//
// Form1
//
@@ -259,5 +274,6 @@
private Label label3;
private Label label2;
private Label lblKey;
+ private TextBox txtJson;
}
}
diff --git a/RedisGui/Form1.cs b/RedisGui/Form1.cs
index b6767dc..57af14b 100644
--- a/RedisGui/Form1.cs
+++ b/RedisGui/Form1.cs
@@ -2,8 +2,9 @@
using StackExchange.Redis;
using System.Diagnostics;
using System.Net;
+using System.Text.Json;
+using System.Text.RegularExpressions;
-#nullable enable
namespace RedisGui
{
@@ -80,18 +81,45 @@ namespace RedisGui
if (e.Node == null)
return;
- RedisKey key = new RedisKey(e.Node.Text);
+ var key = new RedisKey(e.Node.Text);
+
+ if (this.db == null)
+ return;
+
+ if (e.Node.Parent == null)
+ return;
+
+ if (!db.KeyExists(key))
+ {
+ e.Node.Remove();
+ return;
+ }
ShowValue(key);
}
+ public static string PrettyPrintJson(string json)
+ {
+ try
+ {
+ var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
+ var options = new JsonSerializerOptions { WriteIndented = true };
+ return JsonSerializer.Serialize(jsonElement, options);
+ }
+ catch
+ {
+ return json;
+ }
+ }
+
private void ShowValue(RedisKey key)
{
this.lblType.Text = "";
this.lblKey.Text = "";
this.listView1.Items.Clear();
+ this.txtJson.Clear();
if (this.db == null)
return;
@@ -113,14 +141,19 @@ namespace RedisGui
case RedisType.Hash:
foreach (var entry in db.HashGetAll(key))
{
-
+
var val = entry.Value.ToString();
var name = entry.Name.ToString();
if (name.Contains("absexp") && val != "-1")
val = new DateTime(long.Parse(val)).ToString();
if (name.Contains("sldexp") && val != "-1")
- val = TimeSpan.FromMilliseconds(long.Parse(val)/10000).ToString();
+ val = TimeSpan.FromMilliseconds(long.Parse(val) / 10000).ToString();
+
+ if (val.StartsWith('{') && val.EndsWith('}'))
+ {
+ this.txtJson.Text = Regex.Unescape(PrettyPrintJson(val));
+ }
this.listView1.Items.Add(new ListViewItem([entry.Name.ToString(), val]));
}
@@ -152,6 +185,6 @@ namespace RedisGui
}
-
+
}
}