Example #1
0
        private Cursor Clamp(EditorBuffer buffer)
        {
            var row = this.row.Clamp(0, buffer.LinesCount - 1);
            var col = this.col.Clamp(0, buffer.Lines[row].Length);

            return(new Cursor(row, col));
        }
Example #2
0
        public Editor()
        {
            this.buffer = new EditorBuffer(new List <string>
            {
                "One line",
                "Two lines",
                "",
                "Last line, maybe"
            }.ToImmutableList());

            this.cursor = new Cursor(0, 0);
        }
Example #3
0
        private void HandleInput()
        {
            var c = Console.ReadKey(true);

            // ctrl-q exits
            if (c.Key == ConsoleKey.Q && c.Modifiers.HasFlag(ConsoleModifiers.Control))
            {
                Environment.Exit(0);
            }

            if (c.Key == ConsoleKey.RightArrow)
            {
                cursor = cursor.MoveRight(buffer);
                return;
            }

            if (c.Key == ConsoleKey.LeftArrow)
            {
                cursor = cursor.MoveLeft(buffer);
                return;
            }

            if (c.Key == ConsoleKey.UpArrow)
            {
                cursor = cursor.MoveUp(buffer);
                return;
            }

            if (c.Key == ConsoleKey.DownArrow)
            {
                cursor = cursor.MoveDown(buffer);
                return;
            }

            if (c.Key == ConsoleKey.Enter)
            {
                buffer = buffer.InsertNewLine(this.cursor.Row, this.cursor.Col);
                cursor = cursor.MoveDown(buffer).MoveToBeginningOfLine(buffer);
                return;
            }

            if (c.Key == ConsoleKey.Backspace)
            {
                buffer = buffer.DeleteBack(this.cursor.Row, this.cursor.Col);
                cursor = cursor.MoveLeft(buffer);
                return;
            }

            // in all other cases, insert into the current position
            buffer = buffer.Insert(c.KeyChar, this.cursor.Row, this.cursor.Col);
            cursor = cursor.MoveRight(buffer);
        }
Example #4
0
 internal Cursor MoveToBeginningOfLine(EditorBuffer buffer)
 {
     return(new Cursor(row, 0));
 }
Example #5
0
 public Cursor MoveDown(EditorBuffer buffer)
 {
     return(new Cursor(this.Row + 1, this.Col).Clamp(buffer));
 }
Example #6
0
 public Cursor MoveLeft(EditorBuffer buffer)
 {
     return(new Cursor(this.Row, this.Col - 1).Clamp(buffer));
 }