Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text;
- namespace bText;
- public class Program {
- static void Main(string[] args) {
- var program = new Program(args);
- program.Run();
- }
- private int _cursorX;
- private int _cursorY;
- private string _currentBuffer = "";
- private Dictionary<string, List<List<char>>> _namedBuffres = new Dictionary<string, List<List<char>>> {
- [""] = new List<List<char>>()
- };
- private bool _intedWhitespace = true;
- private int _intendCount = 4;
- private List<List<char>> CurrentBuffer => _namedBuffres[_currentBuffer];
- private List<char> CurrentLine => CurrentBuffer[_cursorY];
- Program(string[] args) {
- //todo parse args
- }
- public void Run() {
- while( RunApplicationMainLoop() ) {
- //todo place for other background stuff
- }
- }
- private Boolean RunApplicationMainLoop() {
- Console.SetCursorPosition(_cursorX, _cursorY);
- var key = Console.ReadKey(true);
- return ProcessUserInput(key);
- }
- private Boolean ProcessUserInput(ConsoleKeyInfo key) {
- FixBuffers();
- if( key.Key is ConsoleKey.Enter ) {
- if(_cursorX == CurrentLine.Count ) {
- _cursorY++;
- _cursorX = 0;
- }
- else if(_cursorX < CurrentLine.Count ) {
- var delta = CurrentLine.Count - _cursorX;
- var slice = CurrentLine.Slice(_cursorX, delta);
- CurrentLine.RemoveRange(_cursorX, delta);
- CurrentBuffer.Insert(_cursorY + 1, slice);
- _cursorY++;
- _cursorX = 0;
- RefreshScreen();
- }
- }
- else if( key.Key == ConsoleKey.F2 ) {
- StringBuilder sb = new();
- foreach( var line in CurrentBuffer ) {
- foreach( var c in line ) {
- sb.Append(c);
- }
- sb.AppendLine();
- }
- File.WriteAllText("./out.txt", sb.ToString());
- }
- else if( IsWhiteSpace(key) ) {
- HandleWhiteSpace(key);
- }
- else if( key.Key is ConsoleKey.Backspace ) {
- RemoveBeforeCursor();
- }
- else if( key.Key is ConsoleKey.Delete ) {
- RemoveAfterCursor();
- }
- else if( key.Key is ConsoleKey.LeftArrow ) {
- if( _cursorX > 0 ) {
- _cursorX--;
- } else if ( _cursorX == 0 ) {
- if(_cursorY > 0) {
- _cursorY--;
- _cursorX = CurrentLine.Count;
- }
- }
- }
- else if( key.Key is ConsoleKey.RightArrow ) {
- if( _cursorX < CurrentLine.Count ) {
- _cursorX++;
- } else if (_cursorX == CurrentLine.Count ) {
- _cursorX = 0;
- _cursorY++;
- }
- }
- else if( key.Key is ConsoleKey.UpArrow ) {
- if( _cursorY > 0 ) {
- _cursorY--;
- if( CurrentLine.Count < _cursorX ) {
- _cursorX = CurrentLine.Count;
- }
- }
- }
- else if( key.Key is ConsoleKey.DownArrow ) {
- if( _cursorY < CurrentBuffer.Count - 1 ) {
- _cursorY++;
- }
- }
- else if( IsPrintable(key) ) {
- AppendToBufferAtCurrentPositionAndPrintResult(key.KeyChar);
- }
- return true;
- }
- private void RemoveAfterCursor() {
- if( CurrentLine.Count > _cursorX ) {
- var delta = CurrentLine.Count - _cursorX;
- var slice = CurrentLine.Slice(_cursorX+1, delta-1);
- for( int i = 0; i < delta; i++ ) {
- Console.Write(' ');
- }
- Console.SetCursorPosition(_cursorX, _cursorY);
- foreach( var it in slice ) {
- Console.Write(it);
- }
- CurrentLine.RemoveRange(_cursorX, delta);
- CurrentLine.AddRange(slice);
- }
- else if( CurrentLine.Count == _cursorX ) {
- if( _cursorY < CurrentBuffer.Count - 1 ) {
- var temp = CurrentBuffer[_cursorY + 1];
- CurrentLine.AddRange(temp);
- CurrentBuffer.RemoveAt(_cursorY + 1);
- RefreshScreen();
- }
- }
- }
- private void RemoveBeforeCursor() {
- if( _cursorX > 0 ) {
- if( CurrentLine.Count - 1 > _cursorX - 1 ) {
- var delta = CurrentLine.Count - _cursorX + 1;
- var slice = CurrentLine.Slice(_cursorX, delta-1);
- for( int i = 0; i < delta; i++ ) {
- Console.Write(' ');
- }
- Console.SetCursorPosition(_cursorX - 1, _cursorY);
- foreach( var it in slice ) {
- Console.Write(it);
- }
- CurrentLine.RemoveRange(_cursorX - 1, delta);
- CurrentLine.AddRange(slice);
- }
- else if( CurrentLine.Count == _cursorX ) {
- CurrentLine.RemoveAt(_cursorX - 1);
- Console.SetCursorPosition(_cursorX - 1, _cursorY);
- Console.Write(' ');
- }
- _cursorX--;
- }
- else if (_cursorX == 0 ) {
- if( _cursorY > 0 ) {
- var temp = CurrentBuffer[_cursorY];
- CurrentBuffer.RemoveAt(_cursorY);
- _cursorY--;
- _cursorX = CurrentLine.Count;
- CurrentLine.AddRange(temp);
- RefreshScreen();
- }
- }
- }
- private void RefreshScreen() {
- Console.Clear();
- foreach( var line in CurrentBuffer ) {
- foreach( var it in line ) {
- Console.Write(it);
- }
- Console.WriteLine();
- }
- }
- private void HandleWhiteSpace(ConsoleKeyInfo key) {
- if( _intedWhitespace && key.Key == ConsoleKey.Tab ) {
- for( int i = 0; i < _intendCount; i++ ) {
- AppendToBufferAtCurrentPositionAndPrintResult(' ');
- }
- }
- else {
- AppendToBufferAtCurrentPositionAndPrintResult(key.KeyChar);
- }
- }
- private void AppendToBufferAtCurrentPositionAndPrintResult(Char keyChar) {
- if( CurrentLine.Count - 1 == _cursorX ) {
- CurrentLine.Add(keyChar);
- }
- else {
- var delta = CurrentLine.Count - _cursorX;
- var slice = CurrentLine.Slice(_cursorX, delta);
- for( int i = 0; i < delta; i++ ) {
- Console.Write(' ');
- }
- Console.SetCursorPosition(_cursorX, _cursorY);
- Console.Write(keyChar);
- foreach( var it in slice ) {
- Console.Write(it);
- }
- CurrentLine.RemoveRange(_cursorX, delta);
- CurrentLine.Add(keyChar);
- CurrentLine.AddRange(slice);
- }
- _cursorX++;
- }
- private void FixBuffers() {
- while( CurrentBuffer.Count - 1 < _cursorY ) {
- CurrentBuffer.Add(new List<char>());
- }
- while( CurrentLine.Count < _cursorX ) {
- CurrentLine.Add(' ');
- }
- }
- private Boolean IsWhiteSpace(ConsoleKeyInfo key) => char.IsWhiteSpace(key.KeyChar);
- private bool IsPrintable(ConsoleKeyInfo key) {
- return char.IsLetterOrDigit(key.KeyChar)
- || char.IsPunctuation(key.KeyChar)
- || key.KeyChar is '<' or '>' or '#' or '+' or '-' or '*' or '/' or '$' or '%' or '^' or '&' or '|' or '=';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement