Skip to content

SrijanSriv211/Creadf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

Creadf

Custom and better ReadLine methods for C#.

Note

This project uses Dotnet Core 8

How to Use?

  1. Setting up a prompt. (optional)
string prompt = "> ";
  1. Configure everything before reading the line

Important

It is a must to do the configuration before reading the line, otherwise it won't work.

  • Configure everything in the CreadfConfig
CreadfConfig config = new(
    LeftCursorStartPos: prompt.Length,
    TopCursorStartPos: Console.CursorTop,
    ToggleColorCoding: true,
    ToggleAutoComplete: true,
    Suggestions: ["test", "test2"],
    SyntaxHighlightCodes: SyntaxHighlightCodes,
    CreadfHistory: ["test1", "test21"]
);
CreadfConfig config = new(
    LeftCursorStartPos: Console.CursorLeft,
    TopCursorStartPos: Console.CursorTop,
    ToggleColorCoding: false,
    ToggleAutoComplete: false,
    Suggestions: null,
    SyntaxHighlightCodes: null,
    CreadfHistory: null
);

Structure of all config options that are available as of now.

struct CreadfConfig(int LeftCursorStartPos, int TopCursorStartPos, Dictionary<Creadf.Tokenizer.TokenType, ConsoleColor> SyntaxHighlightCodes, bool ToggleAutoComplete = true, bool ToggleColorCoding = true, List<string> Suggestions = null, List<string> CreadfHistory = null);
  • Configuring the Syntax color coding
Dictionary<Creadf.Tokenizer.TokenType, ConsoleColor> SyntaxHighlightCodes = [];
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.STRING, ConsoleColor.Yellow);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.EXPR, ConsoleColor.Cyan);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.BOOL, ConsoleColor.Magenta);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.SYMBOL, ConsoleColor.White);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.COMMENT, ConsoleColor.DarkGray);

All supported TokenTypes by the current Tokenizer of Creadf

public enum TokenType
{
    EOL = 0,
    COMMENT,
    WHITESPACE,
    IDENTIFIER,
    STRING,
    BOOL,
    EXPR,
    SYMBOL,
}
  1. Initialize the Creadf object
Creadf readline = new(Config);
  1. Initialize the default keybindings
Creadf.InitDefaultKeyBindings();

Where the default keybindings are the following:

Shortcut Comment
End Send end of line
Tab Change autocomplete suggestions
Home Send start of line
Escape Clear suggestions
Delete Delete succeeding character
Backspace Delete previous character
LeftArrow Backward one character
RightArrow Forward one character
UpArrow Forward in history
DownArrow Backward in history
Shift+Escape Clear input and suggestions
Ctrl+Enter Accept current suggestion
Ctrl+Spacebar Show current suggestions
Ctrl+Delete Delete succeeding token
Ctrl+Backspace Delete previous token
Ctrl+LeftArrow Backward one token
Ctrl+RightArrow Forward one token

However you can add more or other Keybindings by using the below method:

public void AddKeyBindings(ConsoleKey key, ConsoleModifiers modifier, Action action);

Replace InitDefaultKeyBindings by the below code

// Creadf.InitDefaultKeyBindings();
AddKeyBindings(ConsoleKey.Enter, ConsoleModifiers.Control, HandleCtrlEnter);
  1. Start taking input

Important

If prompt exists, then it is a must to print it before calling the Readf method.

Console.Write(prompt);
string output = readline.Readf();
Console.WriteLine(output);

or you can use the TakeInput method.

partial class Terminal
{
    public static string TakeInput(CreadfConfig Config, string Prompt="", ConsoleColor PromptColor=ConsoleColor.Gray, ConsoleColor InputColor=ConsoleColor.Gray);
}
string output = Terminal.TakeInput(Config: config, prompt, ConsoleColor.White);
Console.WriteLine(output);

so basically instead of creating a new Creadf object you can just simply do the following:

// Take input
Dictionary<Creadf.Tokenizer.TokenType, ConsoleColor> SyntaxHighlightCodes = [];
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.STRING, ConsoleColor.Yellow);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.EXPR, ConsoleColor.Cyan);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.BOOL, ConsoleColor.DarkCyan);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.COMMENT, ConsoleColor.DarkGray);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.SEMICOLON, ConsoleColor.DarkGray);
SyntaxHighlightCodes.Add(Creadf.Tokenizer.TokenType.HIDDEN, ConsoleColor.DarkGreen);

CreadfConfig config = new(
    LeftCursorStartPos: prompt.Length,
    TopCursorStartPos: Console.CursorTop,
    ToggleColorCoding: true,
    ToggleAutoComplete: true,
    Suggestions: ["India", "America", "Russia", "Elon Musk"],
    SyntaxHighlightCodes: SyntaxHighlightCodes,
    CreadfHistory: []
);

string output = Terminal.TakeInput(Config: config, prompt, ConsoleColor.White);
Console.WriteLine(output);

Releases

No releases published

Packages

No packages published

Languages