Skip to content

Commit 6baf9b2

Browse files
Added syntax highlightings! Wooo!
1 parent 33d3e2e commit 6baf9b2

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

PradBuffer.cs

+53-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ public class PradBuffer
1111
private char[] buffer;
1212
private int BufferSize;
1313

14+
/// <summary>
15+
/// Stores the syntax highlighting data for the input buffer
16+
/// </summary>
17+
public Dictionary<string, ConsoleColor> SyntaxHighlights { get; set; } = new Dictionary<string, ConsoleColor>();
18+
19+
/// <summary>
20+
/// Variable to enable syntax highlights during input.
21+
/// </summary>
22+
public bool EnableSyntaxHighlighting { get; set; } = false;
23+
1424
/// <summary>
1525
/// <para> Stores the current index of the input buffer. </para>
1626
/// <para> It is the position of the cursor in the string (buffer array). </para>
@@ -181,7 +191,7 @@ public void GetInput()
181191
Console.Write(" ");
182192
}
183193
Console.Write("\r");
184-
Console.Write(GetBufferAsString() + " ");
194+
WriteHighlight(GetBufferAsString() + " ");
185195

186196
Console.CursorLeft = BufferIndex;
187197
}
@@ -223,7 +233,7 @@ public void GetInput()
223233
Console.Write(" ");
224234
}
225235
Console.Write("\r");
226-
Console.Write(GetBufferAsString());
236+
WriteHighlight(GetBufferAsString());
227237

228238
BufferIndex = Console.CursorLeft;
229239
break;
@@ -232,6 +242,45 @@ public void GetInput()
232242
}
233243
}
234244

245+
private void WriteHighlight(string str)
246+
{
247+
if (!EnableSyntaxHighlighting)
248+
{
249+
Console.Write(str);
250+
return;
251+
}
252+
253+
int currentIndex = 0;
254+
255+
while (currentIndex < str.Length)
256+
{
257+
bool matched = false;
258+
259+
foreach (var highlight in SyntaxHighlights)
260+
{
261+
string keyword = highlight.Key;
262+
ConsoleColor color = highlight.Value;
263+
264+
if (str.Substring(currentIndex).StartsWith(keyword))
265+
{
266+
Console.ForegroundColor = color;
267+
Console.Write(keyword);
268+
Console.ResetColor();
269+
270+
currentIndex += keyword.Length;
271+
matched = true;
272+
break;
273+
}
274+
}
275+
276+
if (!matched)
277+
{
278+
Console.Write(str[currentIndex]);
279+
currentIndex++;
280+
}
281+
}
282+
}
283+
235284
/// <summary>
236285
/// 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.
237286
/// </summary>
@@ -346,7 +395,7 @@ public void GetInput(string prefix)
346395
Console.Write(" ");
347396
}
348397
Console.Write("\r");
349-
Console.Write(prefix + GetBufferAsString() + " ");
398+
WriteHighlight(prefix + GetBufferAsString() + " ");
350399

351400
Console.CursorLeft = BufferIndex + prefix.Length;
352401
}
@@ -389,7 +438,7 @@ public void GetInput(string prefix)
389438
Console.Write(" ");
390439
}
391440
Console.Write("\r");
392-
Console.Write(prefix + GetBufferAsString());
441+
WriteHighlight(prefix + GetBufferAsString());
393442

394443
BufferIndex = Console.CursorLeft - prefix.Length;
395444
break;

buffer-limit.gif

-2.38 KB
Loading

buffer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Nullable>enable</Nullable>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

10-
<Version>3.1.0</Version>
10+
<Version>3.1.1</Version>
1111
<Authors>Pradosh (helloImPR)</Authors>
1212
<Description>A fast, completely controllable input method solution for C#.</Description>
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>

readme.md

+33-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Introducing **`buffer`** — a lightweight NuGet package that gives you real-tim
1616

1717
## ✨ Features
1818

19-
- `` Real-time updates while users type
20-
- `🧠` Manual control over the input buffer
19+
- `` **Real-time updates while users type**
20+
- `📜` **Real-time Syntax highlighting.**
21+
- `🧠` **Manual control over the input buffer**
2122
- `🧵` Thread-safe & supports multithreading
2223
- `🔠` Access buffer as `char[]` or `string`
2324
- `💻` Compatible with a wide range of .NET Frameworks
@@ -27,10 +28,6 @@ Introducing **`buffer`** — a lightweight NuGet package that gives you real-tim
2728
- `📦` Lightweight and fast
2829
- `🔧` Customizable buffer size
2930

30-
## 🌟 Upcoming Features
31-
32-
- `📜` Real-time syntax highlighting.
33-
3431
---
3532

3633
## 🚀 Quick Start
@@ -95,6 +92,36 @@ InputBuffer.ClearBuffer();
9592
### Here is a demo of Limited Buffer Size of 10 characters
9693
![buffer-limit](https://github.com/user-attachments/assets/3f459e05-65dc-4ce7-8f8a-20c09f660702)
9794

95+
### 🧩 Syntax Highlighting in Real-time
96+
```cpp
97+
PradBuffer Buffer = new PradBuffer();
98+
99+
string s = "";
100+
101+
Buffer.SyntaxHighlights.Add("prad", ConsoleColor.Red);
102+
Buffer.SyntaxHighlights.Add("static", ConsoleColor.Blue);
103+
Buffer.SyntaxHighlights.Add("public", ConsoleColor.Green);
104+
Buffer.SyntaxHighlights.Add("=", ConsoleColor.Yellow);
105+
106+
Buffer.EnableSyntaxHighlighting = true;
107+
108+
while(true){
109+
Buffer.GetInput("> ");
110+
111+
s = Buffer.GetBufferAsString();
112+
113+
Buffer.ClearBuffer();
114+
115+
if(s == "exit") break;
116+
117+
Console.WriteLine($"Value Entered : \"{s}\"");
118+
}
119+
```
120+
121+
#### Demo of Syntax Highlighting
122+
123+
124+
98125
### 🚀 Using its maximum potential
99126

100127
Here is a code example which uses Multithreading to get the input buffer in real-time.

syntax-highlights.gif

252 KB
Loading

0 commit comments

Comments
 (0)