Some more maybe progress? This stuff is hard.

This commit is contained in:
That_One_Nerd 2023-06-01 07:47:22 -04:00
parent 40e7d29f22
commit 45402d6482
2 changed files with 99 additions and 26 deletions

View File

@ -23,11 +23,11 @@ public static class VkvModule
if (!File.Exists(path)) throw new($"No file exists at \"{path}\". Did you mean to run \"vkv create\"?"); if (!File.Exists(path)) throw new($"No file exists at \"{path}\". Did you mean to run \"vkv create\"?");
VkvNode? parentNode; VkvNode? parentNode;
string parentNodeName = "this doesn't work yet."; string parentNodeName = string.Empty;
try try
{ {
FileStream fs = new(path, FileMode.Open); FileStream fs = new(path, FileMode.Open);
parentNode = SerializeVkv.Deserialize(fs); parentNode = SerializeVkv.Deserialize(fs, out parentNodeName);
if (parentNode is null) throw new("Deserialized VKV node is null."); if (parentNode is null) throw new("Deserialized VKV node is null.");
} }
@ -56,20 +56,22 @@ public static class VkvModule
VkvModifyPrintAll(ref context, false); VkvModifyPrintAll(ref context, false);
// Start modifying the parent node. // Start modifying the parent node.
int parentSubIndex = 0; VkvModifyNode(ref parentNode, ref parentNodeName, ref context, true);
VkvModifyNode(ref parentNode, ref parentNodeName, ref context, true, ref parentSubIndex);
// Done editing, let's reset the cursor position and exit the command. // Done editing, let's reset the cursor position and exit the command.
Console.ResetColor(); Console.ResetColor();
Console.SetCursorPosition(0, context.startingCursor + context.displayLines.Count); Console.SetCursorPosition(0, context.startingCursor + context.displayLines.Count);
} }
private static void VkvModifyNode(ref VkvNode node, ref string nodeName, ref VkvModifyContext context, private static VkvModifyReturnOption VkvModifyNode(ref VkvNode node, ref string nodeName,
bool isGlobal, ref int subIndex) ref VkvModifyContext context, bool isGlobal)
{ {
const string add = " started";
Console.Title += add;
VkvModifyMode mode = VkvModifyMode.Default; VkvModifyMode mode = VkvModifyMode.Default;
int doubleSubIndex = -1; // Represents the index of the sub node currently being modified. int subIndex = -1; // Represents the index of the sub node currently being modified.
// If the variable is set to -1, it represents the title. // If the variable is set to -1, it represents the title.
VkvSingleNode? single = node as VkvSingleNode; VkvSingleNode? single = node as VkvSingleNode;
@ -95,29 +97,90 @@ public static class VkvModule
case ConsoleKey.DownArrow: case ConsoleKey.DownArrow:
if (tree is not null) if (tree is not null)
{ {
doubleSubIndex++; subIndex++;
if (doubleSubIndex == 0) if (subIndex == 0)
{ {
// We just shifted down from the title to the first sub node. // We just shifted down from the title to the first sub node.
// We need to overlook the next line, '{'. // We need to overlook the next line, '{'.
context.lineIndex += 2; context.lineIndex += 2;
// Now we also need to start modification of the first sub node.
KeyValuePair<string, VkvNode>? subNode = tree[subIndex];
if (subNode is not null)
{
string subNodeKey = subNode.Value.Key;
VkvNode subNodeValue = subNode.Value.Value;
VkvModifyReturnOption status =
VkvModifyNode(ref subNodeValue, ref subNodeKey, ref context, false);
// Update the parent node with our modified sub node.
tree[subIndex] = new(subNodeKey, subNodeValue);
switch (status)
{
case VkvModifyReturnOption.IncSubIndex:
subIndex++;
context.lineIndex++;
break;
case VkvModifyReturnOption.DecSubIndex:
subIndex--;
context.lineIndex -= 2;
break;
}
}
} }
else if (doubleSubIndex == tree.SubNodeCount + 1) else if (subIndex == tree.SubNodeCount + 1)
{ {
// We're outside the maximum sub nodes. Let's increment the parent // We're outside the maximum sub nodes. Let's increment the parent
// sub index and end this method (and increment the line). // sub index and end this method (and increment the line).
subIndex++;
context.lineIndex++; context.lineIndex++;
return; Console.Title = Console.Title[..^add.Length];
return VkvModifyReturnOption.IncSubIndex;
} }
else else
{ {
// We're in a valid range. Let's just change the sub node we're // We're in a valid range. Let's just change the sub node we're
// focused on. // focused on.
context.lineIndex++; context.lineIndex++;
if (subIndex < tree.SubNodeCount)
{
// We are talking about an already existing node, so we also need
// to start modification of the first sub node.
KeyValuePair<string, VkvNode>? subNode = tree[subIndex];
if (subNode is not null)
{
string subNodeKey = subNode.Value.Key;
VkvNode subNodeValue = subNode.Value.Value;
VkvModifyReturnOption status =
VkvModifyNode(ref subNodeValue, ref subNodeKey, ref context, false);
// Update the parent node with our modified sub node.
tree[subIndex] = new(subNodeKey, subNodeValue);
switch (status)
{
case VkvModifyReturnOption.IncSubIndex:
subIndex++;
context.lineIndex++;
break;
case VkvModifyReturnOption.DecSubIndex:
subIndex--;
context.lineIndex--;
break;
}
}
}
else
{
// TODO: This is where we can decide to add sub nodes.
}
} }
} }
else else
@ -125,27 +188,27 @@ public static class VkvModule
// We aren't in a tree. We just change the parent sub index and // We aren't in a tree. We just change the parent sub index and
// end this method (and increment the line). // end this method (and increment the line).
subIndex++;
context.lineIndex++; context.lineIndex++;
return; Console.Title = Console.Title[..^add.Length];
return VkvModifyReturnOption.IncSubIndex;
} }
break; break;
case ConsoleKey.UpArrow: case ConsoleKey.UpArrow:
if (tree is not null) if (tree is not null)
{ {
doubleSubIndex--; subIndex--;
if (doubleSubIndex == -2) if (subIndex == -2)
{ {
// We're outside the maximum sub nodes. Let's decrement the parent // We're outside the maximum sub nodes. Let's decrement the parent
// sub index and end this method (and decrement the line). // sub index and end this method (and decrement the line).
subIndex--;
context.lineIndex--; context.lineIndex--;
return; Console.Title = Console.Title[..^add.Length];
return VkvModifyReturnOption.DecSubIndex;
} }
else if (doubleSubIndex == -1) else if (subIndex == -1)
{ {
// We just shifted down from the title to the first sub node. // We just shifted down from the title to the first sub node.
// We need to overlook the next line, '{'. // We need to overlook the next line, '{'.
@ -164,9 +227,9 @@ public static class VkvModule
// We aren't in a tree. We just change the parent sub index and // We aren't in a tree. We just change the parent sub index and
// end this method (and decrement the line). // end this method (and decrement the line).
subIndex--;
context.lineIndex--; context.lineIndex--;
return; Console.Title = Console.Title[..^add.Length];
return VkvModifyReturnOption.DecSubIndex;
} }
break; break;
} }
@ -229,5 +292,11 @@ public static class VkvModule
{ {
Default Default
} }
private enum VkvModifyReturnOption
{
Nothing,
IncSubIndex,
DecSubIndex
}
#endregion #endregion
} }

View File

@ -60,23 +60,27 @@ public class VkvTreeNode : VkvNode, IEnumerable<KeyValuePair<string, VkvNode>>
} }
} }
} }
public VkvNode? this[int index] public KeyValuePair<string, VkvNode>? this[int index]
{ {
get get
{ {
if (p_subNodes.Count >= index || index < 0) return null; if (index >= SubNodeCount || index < 0) return null;
return p_subNodes[index]; return new(p_subNodeKeys[index], p_subNodes[index]);
} }
set set
{ {
if (p_subNodes.Count >= index || index < 0) throw new IndexOutOfRangeException(); if (index >= SubNodeCount || index < 0) throw new IndexOutOfRangeException();
if (value is null) if (value is null)
{ {
p_subNodeKeys.RemoveAt(index); p_subNodeKeys.RemoveAt(index);
p_subNodes.RemoveAt(index); p_subNodes.RemoveAt(index);
} }
else p_subNodes[index] = value; else
{
p_subNodeKeys[index] = value.Value.Key;
p_subNodes[index] = value.Value.Value;
}
} }
} }
public KeyValuePair<string, VkvNode>? this[Func<int, KeyValuePair<string, VkvNode>, bool> predicate] public KeyValuePair<string, VkvNode>? this[Func<int, KeyValuePair<string, VkvNode>, bool> predicate]