1- using System ;
1+ // #define LOG_KEYS
2+
3+ using System ;
4+
5+ #if LOG_KEYS
6+ using System . Collections . Generic ;
7+ using System . Text ;
8+ using NClap . Utilities ;
9+ #endif
210
311namespace NClap . ConsoleInput
412{
@@ -7,6 +15,10 @@ namespace NClap.ConsoleInput
715 /// </summary>
816 internal class ConsoleReader : IConsoleReader
917 {
18+ #if LOG_KEYS
19+ private readonly ConsoleStatusBar _statusBar = new ConsoleStatusBar { Enabled = true } ;
20+ #endif
21+
1022 private readonly int _defaultCursorSize ;
1123
1224 // Operation history
@@ -83,7 +95,7 @@ public string ReadLine()
8395 // Grab a new key press event.
8496 var key = ConsoleInput . ReadKey ( true ) ;
8597
86- #if VERBOSE
98+ #if LOG_KEYS
8799 // Log information about the key press.
88100 LogKey ( key ) ;
89101#endif
@@ -391,90 +403,71 @@ private void ProcessTabKeyPress(bool previous, bool lastOpWasCompleteToken)
391403 private void UpdateCursorSize ( ) =>
392404 ConsoleOutput . CursorSize = LineInput . InsertMode ? _defaultCursorSize : 100 ;
393405
394- #if false
406+ #if LOG_KEYS
395407 private void LogKey ( ConsoleKeyInfo key )
396408 {
397- var x = ConsoleOutput . CursorLeft ;
398- var y = ConsoleOutput . CursorTop ;
399-
400- var fgColor = ConsoleOutput . ForegroundColor ;
401- var bgColor = ConsoleOutput . BackgroundColor ;
409+ var builder = new StringBuilder ( ) ;
410+ builder . AppendFormat ( "[Key: {{{0, -12}}}] " , key . Key ) ;
402411
403- try
412+ if ( char . IsControl ( key . KeyChar ) )
404413 {
405- ConsoleOutput . SetCursorPosition ( 0 , 0 ) ;
406- ConsoleOutput . ForegroundColor = ConsoleColor . Yellow ;
407- ConsoleOutput . BackgroundColor = ConsoleColor . DarkBlue ;
408-
409- var builder = new StringBuilder ( ) ;
410- builder . AppendFormat ( "[Key: {{{0, -12}}}] " , key . Key ) ;
411-
412- if ( char . IsControl ( key . KeyChar ) )
413- {
414- builder . AppendFormat ( "[Char: 0x{0:X}] " , ( int ) key . KeyChar ) ;
415- }
416- else if ( key . KeyChar != ( char ) 0 )
417- {
418- builder . AppendFormat ( "[Char: '{0}' : 0x{1:X}] " , key . KeyChar , ( int ) key . KeyChar ) ;
419- }
414+ builder . AppendFormat ( "[Char: 0x{0:X}] " , ( int ) key . KeyChar ) ;
415+ }
416+ else if ( key . KeyChar != ( char ) 0 )
417+ {
418+ builder . AppendFormat ( "[Char: '{0}' : 0x{1:X}] " , key . KeyChar , ( int ) key . KeyChar ) ;
419+ }
420420
421- var modifiers = ( ConsoleModifiers ) 0 ;
422- var translationModifiers = ( ConsoleModifiers ) 0 ;
423- var modifierNames = new List < string > ( ) ;
421+ var modifiers = ( ConsoleModifiers ) 0 ;
422+ var translationModifiers = ( ConsoleModifiers ) 0 ;
423+ var modifierNames = new List < string > ( ) ;
424424
425- if ( key . Modifiers . HasFlag ( ConsoleModifiers . Control ) )
426- {
427- modifiers |= ConsoleModifiers . Control ;
428- modifierNames . Add ( "Ctrl" ) ;
429- }
425+ if ( key . Modifiers . HasFlag ( ConsoleModifiers . Control ) )
426+ {
427+ modifiers |= ConsoleModifiers . Control ;
428+ modifierNames . Add ( "Ctrl" ) ;
429+ }
430430
431- if ( key . Modifiers . HasFlag ( ConsoleModifiers . Alt ) )
432- {
433- modifiers |= ConsoleModifiers . Alt ;
434- modifierNames . Add ( "Alt" ) ;
435- }
431+ if ( key . Modifiers . HasFlag ( ConsoleModifiers . Alt ) )
432+ {
433+ modifiers |= ConsoleModifiers . Alt ;
434+ modifierNames . Add ( "Alt" ) ;
435+ }
436436
437- if ( key . Modifiers . HasFlag ( ConsoleModifiers . Shift ) )
438- {
439- modifiers |= ConsoleModifiers . Shift ;
440- translationModifiers |= ConsoleModifiers . Shift ;
441- }
437+ if ( key . Modifiers . HasFlag ( ConsoleModifiers . Shift ) )
438+ {
439+ modifiers |= ConsoleModifiers . Shift ;
440+ translationModifiers |= ConsoleModifiers . Shift ;
441+ }
442442
443- var translatedToChars = false ;
444- if ( modifiers . HasFlag ( ConsoleModifiers . Alt ) || modifiers . HasFlag ( ConsoleModifiers . Control ) )
443+ var translatedToChars = false ;
444+ if ( modifiers . HasFlag ( ConsoleModifiers . Alt ) || modifiers . HasFlag ( ConsoleModifiers . Control ) )
445+ {
446+ var chars = InputUtilities . GetChars ( key . Key , translationModifiers ) ;
447+ if ( chars . Length > 0 )
445448 {
446- var chars = InputUtilities . GetChars ( key . Key , translationModifiers ) ;
447- if ( chars . Length > 0 )
448- {
449- var charsAsString = new string ( chars ) ;
450- builder . AppendFormat ( "[{0}+{1}]" , string . Join ( "+" , modifierNames ) , charsAsString ) ;
451- translatedToChars = true ;
452- }
449+ var charsAsString = new string ( chars ) ;
450+ builder . AppendFormat ( "[{0}+{1}]" , string . Join ( "+" , modifierNames ) , charsAsString ) ;
451+ translatedToChars = true ;
453452 }
453+ }
454454
455- if ( ! translatedToChars )
456- {
457- if ( key . Modifiers . HasFlag ( ConsoleModifiers . Shift ) ) modifierNames . Add ( "Shift" ) ;
458-
459- if ( modifierNames . Count > 0 )
460- {
461- builder . AppendFormat ( "[{0}+{1}]" , string . Join ( "+" , modifierNames ) , key . Key ) ;
462- }
463- }
455+ if ( ! translatedToChars )
456+ {
457+ if ( key . Modifiers . HasFlag ( ConsoleModifiers . Shift ) ) modifierNames . Add ( "Shift" ) ;
464458
465- if ( builder . Length < ConsoleOutput . BufferWidth )
459+ if ( modifierNames . Count > 0 )
466460 {
467- builder . Append ( new string ( ' ' , ConsoleOutput . BufferWidth - builder . Length ) ) ;
461+ builder . AppendFormat ( "[{0}+{1}]" , string . Join ( "+" , modifierNames ) , key . Key ) ;
468462 }
469-
470- ConsoleOutput . Write ( builder . ToString ( ) ) ;
471463 }
472- finally
464+
465+ if ( builder . Length < ConsoleOutput . BufferWidth )
473466 {
474- ConsoleOutput . ForegroundColor = fgColor ;
475- ConsoleOutput . BackgroundColor = bgColor ;
476- ConsoleOutput . SetCursorPosition ( x , y ) ;
467+ builder . Append ( new string ( ' ' , ConsoleOutput . BufferWidth - builder . Length ) ) ;
477468 }
469+
470+ _statusBar . Set ( builder . ToString ( ) ) ;
478471 }
479472#endif
480473 }
0 commit comments