Skip to content

Commit 5599d9a

Browse files
Changed namespace to use PascalCase Prad.Buffer
Improved the security for accessing the variables. Improved documentations. Better indentations. Renamed file. etc. (version 3.0.0)
1 parent 028d919 commit 5599d9a

File tree

5 files changed

+274
-232
lines changed

5 files changed

+274
-232
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*.cs]
4+
# Use file-scoped namespaces
5+
dotnet_style_namespace_declarations = file_scoped:suggestion
6+
7+
8+
csharp_indent_case_contents = true
9+
csharp_indent_switch_labels = true
10+
csharp_new_line_before_open_brace = all

PradBuffer.cs

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
namespace Prad.Buffer;
2+
3+
/// <summary>
4+
/// <para> This is an custom made input buffer though which we can get the output even if the user have not completed typing. (Multi-threading) </para>
5+
/// <para> coded by <a href="https://github.com/pradosh-arduino">@pradosh-arduino (github)</a> </para>
6+
///
7+
/// <a href="https://github.com/pradosh-arduino/buffer">Github source</a> • <a href="https://www.nuget.org/packages/buffer">Nuget</a> • <a href="https://dev.to/pradcode/better-input-method-for-c-4hnb">Blog</a>
8+
/// </summary>
9+
public class PradBuffer
10+
{
11+
private char[] buffer;
12+
private int BufferSize;
13+
14+
/// <summary>
15+
/// <para> Stores the current index of the input buffer. </para>
16+
/// <para> It is the position of the cursor in the string (buffer array). </para>
17+
/// <para> It will only be less-than the 'Length' variable. </para>
18+
/// </summary>
19+
public int BufferIndex { get; private set; } = 0;
20+
21+
/// <summary>
22+
/// Stores the total length of the input buffer.
23+
/// </summary>
24+
public int Length { get; private set; } = 0;
25+
26+
/// <summary>
27+
/// Constructor to initialize the buffer with its size. Sets to default size of 1024 characters.
28+
/// </summary>
29+
public PradBuffer()
30+
{
31+
BufferSize = 1024;
32+
buffer = new char[BufferSize];
33+
}
34+
35+
/// <summary>
36+
/// Constructor to initialize the buffer with your own character limit.
37+
/// </summary>
38+
/// <param name="size">The size of the buffer. Or the character limit.</param>
39+
public PradBuffer(int size)
40+
{
41+
BufferSize = size;
42+
buffer = new char[BufferSize];
43+
}
44+
45+
/// <summary>
46+
/// Function to add a character to the buffer.
47+
/// </summary>
48+
/// <param name="c">The character to be added.</param>
49+
private void AddChar(char c)
50+
{
51+
if (BufferIndex >= buffer.Length)
52+
return;
53+
54+
buffer[BufferIndex] = c;
55+
BufferIndex++;
56+
Length++;
57+
}
58+
59+
/// <summary>
60+
/// Clears the buffer for next usage, This function will not be automatically called, you have to call by yourself.
61+
/// </summary>
62+
public void ClearBuffer()
63+
{
64+
Array.Clear(buffer, 0, buffer.Length);
65+
66+
BufferIndex = 0;
67+
Length = 0;
68+
}
69+
70+
/// <summary>
71+
/// Gets the input and stores it in the input buffer array cleanly. It does <b>NOT</b> return the buffer.
72+
/// </summary>
73+
public void GetInput()
74+
{
75+
ConsoleKeyInfo current;
76+
77+
while (true)
78+
{
79+
if (!Console.KeyAvailable) continue;
80+
81+
current = Console.ReadKey(true);
82+
83+
if (current.Key == ConsoleKey.Enter)
84+
{
85+
if (Length != 0)
86+
Console.WriteLine();
87+
88+
for (int i = 0; i < Length; i++)
89+
{
90+
if (buffer[i] == '\0')
91+
{
92+
Length = i;
93+
break;
94+
}
95+
}
96+
97+
break;
98+
}
99+
else if (current.Key == ConsoleKey.Backspace)
100+
{
101+
if (BufferIndex > 0)
102+
{
103+
BufferIndex--;
104+
Length--;
105+
Console.Write("\b \b");
106+
}
107+
}
108+
else if (current.Key == ConsoleKey.LeftArrow)
109+
{
110+
if (Console.CursorLeft > 0)
111+
{
112+
Console.CursorLeft--;
113+
BufferIndex--;
114+
}
115+
}
116+
else if (current.Key == ConsoleKey.RightArrow)
117+
{
118+
if (Console.CursorLeft < Length)
119+
{
120+
Console.CursorLeft++;
121+
BufferIndex++;
122+
}
123+
}
124+
else if (current.Key == ConsoleKey.Home)
125+
{
126+
Console.Write("\r");
127+
BufferIndex = 0;
128+
}
129+
else if (current.Key == ConsoleKey.End)
130+
{
131+
Console.CursorLeft = Length;
132+
BufferIndex = Length;
133+
}
134+
else
135+
{
136+
AddChar(current.KeyChar);
137+
Console.Write(current.KeyChar);
138+
}
139+
}
140+
}
141+
142+
/// <summary>
143+
/// Gets the input and stores it in the input buffer array cleanly. It does <b>NOT</b> return the buffer. We can add a prefix to the input.
144+
/// </summary>
145+
/// <param name="prefix">The actual prefix needed to be displayed</param>
146+
public void GetInput(string prefix)
147+
{
148+
ConsoleKeyInfo current;
149+
150+
Console.Write(prefix);
151+
152+
while (true)
153+
{
154+
if (!Console.KeyAvailable) continue;
155+
156+
current = Console.ReadKey(true);
157+
158+
if (current.Key == ConsoleKey.Enter)
159+
{
160+
if (Length != 0)
161+
Console.WriteLine();
162+
163+
for (int i = 0; i < Length; i++)
164+
{
165+
if (buffer[i] == '\0')
166+
{
167+
Length = i;
168+
break;
169+
}
170+
}
171+
172+
break;
173+
}
174+
else if (current.Key == ConsoleKey.Backspace)
175+
{
176+
if (BufferIndex > 0)
177+
{
178+
BufferIndex--;
179+
Length--;
180+
Console.Write("\b \b");
181+
}
182+
}
183+
else if (current.Key == ConsoleKey.LeftArrow)
184+
{
185+
if (Console.CursorLeft > prefix.Length)
186+
{
187+
Console.CursorLeft--;
188+
BufferIndex--;
189+
}
190+
}
191+
else if (current.Key == ConsoleKey.RightArrow)
192+
{
193+
if (Console.CursorLeft < Length + prefix.Length)
194+
{
195+
Console.CursorLeft++;
196+
BufferIndex++;
197+
}
198+
}
199+
else if (current.Key == ConsoleKey.Home)
200+
{
201+
Console.Write("\r");
202+
Console.CursorLeft += prefix.Length;
203+
BufferIndex = 0;
204+
}
205+
else if (current.Key == ConsoleKey.End)
206+
{
207+
Console.CursorLeft = Length + prefix.Length;
208+
BufferIndex = Length;
209+
}
210+
else
211+
{
212+
AddChar(current.KeyChar);
213+
Console.Write(current.KeyChar);
214+
}
215+
}
216+
}
217+
218+
/// <summary>
219+
/// Function to get the buffer values.
220+
/// </summary>
221+
/// <returns>It returns the buffer as an character array.</returns>
222+
public char[] GetBuffer()
223+
{
224+
return buffer;
225+
}
226+
227+
/// <summary>
228+
/// Function to get the buffer array as a string.
229+
/// </summary>
230+
/// <returns>A proper string with buffer values.</returns>
231+
public string GetBufferAsString()
232+
{
233+
return new string(buffer, 0, Length);
234+
}
235+
236+
/// <summary>
237+
/// Function to get the allocated buffer size.
238+
/// </summary>
239+
/// <returns>The buffer size in integer.</returns>
240+
public int GetBufferSize()
241+
{
242+
return BufferSize;
243+
}
244+
245+
/// <summary>
246+
/// Function to set the buffer size.
247+
/// This function will <b>CLEAR</b> the buffer and reallocate the buffer with the new size.
248+
/// </summary>
249+
/// <param name="size">The size of buffer (default is 1024)</param>
250+
public void SetBufferSize(int size)
251+
{
252+
BufferSize = size;
253+
buffer = new char[BufferSize];
254+
}
255+
}

0 commit comments

Comments
 (0)