Advertisement
Guest User

Untitled

a guest
Apr 10th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.63 KB | None | 0 0
  1.  
  2. using System.Text;
  3.  
  4. namespace bText;
  5.  
  6. public class Program {
  7.  
  8.     static void Main(string[] args) {
  9.         var program = new Program(args);
  10.         program.Run();
  11.     }
  12.  
  13.     private int _cursorX;
  14.     private int _cursorY;
  15.  
  16.     private string _currentBuffer = "";
  17.     private Dictionary<string, List<List<char>>> _namedBuffres = new Dictionary<string, List<List<char>>> {
  18.         [""] = new List<List<char>>()
  19.     };
  20.  
  21.     private bool _intedWhitespace = true;
  22.     private int _intendCount = 4;
  23.  
  24.     private List<List<char>> CurrentBuffer => _namedBuffres[_currentBuffer];
  25.     private List<char> CurrentLine => CurrentBuffer[_cursorY];
  26.  
  27.  
  28.     Program(string[] args) {
  29.         //todo parse args
  30.     }
  31.  
  32.     public void Run() {
  33.         while( RunApplicationMainLoop() ) {
  34.             //todo place for other background stuff
  35.         }
  36.     }
  37.  
  38.     private Boolean RunApplicationMainLoop() {
  39.        
  40.         Console.SetCursorPosition(_cursorX, _cursorY);
  41.         var key = Console.ReadKey(true);
  42.         return ProcessUserInput(key);
  43.     }
  44.  
  45.     private Boolean ProcessUserInput(ConsoleKeyInfo key) {
  46.         FixBuffers();
  47.         if( key.Key is ConsoleKey.Enter ) {
  48.             if(_cursorX == CurrentLine.Count ) {
  49.                 _cursorY++;
  50.                 _cursorX = 0;
  51.             }
  52.             else if(_cursorX < CurrentLine.Count ) {
  53.                 var delta = CurrentLine.Count - _cursorX;
  54.                 var slice = CurrentLine.Slice(_cursorX, delta);
  55.                 CurrentLine.RemoveRange(_cursorX, delta);
  56.                 CurrentBuffer.Insert(_cursorY + 1, slice);
  57.                 _cursorY++;
  58.                 _cursorX = 0;
  59.                 RefreshScreen();
  60.             }
  61.         }
  62.         else if( key.Key == ConsoleKey.F2 ) {
  63.             StringBuilder sb = new();
  64.             foreach( var line in CurrentBuffer ) {
  65.                 foreach( var c in line ) {
  66.                     sb.Append(c);
  67.                 }
  68.                 sb.AppendLine();
  69.             }
  70.             File.WriteAllText("./out.txt", sb.ToString());
  71.         }
  72.         else if( IsWhiteSpace(key) ) {
  73.             HandleWhiteSpace(key);
  74.         }
  75.         else if( key.Key is ConsoleKey.Backspace ) {
  76.             RemoveBeforeCursor();
  77.         }
  78.         else if( key.Key is ConsoleKey.Delete ) {
  79.             RemoveAfterCursor();
  80.         }
  81.         else if( key.Key is ConsoleKey.LeftArrow ) {
  82.             if( _cursorX > 0 ) {
  83.                 _cursorX--;
  84.             } else if ( _cursorX == 0 ) {
  85.                 if(_cursorY > 0) {
  86.                     _cursorY--;
  87.                     _cursorX = CurrentLine.Count;
  88.                 }
  89.             }
  90.         }
  91.         else if( key.Key is ConsoleKey.RightArrow ) {
  92.             if( _cursorX < CurrentLine.Count ) {
  93.                 _cursorX++;
  94.             } else if (_cursorX == CurrentLine.Count ) {
  95.                 _cursorX = 0;
  96.                 _cursorY++;
  97.             }
  98.         }
  99.         else if( key.Key is ConsoleKey.UpArrow ) {
  100.             if( _cursorY > 0 ) {
  101.                 _cursorY--;
  102.                 if( CurrentLine.Count < _cursorX ) {
  103.                     _cursorX = CurrentLine.Count;
  104.                 }
  105.             }
  106.         }
  107.         else if( key.Key is ConsoleKey.DownArrow ) {
  108.             if( _cursorY < CurrentBuffer.Count - 1 ) {
  109.                 _cursorY++;
  110.             }
  111.         }
  112.         else if( IsPrintable(key) ) {
  113.             AppendToBufferAtCurrentPositionAndPrintResult(key.KeyChar);
  114.         }
  115.         return true;
  116.     }
  117.  
  118.     private void RemoveAfterCursor() {
  119.         if( CurrentLine.Count > _cursorX ) {
  120.             var delta = CurrentLine.Count - _cursorX;
  121.             var slice = CurrentLine.Slice(_cursorX+1, delta-1);
  122.             for( int i = 0; i < delta; i++ ) {
  123.                 Console.Write(' ');
  124.             }
  125.             Console.SetCursorPosition(_cursorX, _cursorY);
  126.             foreach( var it in slice ) {
  127.                 Console.Write(it);
  128.             }
  129.             CurrentLine.RemoveRange(_cursorX, delta);
  130.             CurrentLine.AddRange(slice);
  131.         }
  132.         else if( CurrentLine.Count == _cursorX ) {
  133.             if( _cursorY < CurrentBuffer.Count - 1 ) {
  134.                 var temp = CurrentBuffer[_cursorY + 1];
  135.  
  136.                 CurrentLine.AddRange(temp);
  137.                 CurrentBuffer.RemoveAt(_cursorY + 1);
  138.                 RefreshScreen();
  139.             }
  140.         }
  141.     }
  142.     private void RemoveBeforeCursor() {
  143.         if( _cursorX > 0 ) {
  144.             if( CurrentLine.Count - 1 > _cursorX - 1 ) {
  145.                 var delta = CurrentLine.Count - _cursorX + 1;
  146.                 var slice = CurrentLine.Slice(_cursorX, delta-1);
  147.                 for( int i = 0; i < delta; i++ ) {
  148.                     Console.Write(' ');
  149.                 }
  150.                 Console.SetCursorPosition(_cursorX - 1, _cursorY);
  151.                 foreach( var it in slice ) {
  152.                     Console.Write(it);
  153.                 }
  154.                 CurrentLine.RemoveRange(_cursorX - 1, delta);
  155.                 CurrentLine.AddRange(slice);
  156.             }
  157.             else if( CurrentLine.Count == _cursorX ) {
  158.                 CurrentLine.RemoveAt(_cursorX - 1);
  159.                 Console.SetCursorPosition(_cursorX - 1, _cursorY);
  160.                 Console.Write(' ');
  161.             }
  162.             _cursorX--;
  163.         }
  164.         else if (_cursorX == 0 ) {
  165.             if( _cursorY > 0 ) {
  166.                 var temp = CurrentBuffer[_cursorY];
  167.                 CurrentBuffer.RemoveAt(_cursorY);
  168.                 _cursorY--;
  169.                 _cursorX = CurrentLine.Count;
  170.                 CurrentLine.AddRange(temp);
  171.                 RefreshScreen();
  172.             }
  173.         }
  174.     }
  175.  
  176.     private void RefreshScreen() {
  177.         Console.Clear();
  178.         foreach( var line in CurrentBuffer ) {
  179.             foreach( var it in line ) {
  180.                 Console.Write(it);
  181.             }
  182.             Console.WriteLine();
  183.         }
  184.     }
  185.  
  186.     private void HandleWhiteSpace(ConsoleKeyInfo key) {
  187.         if( _intedWhitespace && key.Key == ConsoleKey.Tab ) {
  188.             for( int i = 0; i < _intendCount; i++ ) {
  189.                 AppendToBufferAtCurrentPositionAndPrintResult(' ');
  190.             }
  191.         }
  192.         else {
  193.             AppendToBufferAtCurrentPositionAndPrintResult(key.KeyChar);
  194.         }
  195.     }
  196.  
  197.     private void AppendToBufferAtCurrentPositionAndPrintResult(Char keyChar) {
  198.         if( CurrentLine.Count - 1 == _cursorX ) {
  199.             CurrentLine.Add(keyChar);
  200.         }
  201.         else {
  202.             var delta = CurrentLine.Count - _cursorX;
  203.             var slice = CurrentLine.Slice(_cursorX, delta);
  204.             for( int i = 0; i < delta; i++ ) {
  205.                 Console.Write(' ');
  206.             }
  207.             Console.SetCursorPosition(_cursorX, _cursorY);
  208.             Console.Write(keyChar);
  209.             foreach( var it in slice ) {
  210.                 Console.Write(it);
  211.             }
  212.             CurrentLine.RemoveRange(_cursorX, delta);
  213.             CurrentLine.Add(keyChar);
  214.             CurrentLine.AddRange(slice);
  215.         }
  216.         _cursorX++;
  217.     }
  218.  
  219.     private void FixBuffers() {
  220.         while( CurrentBuffer.Count - 1 < _cursorY ) {
  221.             CurrentBuffer.Add(new List<char>());
  222.         }
  223.         while( CurrentLine.Count < _cursorX ) {
  224.             CurrentLine.Add(' ');
  225.         }
  226.     }
  227.  
  228.     private Boolean IsWhiteSpace(ConsoleKeyInfo key) => char.IsWhiteSpace(key.KeyChar);
  229.  
  230.     private bool IsPrintable(ConsoleKeyInfo key) {
  231.         return char.IsLetterOrDigit(key.KeyChar)
  232.             || char.IsPunctuation(key.KeyChar)
  233.             || key.KeyChar is '<' or '>' or '#' or '+' or '-' or '*' or '/' or '$' or '%' or '^' or '&' or '|' or '=';
  234.     }
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement