added "all" argument

This commit is contained in:
liyunze 2026-02-26 10:12:33 +11:00
parent a550d823b1
commit 32ac1b8fd4
2 changed files with 62 additions and 28 deletions

48
Code.cs
View file

@ -95,6 +95,7 @@ public static class TaskHelpers
return tasks.Split(Separators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList(); return tasks.Split(Separators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList();
} }
// return true index
public static int GetTaskIndex(List<Task> tasks, string input) public static int GetTaskIndex(List<Task> tasks, string input)
{ {
if (int.TryParse(input, out int n)) if (int.TryParse(input, out int n))
@ -109,6 +110,14 @@ public static class TaskHelpers
public static List<string> ParseTasksInput(string input, Func<int> getFocusedTask) public static List<string> ParseTasksInput(string input, Func<int> getFocusedTask)
{ {
input = input.Trim(); input = input.Trim();
if (input == "all")
{
return new()
{
"all"
};
}
bool IsSpaceSeparatedInts = Regex.IsMatch(input, @"^\d+(\s+\d+)*$"); bool IsSpaceSeparatedInts = Regex.IsMatch(input, @"^\d+(\s+\d+)*$");
if (IsSpaceSeparatedInts) if (IsSpaceSeparatedInts)
{ {
@ -251,8 +260,13 @@ public static class MessageBuilder
return "No tasks to log"; return "No tasks to log";
} }
public static string BuildRemoveMessage(List<string> tasksRemoved, List<string> tasksFailedToRemove) public static string BuildRemoveMessage(List<string> tasksRemoved, List<string> tasksFailedToRemove, bool allTasks)
{ {
if (allTasks)
{
return "All tasks are removed!";
}
if (tasksFailedToRemove.Count == 0) if (tasksFailedToRemove.Count == 0)
{ {
return "Removed task(s)!"; return "Removed task(s)!";
@ -261,8 +275,12 @@ public static class MessageBuilder
return $"Failed to remove: {String.Join(", ", tasksFailedToRemove)}"; return $"Failed to remove: {String.Join(", ", tasksFailedToRemove)}";
} }
public static string BuildCompletedMessage(List<string> tasksCompleted, List<string> tasksFailedToComplete) public static string BuildCompletedMessage(List<string> tasksCompleted, List<string> tasksFailedToComplete, bool allTasks)
{ {
if (allTasks)
{
return "All tasks are completed!";
}
if (tasksFailedToComplete.Count == 0 && tasksCompleted.Count == 1) if (tasksFailedToComplete.Count == 0 && tasksCompleted.Count == 1)
{ {
return "Task completed!"; return "Task completed!";
@ -270,7 +288,7 @@ public static class MessageBuilder
if (tasksFailedToComplete.Count == 0) if (tasksFailedToComplete.Count == 0)
{ {
return "Completed all task(s) specified!"; return "Completed the task(s)!";
} }
return $"Failed to complete: {String.Join(", ", tasksFailedToComplete)}"; return $"Failed to complete: {String.Join(", ", tasksFailedToComplete)}";
@ -342,6 +360,8 @@ public class TaskOperations
return new Response<(int, string)>(false, default, "Task cannot be empty"); return new Response<(int, string)>(false, default, "Task cannot be empty");
if (int.TryParse(taskName, out _)) if (int.TryParse(taskName, out _))
return new Response<(int, string)>(false, default, $"'{taskName}' cannot be a number"); return new Response<(int, string)>(false, default, $"'{taskName}' cannot be a number");
if (taskName.Equals("all", StringComparison.OrdinalIgnoreCase))
return new Response<(int, string)>(false, default, $"'all' is a reserved keyword");
string key = getKey(); string key = getKey();
if (!taskData.ContainsKey(key)) if (!taskData.ContainsKey(key))
{ {
@ -810,9 +830,12 @@ public class CPHInline
return false; return false;
} }
bool allTasks = tasksToBeRemoved.Count == 1 && tasksToBeRemoved[0] == "all";
var tasksRemoved = new List<string>(); var tasksRemoved = new List<string>();
var tasksFailedToRemove = new List<string>(); var tasksFailedToRemove = new List<string>();
var taskIndices = new List<int>(); var taskIndices = new List<int>();
if (!allTasks)
{
foreach (string task in tasksToBeRemoved) foreach (string task in tasksToBeRemoved)
{ {
int index = TaskHelpers.GetTaskIndex(userTasks, task); int index = TaskHelpers.GetTaskIndex(userTasks, task);
@ -832,6 +855,11 @@ public class CPHInline
Respond($"Failed to remove task(s): {String.Join(", ", tasksFailedToRemove)}"); Respond($"Failed to remove task(s): {String.Join(", ", tasksFailedToRemove)}");
return false; return false;
} }
}
else
{
taskIndices = Enumerable.Range(0, userTasks.Count).ToList();
}
foreach (int i in taskIndices.OrderByDescending(n => n)) foreach (int i in taskIndices.OrderByDescending(n => n))
{ {
@ -842,7 +870,7 @@ public class CPHInline
operations.SaveIntoTasks(userTasks); operations.SaveIntoTasks(userTasks);
operations.Cleanup(true); operations.Cleanup(true);
SaveTasks(); SaveTasks();
Respond(MessageBuilder.BuildRemoveMessage(tasksRemoved, tasksFailedToRemove)); Respond(MessageBuilder.BuildRemoveMessage(tasksRemoved, tasksFailedToRemove, allTasks));
return true; return true;
} }
@ -885,9 +913,12 @@ public class CPHInline
return false; return false;
} }
bool allTasks = tasksToBeCompleted.Count == 1 && tasksToBeCompleted[0] == "all";
var tasksCompleted = new List<string>(); var tasksCompleted = new List<string>();
var tasksFailedToComplete = new List<string>(); var tasksFailedToComplete = new List<string>();
var taskIndices = new List<int>(); var taskIndices = new List<int>();
if (!allTasks)
{
foreach (string task in tasksToBeCompleted) foreach (string task in tasksToBeCompleted)
{ {
int index = TaskHelpers.GetTaskIndex(userTasks, task); int index = TaskHelpers.GetTaskIndex(userTasks, task);
@ -901,6 +932,11 @@ public class CPHInline
tasksFailedToComplete.Add(task); tasksFailedToComplete.Add(task);
} }
} }
}
else
{
taskIndices = Enumerable.Range(0, userTasks.Count).ToList();
}
if (taskIndices.Count == 0) if (taskIndices.Count == 0)
{ {
@ -912,15 +948,13 @@ public class CPHInline
{ {
userTasks[i].Completed = true; userTasks[i].Completed = true;
userTasks[i].Focused = false; userTasks[i].Focused = false;
Broadcast(new { mode = "done", index = i }, null); Broadcast(new { mode = "done", index = i }, null);
} }
IncrementDoneCount(taskIndices.Count); IncrementDoneCount(taskIndices.Count);
operations.SaveIntoTasks(userTasks); operations.SaveIntoTasks(userTasks);
SaveTasks(); SaveTasks();
Respond(MessageBuilder.BuildCompletedMessage(tasksCompleted, tasksFailedToComplete)); Respond(MessageBuilder.BuildCompletedMessage(tasksCompleted, tasksFailedToComplete, allTasks));
return true; return true;
} }

File diff suppressed because one or more lines are too long