Ejemplo n.º 1
0
 private void HandleUpArrow(ObservableCollection <string> document, SubmissionView view)
 {
     if (view.CurrentLine > 0)
     {
         view.CurrentLine--;
     }
 }
Ejemplo n.º 2
0
        private void HandleBackspace(ObservableCollection <string> document, SubmissionView view)
        {
            var start = view.CurrentCharacter;

            if (start == 0)
            {
                if (view.CurrentLine == 0)
                {
                    return;
                }

                var currentLine  = document[view.CurrentLine];
                var previousLine = document[view.CurrentLine - 1];
                document.RemoveAt(view.CurrentLine);
                view.CurrentLine--;
                document[view.CurrentLine] = previousLine + currentLine;
                view.CurrentCharacter      = previousLine.Length;
            }
            else
            {
                var line   = document[view.CurrentLine];
                var before = line.Substring(0, start - 1);
                var after  = line.Substring(start);
                document[view.CurrentLine] = before + after;
                view.CurrentCharacter--;
            }
        }
Ejemplo n.º 3
0
 private void HandleLeftArrow(ObservableCollection <string> document, SubmissionView view)
 {
     if (view.CurrentCharacter > 0)
     {
         view.CurrentCharacter--;
     }
 }
Ejemplo n.º 4
0
 private void HandleEscape(ObservableCollection <string> document, SubmissionView view)
 {
     document.Clear();
     document.Add(string.Empty);
     view.CurrentLine      = 0;
     view.CurrentCharacter = 0;
 }
Ejemplo n.º 5
0
        private void HandleTyping(ObservableCollection <string> document, SubmissionView view, string text)
        {
            var lineIndex = view.CurrentLine;
            var start     = view.CurrentCharacter;

            document[lineIndex]    = document[lineIndex].Insert(start, text);
            view.CurrentCharacter += text.Length;
        }
Ejemplo n.º 6
0
 private void HandlePageDown(ObservableCollection <string> document, SubmissionView view)
 {
     submissionHistoryIndex++;
     if (submissionHistoryIndex > submissionHistory.Count - 1)
     {
         submissionHistoryIndex = 0;
     }
     UpdateDocumentFromHistory(document, view);
 }
Ejemplo n.º 7
0
 private void HandlePageUp(ObservableCollection <string> document, SubmissionView view)
 {
     submissionHistoryIndex--;
     if (submissionHistoryIndex < 0)
     {
         submissionHistoryIndex = submissionHistory.Count - 1;
     }
     UpdateDocumentFromHistory(document, view);
 }
Ejemplo n.º 8
0
        private void HandleDownArrow(ObservableCollection <string> document, SubmissionView view)
        {
            var totalLines = document.Count;

            if (view.CurrentLine < totalLines - 1)
            {
                view.CurrentLine++;
            }
        }
Ejemplo n.º 9
0
        private void HandleRightArrow(ObservableCollection <string> document, SubmissionView view)
        {
            var lineWidth = document[view.CurrentLine].Length;

            if (view.CurrentCharacter <= lineWidth - 1)
            {
                view.CurrentCharacter++;
            }
        }
Ejemplo n.º 10
0
        private void HandleTab(ObservableCollection <string> document, SubmissionView view)
        {
            const int tabWidth        = 4;
            var       start           = view.CurrentCharacter;
            var       remainingSpaces = tabWidth - start % tabWidth;
            var       line            = document[view.CurrentLine];

            document[view.CurrentLine] = line.Insert(start, new string(' ', remainingSpaces));
            view.CurrentCharacter     += remainingSpaces;
        }
Ejemplo n.º 11
0
        private void HandleEnter(ObservableCollection <string> document, SubmissionView view)
        {
            var submissionText = string.Join(Environment.NewLine, document);

            if (submissionText.StartsWith("#")) // || IsCompleteSubmission(submissionText))
            {
                done = true;
                return;
            }

            InsertLine(document, view);
        }
Ejemplo n.º 12
0
        private void InsertLine(ObservableCollection <string> document, SubmissionView view)
        {
            var remainder = document[view.CurrentLine].Substring(view.CurrentCharacter);

            document[view.CurrentLine] = document[view.CurrentLine].Substring(0, view.CurrentCharacter);

            var lineIndex = view.CurrentLine + 1;

            document.Insert(lineIndex, remainder);
            view.CurrentCharacter = 0;
            view.CurrentLine      = lineIndex;
        }
Ejemplo n.º 13
0
        private string EditSubmission()
        {
            done = false;

            var document = new ObservableCollection <string>()
            {
                ""
            };
            var view = new SubmissionView(RenderLine, document);

            while (!done)
            {
                var key = Console.ReadKey(true);
                HandleKey(key, document, view);
            }

            view.CurrentLine      = document.Count - 1;
            view.CurrentCharacter = document[view.CurrentLine].Length;
            Console.WriteLine();

            return(string.Join(Environment.NewLine, document));
        }
Ejemplo n.º 14
0
        private void HandleDelete(ObservableCollection <string> document, SubmissionView view)
        {
            var lineIndex = view.CurrentLine;
            var line      = document[lineIndex];
            var start     = view.CurrentCharacter;

            if (start >= line.Length)
            {
                if (view.CurrentLine == document.Count - 1)
                {
                    return;
                }

                var nextLine = document[lineIndex + 1];
                document[view.CurrentLine] += nextLine;
                document.RemoveAt(view.CurrentLine + 1);
            }
            else
            {
                var before = line.Substring(0, start);
                var after  = line.Substring(start + 1);
                document[lineIndex] = before + after;
            }
        }
Ejemplo n.º 15
0
 private void HandleControlEnter(ObservableCollection <string> document, SubmissionView view)
 {
     done = true;
 }
Ejemplo n.º 16
0
        private void UpdateDocumentFromHistory(ObservableCollection <string> document, SubmissionView view)
        {
            if (submissionHistory.Count == 0)
            {
                return;
            }

            document.Clear();

            var historyItem = submissionHistory[submissionHistoryIndex];
            var lines       = historyItem.Split(Environment.NewLine);

            foreach (var line in lines)
            {
                document.Add(line);
            }

            view.CurrentLine      = document.Count - 1;
            view.CurrentCharacter = document[view.CurrentLine].Length;
        }
Ejemplo n.º 17
0
 private void HandleHome(ObservableCollection <string> document, SubmissionView view)
 {
     view.CurrentCharacter = 0;
 }
Ejemplo n.º 18
0
        private void HandleKey(
            ConsoleKeyInfo key,
            ObservableCollection <string> document,
            SubmissionView view)
        {
            if (key.Modifiers == default(ConsoleModifiers))
            {
                switch (key.Key)
                {
                case ConsoleKey.Escape:
                    HandleEscape(document, view);
                    break;

                case ConsoleKey.Enter:
                    HandleEnter(document, view);
                    break;

                case ConsoleKey.LeftArrow:
                    HandleLeftArrow(document, view);
                    break;

                case ConsoleKey.RightArrow:
                    HandleRightArrow(document, view);
                    break;

                case ConsoleKey.UpArrow:
                    HandleUpArrow(document, view);
                    break;

                case ConsoleKey.DownArrow:
                    HandleDownArrow(document, view);
                    break;

                case ConsoleKey.Backspace:
                    HandleBackspace(document, view);
                    break;

                case ConsoleKey.Delete:
                    HandleDelete(document, view);
                    break;

                case ConsoleKey.Home:
                    HandleHome(document, view);
                    break;

                case ConsoleKey.End:
                    HandleEnd(document, view);
                    break;

                case ConsoleKey.Tab:
                    HandleTab(document, view);
                    break;

                case ConsoleKey.PageUp:
                    HandlePageUp(document, view);
                    break;

                case ConsoleKey.PageDown:
                    HandlePageDown(document, view);
                    break;
                }
            }
            else if (key.Modifiers == ConsoleModifiers.Control)
            {
                switch (key.Key)
                {
                case ConsoleKey.Enter:
                    HandleControlEnter(document, view);
                    break;
                }
            }

            if (key.Key != ConsoleKey.Backspace && key.KeyChar >= ' ')
            {
                HandleTyping(document, view, key.KeyChar.ToString());
            }
        }
Ejemplo n.º 19
0
 private void HandleEnd(ObservableCollection <string> document, SubmissionView view)
 {
     view.CurrentCharacter = document[view.CurrentLine].Length;
 }