Skip to content

Commit

Permalink
Combine bold and italic
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Mar 23, 2023
1 parent 4b3c3f0 commit 5f571cf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 36 deletions.
118 changes: 83 additions & 35 deletions AskGPT/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void Finish()
{
if (state != TokenState.Finished) {
EndToken();
FlushWriteBuffer();
FlushWriteBuffer(GetStyle());
while (contextStack.Count > 1) {
EndContext();
}
Expand All @@ -117,9 +117,14 @@ void BeginContext(FormatContext contextType)
{
contextStack.Push(contextType);
bracketDepthStack.Push(bracketDepth);
bracketDepth = 0;
writer.BeginContext(contextType);
}
void EndContext()
{
contextStack.Pop();
bracketDepth = bracketDepthStack.Pop();
writer.EndContext();
}
void EndContext(FormatContext upToContextType)
{
while (contextStack.Count > 1) {
Expand All @@ -130,12 +135,6 @@ void EndContext(FormatContext upToContextType)
}
}
}
void EndContext()
{
contextStack.Pop();
bracketDepth = bracketDepthStack.Pop();
writer.EndContext();
}
FormatContext CurrentContextType => contextStack.Peek();

bool InContext(FormatContext c) => contextStack.Contains(c);
Expand All @@ -148,32 +147,52 @@ void EndContext()

void Write(string text, TokenFormat format)
{
if (format == TokenFormat.Identifier) {
var style = GetStyle();
if (format == TokenFormat.Identifier)
{
writeBuffer.Add((text, format));
}
else if (text == ".") {
else if (text == ".")
{
writeBuffer.Add((text, format));
}
else if (text == "(" && writeBuffer.Count > 0 && writeBuffer[^1].format == TokenFormat.Identifier) {
else if (text == "(" && writeBuffer.Count > 0 && writeBuffer[^1].format == TokenFormat.Identifier)
{
var (lastText, lastFormat) = writeBuffer[^1];
writeBuffer[^1] = (lastText, TokenFormat.Function);
FlushWriteBuffer();
writer.Write(text, format);
FlushWriteBuffer(style);
writer.Write(text, format, style);
}
else {
FlushWriteBuffer();
writer.Write(text, format);
else
{
FlushWriteBuffer(style);
writer.Write(text, format, style);
}
}

void FlushWriteBuffer()
void FlushWriteBuffer(TokenStyle style)
{
foreach (var (text, format) in writeBuffer) {
writer.Write(text, format);
writer.Write(text, format, style);
}
writeBuffer.Clear();
}

TokenStyle GetStyle()
{
var s = TokenStyle.None;
if (InContext(FormatContext.Bold)) {
s |= TokenStyle.Bold;
}
else if (InContext(FormatContext.Italic)) {
s |= TokenStyle.Italic;
}
else if (InContext(FormatContext.Underline)) {
s |= TokenStyle.Italic;
}
return s;
}

void EndToken()
{
var tokenText = token.ToString();
Expand Down Expand Up @@ -202,9 +221,6 @@ void EndToken()
Write(tokenText, TokenFormat.Identifier);
}
}
else if (InContext(FormatContext.Bold)) {
Write(tokenText, TokenFormat.Bold);
}
else {
Write(tokenText, TokenFormat.Body);
}
Expand Down Expand Up @@ -508,15 +524,23 @@ bool Run(char ch)
case TokenState.OneStar:
if (ch == '*') {
token.Append(ch);
EndToken();
if (InContext(FormatContext.Bold)) {
EndContext(FormatContext.Bold);
EndToken();
}
else {
EndToken();
BeginContext(FormatContext.Bold);
}
return true;
}
else if (char.IsWhiteSpace(ch)) {
Write("*", TokenFormat.Operator);
token.Clear();
token.Append(ch);
state = TokenState.WS;
return true;
}
else {
EndToken();
if (InContext(FormatContext.Italic)) {
Expand All @@ -536,8 +560,6 @@ bool Run(char ch)
enum TokenFormat {
Markdown,
Body,
Bold,
Italic,
Underline,
Number,
String,
Expand All @@ -551,10 +573,20 @@ enum TokenFormat {
Bracket = 1000,
}

[Flags]
enum TokenStyle {
None = 0,
Bold = 0x1,
Dimmed = 0x2,
Italic = 0x4,
Underline = 0x8,
}

enum FormatContext {
Text,
Italic,
Bold,
Italic,
Underline,
InlineCode,
Code = 1000,
CSharpCode,
Expand Down Expand Up @@ -589,35 +621,51 @@ public void EndContext()
contextStack.Pop();
}
public FormatContext CurrentContextType => contextStack.Peek();
public abstract void Write(string token, TokenFormat format);
public abstract void Write(string token, TokenFormat format, TokenStyle style);
public virtual void Finish() {}
}

class ConsoleWriter : FormattedWriter
{
readonly int width = Console.WindowWidth;
int column = 0;
const bool showMarkdown = true;
ConsoleColor[] bracketColors = new ConsoleColor[] {
ConsoleColor.DarkYellow,
ConsoleColor.DarkMagenta,
ConsoleColor.DarkCyan
};
public override void Write(string token, TokenFormat format)
public override void Write(string token, TokenFormat format, TokenStyle style)
{
var needsResetAfter = false;
if (style.HasFlag(TokenStyle.Bold)) {
Console.Write("\u001B[1m");
needsResetAfter = true;
}
if (style.HasFlag(TokenStyle.Dimmed)) {
Console.Write("\u001B[2m");
needsResetAfter = true;
}
if (style.HasFlag(TokenStyle.Italic)) {
Console.Write("\u001B[3m");
needsResetAfter = true;
}
if (style.HasFlag(TokenStyle.Underline)) {
Console.Write("\u001B[4m");
needsResetAfter = true;
}
switch (format) {
case TokenFormat.Markdown:
// Console.ForegroundColor = ConsoleColor.DarkGray;
// break;
return;
if (showMarkdown) {
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
}
else {
return;
}
case TokenFormat.Body:
Console.ResetColor();
break;
case TokenFormat.Bold:
Console.ResetColor();
Console.Write("\u001B[1m");
needsResetAfter = true;
break;
case TokenFormat.Comment:
Console.ForegroundColor = ConsoleColor.Gray;
break;
Expand Down
2 changes: 1 addition & 1 deletion AskGPT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
string configFilePath = Path.Combine(configDir, "config.json");

const int maxHistory = 1000;
const bool testFormatter = false;
const bool testFormatter = true;

//
// Load the API key
Expand Down

0 comments on commit 5f571cf

Please sign in to comment.