-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtensionMethods.cs
172 lines (160 loc) · 4.33 KB
/
ExtensionMethods.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
167
168
169
170
171
172
using System;
using System.Collections;
using System.Collections.Generic;
namespace ExtensionMethods
{
public static class MyExtensions
{
public static string ToUpperFirstLetter(this string source)
{
if (string.IsNullOrEmpty(source))
return string.Empty;
// convert to char array of the string
char[] letters = source.ToCharArray();
// upper case the first char
letters[0] = char.ToUpper(letters[0]);
// return the array made of the new char array
return new string(letters);
}
public static string ListThroughCommands(this List<string> cmds)
{
// define variables to help
string s = "";
int whichFromEnd = 0;
int whichToDisplay;
bool run = true;
int stringPosition = 0;
while (run)
{
// get user input
ConsoleKeyInfo keyhit = Console.ReadKey();
switch (keyhit.Key)
{
case ConsoleKey.Escape: // return "close", which will end program
s = "close";
run = false;
break;
case ConsoleKey.Backspace: // backspace character
if (s.Length > 0 & stringPosition>0)
{
s = s.Remove(stringPosition-1,1);
stringPosition--;
char[] charlist = s.ToCharArray();
for (int i=stringPosition; i<charlist.Length; i++)
Console.Write(charlist[i]);
Console.Write(" ");
Console.CursorLeft = stringPosition;
}
break;
case ConsoleKey.Delete: // delete character
if (s.Length>stringPosition)
{
s = s.Remove(stringPosition,1);
char[] charlist = s.ToCharArray();
for (int i=stringPosition; i<charlist.Length; i++)
Console.Write(charlist[i]);
Console.Write(" ");
Console.CursorLeft = stringPosition;
}
break;
case ConsoleKey.Enter: // return s
if (s.Trim().Length>0)
run = false;
stringPosition = 0;
break;
case ConsoleKey.End: // go to end
stringPosition = s.Length;
Console.CursorLeft = stringPosition;
break;
case ConsoleKey.Home: // go to beginning
stringPosition = 0;
Console.CursorLeft = stringPosition;
break;
case ConsoleKey.RightArrow: // go one character right
if (s.Length>(stringPosition))
{
stringPosition++;
Console.CursorLeft = stringPosition;
}
break;
case ConsoleKey.LeftArrow: // go one character left
if (stringPosition>0)
{
stringPosition--;
Console.CursorLeft = stringPosition;
}
break;
case ConsoleKey.UpArrow: // List through commands
whichFromEnd += 1;
if (whichFromEnd > cmds.Count)
whichFromEnd = cmds.Count;
whichToDisplay = cmds.Count-whichFromEnd;
if (whichToDisplay >= 0 & cmds.Count > 0)
{
Console.CursorLeft = 0;
int w = Console.WindowWidth;
for (int i=1; i<w; i++)
Console.Write(" ");
Console.CursorLeft = 0;
Console.Write(cmds[whichToDisplay]);
s = cmds[whichToDisplay];
}
stringPosition = s.Length;
break;
case ConsoleKey.DownArrow: // List through commands
whichFromEnd += -1;
if (whichFromEnd <=0)
whichFromEnd = 1;
whichToDisplay = cmds.Count-whichFromEnd;
if (whichToDisplay < cmds.Count & cmds.Count > 0)
{
Console.CursorLeft = 0;
int w = Console.WindowWidth;
for (int i=1; i<w; i++)
Console.Write(" ");
Console.CursorLeft = 0;
Console.Write(cmds[whichToDisplay]);
s = cmds[whichToDisplay];
}
stringPosition = s.Length;
break;
default:
int asciicode = (int)keyhit.KeyChar;
if (asciicode<255) //better condition
{
s = s.Insert(stringPosition, keyhit.KeyChar.ToString());
stringPosition++;
char[] charlist = s.ToCharArray();
for (int i=stringPosition; i<charlist.Length; i++)
Console.Write(charlist[i]);
Console.CursorLeft = stringPosition;
}
break;
}
}
return s;
}
}
public class LimitedQueue<T> : Queue
{
private int limit = -1;
public int Limit
{
get { return limit; }
set { limit = value; }
}
public LimitedQueue(int limit)
: base(limit)
{
this.Limit = limit;
}
public void Enqueue(T item)
{
if (this.Count >= this.Limit)
{
this.Dequeue();
}
base.Enqueue(item);
}
}
}