Skip to content

Commit 2c48036

Browse files
The syntax highlight dictionary now not only supports ConsoleColor but also integers and strings. version 3.3.0
1 parent e224367 commit 2c48036

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

PradBuffer.cs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PradBuffer
1414
/// <summary>
1515
/// Stores the syntax highlighting data for the input buffer
1616
/// </summary>
17-
public Dictionary<string, ConsoleColor> SyntaxHighlights { get; set; } = new Dictionary<string, ConsoleColor>();
17+
public Dictionary<string, object> SyntaxHighlights { get; set; } = new Dictionary<string, object>();
1818

1919
/// <summary>
2020
/// Variable to enable syntax highlights during input.
@@ -259,16 +259,48 @@ private void WriteHighlight(string str)
259259
foreach (var highlight in SyntaxHighlights)
260260
{
261261
string keyword = highlight.Key;
262-
ConsoleColor color = highlight.Value;
263262

264-
if (str.Substring(currentIndex).StartsWith(keyword))
263+
switch (highlight.Value)
265264
{
266-
Console.ForegroundColor = color;
267-
Console.Write(keyword);
268-
Console.ResetColor();
265+
case ConsoleColor color:
266+
if (str.Substring(currentIndex).StartsWith(keyword))
267+
{
268+
Console.ForegroundColor = color;
269+
Console.Write(keyword);
270+
Console.ResetColor();
271+
272+
currentIndex += keyword.Length;
273+
matched = true;
274+
}
275+
break;
276+
277+
case int colorInt:
278+
if (str.Substring(currentIndex).StartsWith(keyword))
279+
{
280+
Console.ForegroundColor = (ConsoleColor)colorInt;
281+
Console.Write(keyword);
282+
Console.ResetColor();
283+
284+
currentIndex += keyword.Length;
285+
matched = true;
286+
}
287+
break;
288+
289+
case string customColor:
290+
if (str.Substring(currentIndex).StartsWith(keyword))
291+
{
292+
Console.Write(customColor + keyword + "\x1b[0m");
293+
currentIndex += keyword.Length;
294+
matched = true;
295+
}
296+
break;
297+
298+
default:
299+
throw new InvalidOperationException($"Unsupported highlight value type: {highlight.Value.GetType()}");
300+
}
269301

270-
currentIndex += keyword.Length;
271-
matched = true;
302+
if (matched)
303+
{
272304
break;
273305
}
274306
}

buffer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Nullable>enable</Nullable>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

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

readme.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ⚡️ Better Input for C# with `buffer`
22

3-
![Buffer package in action](https://raw.githubusercontent.com/pradosh-arduino/buffer/ac911024b63e8a7f5b245be591859991fe23aec4/buffer.gif)
3+
<div align="center">
4+
<img src="https://raw.githubusercontent.com/pradosh-arduino/buffer/ac911024b63e8a7f5b245be591859991fe23aec4/buffer.gif" alt="Buffer package in action" style="width: 80%; height: auto;" />
5+
</div>
46

57
[![NuGet Downloads](https://img.shields.io/nuget/dt/buffer?style=flat-square&logo=nuget&logoColor=ffffff&logoSize=auto&label=Downloads&labelColor=323ca8&color=545454)](https://www.nuget.org/stats/packages/buffer?groupby=Version)
68
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/pradosh-arduino/buffer/dotnet.yml?style=flat-square&logo=github&logoColor=ffffff&logoSize=auto&label=Build)](https://github.com/pradosh-arduino/buffer/actions/workflows/dotnet.yml)
@@ -90,18 +92,29 @@ InputBuffer.ClearBuffer();
9092
```
9193

9294
### Here is a demo of Limited Buffer Size of 10 characters
93-
![buffer-limit](https://raw.githubusercontent.com/pradosh-arduino/buffer/main/buffer-limit.gif)
95+
<div align="center">
96+
<img src="https://raw.githubusercontent.com/pradosh-arduino/buffer/main/buffer-limit.gif" alt="buffer-limit" style="width: 70%; height: auto;" />
97+
</div>
9498

9599
### 🧩 Syntax Highlighting in Real-time
96100
```cpp
97101
PradBuffer Buffer = new PradBuffer();
98102

103+
Console.WriteLine("Enter something to get started, ");
104+
99105
string s = "";
100106

107+
// Supports Console Colors
101108
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);
109+
110+
// Supports Integers, color will be selected with respective to ConsoleColor enum.
111+
Buffer.SyntaxHighlights.Add("static", 9);
112+
113+
// Directly supports ANSI Escape codes. **Beware any mistakes CAN and WILL break the input.**
114+
Buffer.SyntaxHighlights.Add("public", "\x1b[32m"); // Green ANSI Escape code.
115+
116+
// ! Throws exception if any other data type is being used.
117+
// Buffer.SyntaxHighlights.Add("=", 56.3d);
105118

106119
Buffer.EnableSyntaxHighlighting = true;
107120

@@ -119,7 +132,9 @@ while(true){
119132
```
120133

121134
#### Demo of Syntax Highlighting
122-
![Syntax Highlighting Demo](https://raw.githubusercontent.com/pradosh-arduino/buffer/main/syntax-highlights.gif)
135+
<div align="center">
136+
<img src="https://raw.githubusercontent.com/pradosh-arduino/buffer/main/syntax-highlights.gif" alt="Syntax Highlighting Demo" style="width: 70%; height: auto;" />
137+
</div>
123138

124139
### 🚀 Using its maximum potential
125140

0 commit comments

Comments
 (0)