forked from m4rreParre/Simple-TaskManagerCLITool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.cs
More file actions
129 lines (119 loc) · 4.21 KB
/
UserInterface.cs
File metadata and controls
129 lines (119 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace TaskManagerCLI;
class UserInterface
{
public static int MenuHandler()
{
int selectedIndex = 0;
ConsoleKey key;
Console.CursorVisible = false;
do
{
// Clears the console
Console.Write("\x1b[2J\x1b[H\x1b[2J\x1b[H");
Program.WriteTasks(Program.tasks, selectedIndex);
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
key = keyInfo.Key;
selectedIndex = KeyPressHandler(key, selectedIndex);
Program.SaveTasks();
} while (key != ConsoleKey.Enter);
Console.CursorVisible = true;
// TODO: Are you doing something with this return value?
return selectedIndex;
}
static int KeyPressHandler(ConsoleKey key, int selectedIndex)
{
switch (key)
{
case ConsoleKey.UpArrow:
selectedIndex--;
if (selectedIndex < 0)
{
selectedIndex = Program.tasks.Count - 1; // wrapping
return selectedIndex;
}
break;
case ConsoleKey.DownArrow:
selectedIndex++;
if (selectedIndex >= Program.tasks.Count)
{
selectedIndex = 0; // wrapping
return selectedIndex;
}
break;
case ConsoleKey.Spacebar:
Program.ToggleTaskState(Program.tasks[selectedIndex].TaskID);
break;
}
return selectedIndex;
}
static void Main(string[] args)
{
Program.LoadTasks();
if (args.Length == 0)
{
if (Program.tasks.Count <= 0)
{
Console.WriteLine("Ohh noo you have an empty task list:(");
Console.WriteLine("Try adding some tasks - use <tasks help> to get help");
}
else
{
MenuHandler();
}
}
else
{
string command = args[0].ToLower();
switch (command)
{
case "--help":
case "--h":
case "help":
Console.WriteLine(" tasks - Interactive menu");
Console.WriteLine(" tasks add <text> - Add a task");
Console.WriteLine(" tasks remove <id> - Remove a task");
Console.WriteLine(" tasks edit <id> <text> - Edit a task");
break;
case "add":
if (args.Length < 2)
{
Console.WriteLine("Usage: tasks add <task description>");
return;
}
string taskText = string.Join(" ", args.Skip(1));
Program.AddTask(taskText);
Program.SaveTasks();
break;
case "remove":
if (args.Length < 2 || !int.TryParse(args[1], out int removeId))
{
Console.WriteLine("Usage: tasks remove <id>");
return;
}
Program.RemoveTask(removeId);
Program.SaveTasks();
break;
case "edit":
if (args.Length < 3 || !int.TryParse(args[1], out int editId))
{
Console.WriteLine("Usage: tasks edit <id> <edited task>");
return;
}
string newText = string.Join(" ", args.Skip(2));
Program.EditTask(editId, newText);
Program.SaveTasks();
break;
default:
Console.WriteLine("Write tasks --help or tasks --h to show commands");
break;
}
}
}
}