-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cs
166 lines (141 loc) · 4.28 KB
/
Input.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//============================================================
// Student Number : S10203296, S10205301
// Student Name : Benedict Woo, Melvin Kee
// Module Group : T06
//============================================================
using System;
using System.Collections.Generic;
using COVIDMonitoringSystem.ConsoleApp.Utilities;
using COVIDMonitoringSystem.Core.Utilities;
namespace COVIDMonitoringSystem.ConsoleApp.Display.Elements
{
public class Input : SelectableElement
{
private string prompt;
public string Prompt
{
get => prompt;
set
{
prompt = value;
UpdateCursor();
QueueToRerender();
}
}
public override string Text
{
get => text;
set
{
text = value;
UpdateCursor();
QueueToRerender();
UpdateSuggestion();
}
}
public override bool Selected
{
get => selected;
set
{
selected = value;
if (value == false)
{
OnDeselect();
}
else
{
UpdateSuggestion();
}
QueueToRerender();
}
}
public override bool ClearOnExit { get; set; } = true;
public ActionMethod MethodRunner { get; set; }
public Action OnEnterRunner { get; set; }
public string SuggestionType { get; set; }
public List<string> CachedSuggestions { get; set; }
public int SuggestionIndex { get; set; }
public Input(string name = null) : base(name)
{
}
public Input(string name, string prompt) : base(name)
{
Prompt = prompt;
}
protected override int WriteToScreen()
{
if (!Selected || !HasSuggestions())
{
return CHelper.WriteLine($"{Prompt}: {Text}", Align);
}
var remainingSuggestionLetters = CachedSuggestions[SuggestionIndex].Substring(Text.Length);
var length = CHelper.WriteLine($"{Prompt}: {Text}", Align);
ColourSelector.Suggestion();
BoundingBox.SetCursorPosition();
CHelper.WriteLine($"{remainingSuggestionLetters}", TextAlign.None);
ColourSelector.Selected();
return length;
}
public void UpdateCursor()
{
BoundingBox.CursorLeft = CoreHelper.GetStringLength(Prompt) + CoreHelper.GetStringLength(Text) + 6;
}
public void Run()
{
if (MethodRunner != null)
{
MethodRunner.Run(TargetScreen);
return;
}
OnEnterRunner?.Invoke();
}
protected override void OnDeselect()
{
if (TargetScreen.Active)
{
Run();
}
}
public void ApplySuggestion()
{
if (!HasSuggestions())
{
return;
}
Text = CachedSuggestions[SuggestionIndex];
}
public bool NextSuggestion()
{
if (!HasSuggestions())
{
return true;
}
if (SuggestionIsConcrete())
{
ApplySuggestion();
return true;
}
SuggestionIndex = CoreHelper.Mod(++SuggestionIndex, CachedSuggestions.Count);
Render();
return false;
}
protected void UpdateSuggestion()
{
if (SuggestionType == null)
{
return;
}
CachedSuggestions = TargetScreen.DisplayManager.ValuesManager.GetSuggestion(SuggestionType, TargetScreen, Text);
SuggestionIndex = 0;
}
private bool HasSuggestions()
{
return CachedSuggestions != null && CachedSuggestions.Count > 0;
}
private bool SuggestionIsConcrete()
{
return CachedSuggestions != null && CachedSuggestions.Count == 1;
}
}
}