Skip to content

Commit

Permalink
Track ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Mar 11, 2023
1 parent 6e06346 commit 87bd19b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
51 changes: 50 additions & 1 deletion AskGPT/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ enum TokenState {
Trivia,
Word,
Number,
OneTick,
TwoTick,
ThreeTick,
Finished,
}

static readonly Dictionary<string, HashSet<string>> keywordsForProgrammingLanguage = new() {
{ "python", new() { "class", "def", "if", "import", "return", "while", "for", "break" } },
};

public void Append(string markdown)
{
Expand Down Expand Up @@ -48,6 +55,11 @@ void EndToken()
case TokenState.Number:
writer.Write(token.ToString(), TokenFormat.Number);
break;
case TokenState.OneTick:
case TokenState.TwoTick:
case TokenState.ThreeTick:
writer.Write(token.ToString(), TokenFormat.Markdown);
break;
default:
throw new NotImplementedException($"Cannot end state {state}");
}
Expand Down Expand Up @@ -94,6 +106,10 @@ bool Run(char ch)
writer.Write(ch.ToString(), TokenFormat.Bracket + bracketDepth);
bracketDepth--;
break;
case '`':
token.Append(ch);
state = TokenState.OneTick;
break;
default:
token.Append(ch);
state = TokenState.Word;
Expand Down Expand Up @@ -128,13 +144,43 @@ bool Run(char ch)
EndToken();
return false;
}
case TokenState.OneTick:
if (ch == '`') {
token.Append(ch);
state = TokenState.TwoTick;
return true;
}
else {
EndToken();
return false;
}
case TokenState.TwoTick:
if (ch == '`') {
token.Append(ch);
state = TokenState.ThreeTick;
return true;
}
else {
EndToken();
return false;
}
case TokenState.ThreeTick:
token.Append(ch);
if (ch == '\n') {
EndToken();
return true;
}
else {
return true;
}
default:
throw new NotImplementedException($"Unknown state {state}");
}
}
}

enum TokenFormat {
Markdown,
Body,
Number,
Punctuation,
Expand All @@ -151,13 +197,16 @@ class FormattedWriter
public void Write(string token, TokenFormat format)
{
switch (format) {
case TokenFormat.Markdown:
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
case TokenFormat.Body:
break;
case TokenFormat.Number:
Console.ForegroundColor = ConsoleColor.DarkYellow;
break;
case TokenFormat.Punctuation:
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.Gray;
break;
default:
if (format >= TokenFormat.Bracket) {
Expand Down
4 changes: 2 additions & 2 deletions AskGPT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
.ToList();
}

if (true && history.Count >= 3)
if (true && history.Count >= 9)
{
string lastText = history[^3].Message.Content;
string lastText = history[^9].Message.Content;
var lastFormatter = new Formatter();
lastFormatter.Append(lastText);
lastFormatter.Finish();
Expand Down

0 comments on commit 87bd19b

Please sign in to comment.