mirror of
https://github.com/liyunze-coding/rython-task-bot-v2.git
synced 2026-09-22 07:24:27 +00:00
added "all" argument
This commit is contained in:
parent
a550d823b1
commit
32ac1b8fd4
2 changed files with 62 additions and 28 deletions
48
Code.cs
48
Code.cs
|
|
@ -95,6 +95,7 @@ public static class TaskHelpers
|
|||
return tasks.Split(Separators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToList();
|
||||
}
|
||||
|
||||
// return true index
|
||||
public static int GetTaskIndex(List<Task> tasks, string input)
|
||||
{
|
||||
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)
|
||||
{
|
||||
input = input.Trim();
|
||||
if (input == "all")
|
||||
{
|
||||
return new()
|
||||
{
|
||||
"all"
|
||||
};
|
||||
}
|
||||
|
||||
bool IsSpaceSeparatedInts = Regex.IsMatch(input, @"^\d+(\s+\d+)*$");
|
||||
if (IsSpaceSeparatedInts)
|
||||
{
|
||||
|
|
@ -251,8 +260,13 @@ public static class MessageBuilder
|
|||
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)
|
||||
{
|
||||
return "Removed task(s)!";
|
||||
|
|
@ -261,8 +275,12 @@ public static class MessageBuilder
|
|||
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)
|
||||
{
|
||||
return "Task completed!";
|
||||
|
|
@ -270,7 +288,7 @@ public static class MessageBuilder
|
|||
|
||||
if (tasksFailedToComplete.Count == 0)
|
||||
{
|
||||
return "Completed all task(s) specified!";
|
||||
return "Completed the task(s)!";
|
||||
}
|
||||
|
||||
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");
|
||||
if (int.TryParse(taskName, out _))
|
||||
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();
|
||||
if (!taskData.ContainsKey(key))
|
||||
{
|
||||
|
|
@ -810,9 +830,12 @@ public class CPHInline
|
|||
return false;
|
||||
}
|
||||
|
||||
bool allTasks = tasksToBeRemoved.Count == 1 && tasksToBeRemoved[0] == "all";
|
||||
var tasksRemoved = new List<string>();
|
||||
var tasksFailedToRemove = new List<string>();
|
||||
var taskIndices = new List<int>();
|
||||
if (!allTasks)
|
||||
{
|
||||
foreach (string task in tasksToBeRemoved)
|
||||
{
|
||||
int index = TaskHelpers.GetTaskIndex(userTasks, task);
|
||||
|
|
@ -832,6 +855,11 @@ public class CPHInline
|
|||
Respond($"Failed to remove task(s): {String.Join(", ", tasksFailedToRemove)}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
taskIndices = Enumerable.Range(0, userTasks.Count).ToList();
|
||||
}
|
||||
|
||||
foreach (int i in taskIndices.OrderByDescending(n => n))
|
||||
{
|
||||
|
|
@ -842,7 +870,7 @@ public class CPHInline
|
|||
operations.SaveIntoTasks(userTasks);
|
||||
operations.Cleanup(true);
|
||||
SaveTasks();
|
||||
Respond(MessageBuilder.BuildRemoveMessage(tasksRemoved, tasksFailedToRemove));
|
||||
Respond(MessageBuilder.BuildRemoveMessage(tasksRemoved, tasksFailedToRemove, allTasks));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -885,9 +913,12 @@ public class CPHInline
|
|||
return false;
|
||||
}
|
||||
|
||||
bool allTasks = tasksToBeCompleted.Count == 1 && tasksToBeCompleted[0] == "all";
|
||||
var tasksCompleted = new List<string>();
|
||||
var tasksFailedToComplete = new List<string>();
|
||||
var taskIndices = new List<int>();
|
||||
if (!allTasks)
|
||||
{
|
||||
foreach (string task in tasksToBeCompleted)
|
||||
{
|
||||
int index = TaskHelpers.GetTaskIndex(userTasks, task);
|
||||
|
|
@ -901,6 +932,11 @@ public class CPHInline
|
|||
tasksFailedToComplete.Add(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
taskIndices = Enumerable.Range(0, userTasks.Count).ToList();
|
||||
}
|
||||
|
||||
if (taskIndices.Count == 0)
|
||||
{
|
||||
|
|
@ -912,15 +948,13 @@ public class CPHInline
|
|||
{
|
||||
userTasks[i].Completed = true;
|
||||
userTasks[i].Focused = false;
|
||||
|
||||
Broadcast(new { mode = "done", index = i }, null);
|
||||
}
|
||||
|
||||
IncrementDoneCount(taskIndices.Count);
|
||||
|
||||
operations.SaveIntoTasks(userTasks);
|
||||
SaveTasks();
|
||||
Respond(MessageBuilder.BuildCompletedMessage(tasksCompleted, tasksFailedToComplete));
|
||||
Respond(MessageBuilder.BuildCompletedMessage(tasksCompleted, tasksFailedToComplete, allTasks));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue