File: SuperConsole.cs

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (617 lines) | stat: -rw-r--r-- 20,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Scripting.Utils;
using System.Text;

namespace Microsoft.Scripting.Hosting.Shell {
#if !SILVERLIGHT // SuperConsole (ConsoleColor)
    public sealed class SuperConsole : BasicConsole {

        #region Nested types: History, SuperConsoleOptions, Cursor

        /// <summary>
        /// Class managing the command history.
        /// </summary>
        class History {
            protected List<string> _list = new List<string>();
            private int _current;
            private bool _increment;         // increment on Next()

            public string Current {
                get {
                    return _current >= 0 && _current < _list.Count ? _list[_current] : String.Empty;
                }
            }

            public void Add(string line, bool setCurrentAsLast) {
                if (line != null && line.Length > 0) {
                    int oldCount = _list.Count;
                    _list.Add(line);
                    if (setCurrentAsLast || _current == oldCount) {
                        _current = _list.Count;
                    } else {
                        _current++;
                    }
                    // Do not increment on the immediately following Next()
                    _increment = false;
                }
            }

            public string Previous() {
                if (_current > 0) {
                    _current--;
                    _increment = true;
                }
                return Current;
            }

            public string Next() {
                if (_current + 1 < _list.Count) {
                    if (_increment) _current++;
                    _increment = true;
                }
                return Current;
            }
        }

        /// <summary>
        /// List of available options
        /// </summary>
        class SuperConsoleOptions {
            private List<string> _list = new List<string>();
            private int _current;
            private string _root;

            public int Count {
                get {
                    return _list.Count;
                }
            }

            private string Current {
                get {
                    return _current >= 0 && _current < _list.Count ? _list[_current] : String.Empty;
                }
            }

            public void Clear() {
                _list.Clear();
                _current = -1;
            }

            public void Add(string line) {
                if (line != null && line.Length > 0) {
                    _list.Add(line);
                }
            }

            public string Previous() {
                if (_list.Count > 0) {
                    _current = ((_current - 1) + _list.Count) % _list.Count;
                }
                return Current;
            }

            public string Next() {
                if (_list.Count > 0) {
                    _current = (_current + 1) % _list.Count;
                }
                return Current;
            }

            public string Root {
                get {
                    return _root;
                }
                set {
                    _root = value;
                }
            }
        }

        /// <summary>
        /// Cursor position management
        /// </summary>
        struct Cursor {
            /// <summary>
            /// Beginning position of the cursor - top coordinate.
            /// </summary>
            private int _anchorTop;
            /// <summary>
            /// Beginning position of the cursor - left coordinate.
            /// </summary>
            private int _anchorLeft;

            public void Anchor() {
                _anchorTop = Console.CursorTop;
                _anchorLeft = Console.CursorLeft;
            }

            public void Reset() {
                Console.CursorTop = _anchorTop;
                Console.CursorLeft = _anchorLeft;
            }

            public void Place(int index) {
                Console.CursorLeft = (_anchorLeft + index) % Console.BufferWidth;
                int cursorTop = _anchorTop + (_anchorLeft + index) / Console.BufferWidth;
                if (cursorTop >= Console.BufferHeight) {
                    _anchorTop -= cursorTop - Console.BufferHeight + 1;
                    cursorTop = Console.BufferHeight - 1;
                }
                Console.CursorTop = cursorTop;
            }

            public static void Move(int delta) {
                int position = Console.CursorTop * Console.BufferWidth + Console.CursorLeft + delta;

                Console.CursorLeft = position % Console.BufferWidth;
                Console.CursorTop = position / Console.BufferWidth;
            }
        }

        #endregion

        /// <summary>
        /// The console input buffer.
        /// </summary>
        private StringBuilder _input = new StringBuilder();

        /// <summary>
        /// Current position - index into the input buffer
        /// </summary>
        private int _current;

        /// <summary>
        /// The number of white-spaces displayed for the auto-indenation of the current line
        /// </summary>
        private int _autoIndentSize;

        /// <summary>
        /// Length of the output currently rendered on screen.
        /// </summary>
        private int _rendered;

        /// <summary>
        /// Command history
        /// </summary>
        private History _history = new History();

        /// <summary>
        /// Tab options available in current context
        /// </summary>
        private SuperConsoleOptions _options = new SuperConsoleOptions();

        /// <summary>
        /// Cursort anchor - position of cursor when the routine was called
        /// </summary>
        private Cursor _cursor;

        /// <summary>
        /// The command line that this console is attached to.
        /// </summary>
        private CommandLine _commandLine;

        public SuperConsole(CommandLine commandLine, bool colorful)
            : base(colorful) {
            ContractUtils.RequiresNotNull(commandLine, "commandLine");
            _commandLine = commandLine;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        private bool GetOptions() {
            _options.Clear();

            int len;
            for (len = _input.Length; len > 0; len--) {
                char c = _input[len - 1];
                if (Char.IsLetterOrDigit(c)) {
                    continue;
                } else if (c == '.' || c == '_') {
                    continue;
                } else {
                    break;
                }
            }

            string name = _input.ToString(len, _input.Length - len);
            if (name.Trim().Length > 0) {
                int lastDot = name.LastIndexOf('.');
                string attr, pref, root;
                if (lastDot < 0) {
                    attr = String.Empty;
                    pref = name;
                    root = _input.ToString(0, len);
                } else {
                    attr = name.Substring(0, lastDot);
                    pref = name.Substring(lastDot + 1);
                    root = _input.ToString(0, len + lastDot + 1);
                }

                try {
                    IList<string> result;
                    if (String.IsNullOrEmpty(attr)) {
                        result = _commandLine.GetGlobals(name);
                    } else {
                        result = _commandLine.GetMemberNames(attr);
                    }

                    _options.Root = root;
                    foreach (string option in result) {
                        if (option.StartsWith(pref, StringComparison.CurrentCultureIgnoreCase)) {
                            _options.Add(option);
                        }
                    }
                } catch {
                    _options.Clear();
                }
                return true;
            } else {
                return false;
            }
        }

        private void SetInput(string line) {
            _input.Length = 0;
            _input.Append(line);

            _current = _input.Length;

            Render();
        }

        private void Initialize() {
            _cursor.Anchor();
            _input.Length = 0;
            _current = 0;
            _rendered = 0;
        }

        // Check if the user is backspacing the auto-indentation. In that case, we go back all the way to
        // the previous indentation level.
        // Return true if we did backspace the auto-indenation.
        private bool BackspaceAutoIndentation() {
            if (_input.Length == 0 || _input.Length > _autoIndentSize) return false;

            // Is the auto-indenation all white space, or has the user since edited the auto-indentation?
            for (int i = 0; i < _input.Length; i++) {
                if (_input[i] != ' ') return false;
            }

            // Calculate the previous indentation level
            //!!! int newLength = ((input.Length - 1) / ConsoleOptions.AutoIndentSize) * ConsoleOptions.AutoIndentSize;            
            int newLength = _input.Length - 4;

            int backspaceSize = _input.Length - newLength;
            _input.Remove(newLength, backspaceSize);
            _current -= backspaceSize;
            Render();
            return true;
        }

        private void OnBackspace() {
            if (BackspaceAutoIndentation()) return;

            if (_input.Length > 0 && _current > 0) {
                _input.Remove(_current - 1, 1);
                _current--;
                Render();
            }
        }

        private void OnDelete() {
            if (_input.Length > 0 && _current < _input.Length) {
                _input.Remove(_current, 1);
                Render();
            }
        }

        private void Insert(ConsoleKeyInfo key) {
            char c;
            if (key.Key == ConsoleKey.F6) {
                Debug.Assert(FinalLineText.Length == 1);

                c = FinalLineText[0];
            } else {
                c = key.KeyChar;
            }
            Insert(c);
        }

        private void Insert(char c) {
            if (_current == _input.Length) {
                if (Char.IsControl(c)) {
                    string s = MapCharacter(c);
                    _current++;
                    _input.Append(c);
                    Output.Write(s);
                    _rendered += s.Length;
                } else {
                    _current++;
                    _input.Append(c);
                    Output.Write(c);
                    _rendered++;
                }
            } else {
                _input.Insert(_current, c);
                _current++;
                Render();
            }
        }

        private static string MapCharacter(char c) {
            if (c == 13) return "\r\n";
            if (c <= 26) return "^" + ((char)(c + 'A' - 1)).ToString();

            return "^?";
        }

        private static int GetCharacterSize(char c) {
            if (Char.IsControl(c)) {
                return MapCharacter(c).Length;
            } else {
                return 1;
            }
        }

        private void Render() {
            _cursor.Reset();
            StringBuilder output = new StringBuilder();
            int position = -1;
            for (int i = 0; i < _input.Length; i++) {
                if (i == _current) {
                    position = output.Length;
                }
                char c = _input[i];
                if (Char.IsControl(c)) {
                    output.Append(MapCharacter(c));
                } else {
                    output.Append(c);
                }
            }

            if (_current == _input.Length) {
                position = output.Length;
            }

            string text = output.ToString();
            Output.Write(text);

            if (text.Length < _rendered) {
                Output.Write(new String(' ', _rendered - text.Length));
            }
            _rendered = text.Length;
            _cursor.Place(position);
        }

        private void MoveLeft(ConsoleModifiers keyModifiers) {
            if ((keyModifiers & ConsoleModifiers.Control) != 0) {
                // move back to the start of the previous word
                if (_input.Length > 0 && _current != 0) {
                    bool nonLetter = IsSeperator(_input[_current - 1]);
                    while (_current > 0 && (_current - 1 < _input.Length)) {
                        MoveLeft();

                        if (IsSeperator(_input[_current]) != nonLetter) {
                            if (!nonLetter) {
                                MoveRight();
                                break;
                            }

                            nonLetter = false;
                        }
                    }
                }
            } else {
                MoveLeft();
            }
        }

        private static bool IsSeperator(char ch) {
            return !Char.IsLetter(ch);
        }

        private void MoveRight(ConsoleModifiers keyModifiers) {
            if ((keyModifiers & ConsoleModifiers.Control) != 0) {
                // move to the next word
                if (_input.Length != 0 && _current < _input.Length) {
                    bool nonLetter = IsSeperator(_input[_current]);
                    while (_current < _input.Length) {
                        MoveRight();

                        if (_current == _input.Length) break;
                        if (IsSeperator(_input[_current]) != nonLetter) {
                            if (nonLetter)
                                break;

                            nonLetter = true;
                        }
                    }
                }
            } else {
                MoveRight();
            }
        }

        private void MoveRight() {
            if (_current < _input.Length) {
                char c = _input[_current];
                _current++;
                Cursor.Move(GetCharacterSize(c));
            }
        }

        private void MoveLeft() {
            if (_current > 0 && (_current - 1 < _input.Length)) {
                _current--;
                char c = _input[_current];
                Cursor.Move(-GetCharacterSize(c));
            }
        }

        private const int TabSize = 4;
        private void InsertTab() {
            for (int i = TabSize - (_current % TabSize); i > 0; i--) {
                Insert(' ');
            }
        }

        private void MoveHome() {
            _current = 0;
            _cursor.Reset();
        }

        private void MoveEnd() {
            _current = _input.Length;
            _cursor.Place(_rendered);
        }

        public override string ReadLine(int autoIndentSize) {
            Initialize();

            _autoIndentSize = autoIndentSize;
            for (int i = 0; i < _autoIndentSize; i++)
                Insert(' ');

            bool inputChanged = false;
            bool optionsObsolete = false;

            for (; ; ) {
                ConsoleKeyInfo key = Console.ReadKey(true);

                switch (key.Key) {
                    case ConsoleKey.Backspace:
                        OnBackspace();
                        inputChanged = optionsObsolete = true;
                        break;
                    case ConsoleKey.Delete:
                        OnDelete();
                        inputChanged = optionsObsolete = true;
                        break;
                    case ConsoleKey.Enter:
                        return OnEnter(inputChanged);
                    case ConsoleKey.Tab: {
                            bool prefix = false;
                            if (optionsObsolete) {
                                prefix = GetOptions();
                                optionsObsolete = false;
                            }

                            // Displays the next option in the option list,
                            // or beeps if no options available for current input prefix.
                            // If no input prefix, simply print tab.
                            DisplayNextOption(key, prefix);
                            inputChanged = true;
                            break;
                        }
                    case ConsoleKey.UpArrow:
                        SetInput(_history.Previous());
                        optionsObsolete = true;
                        inputChanged = false;
                        break;
                    case ConsoleKey.DownArrow:
                        SetInput(_history.Next());
                        optionsObsolete = true;
                        inputChanged = false;
                        break;
                    case ConsoleKey.RightArrow:
                        MoveRight(key.Modifiers);
                        optionsObsolete = true;
                        break;
                    case ConsoleKey.LeftArrow:
                        MoveLeft(key.Modifiers);
                        optionsObsolete = true;
                        break;
                    case ConsoleKey.Escape:
                        SetInput(String.Empty);
                        inputChanged = optionsObsolete = true;
                        break;
                    case ConsoleKey.Home:
                        MoveHome();
                        optionsObsolete = true;
                        break;
                    case ConsoleKey.End:
                        MoveEnd();
                        optionsObsolete = true;
                        break;
                    case ConsoleKey.LeftWindows:
                    case ConsoleKey.RightWindows:
                        // ignore these
                        continue;

                    default:
                        if (key.KeyChar == '\x0D') goto case ConsoleKey.Enter;      // Ctrl-M
                        if (key.KeyChar == '\x08') goto case ConsoleKey.Backspace;  // Ctrl-H
                        Insert(key);
                        inputChanged = optionsObsolete = true;
                        break;
                }
            }
        }

        /// <summary>
        /// Displays the next option in the option list,
        /// or beeps if no options available for current input prefix.
        /// If no input prefix, simply print tab.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="prefix"></param>
        private void DisplayNextOption(ConsoleKeyInfo key, bool prefix) {
            if (_options.Count > 0) {
                string part = (key.Modifiers & ConsoleModifiers.Shift) != 0 ? _options.Previous() : _options.Next();
                SetInput(_options.Root + part);
            } else {
                if (prefix) {
                    Console.Beep();
                } else {
                    InsertTab();
                }
            }
        }

        /// <summary>
        /// Handle the enter key. Adds the current input (if not empty) to the history.
        /// </summary>
        /// <param name="inputChanged"></param>
        /// <returns>The input string.</returns>
        private string OnEnter(bool inputChanged) {
            Output.Write("\n");
            string line = _input.ToString();
            if (line == FinalLineText) return null;
            if (line.Length > 0) {
                _history.Add(line, inputChanged);
            }
            return line;
        }

        string FinalLineText {
            get {
                return Environment.OSVersion.Platform != PlatformID.Unix ? "\x1A" : "\x04";
            }
        }
    }
#else
    public sealed class SuperConsole : BasicConsole {
        public SuperConsole(CommandLine  commandLine, bool isColorful)
            : base(isColorful) {
        }
    }
#endif
}