コード例 #1
0
        public void UpdateFooter(ConsoleInstance console)
        {
            string otherUpdate = string.Empty;

            foreach (ConsoleInstance instance in ConsoleAsync.Manager.Consoles)
            {
                if ((instance.Name != console.Name) && instance.Writer.HasUpdate)
                {
                    otherUpdate = "?";
                    break;
                }
            }

            List <string> consoleNames = ConsoleAsync.Manager.Consoles.Select(c => c.Name).ToList();
            string        counters     = string.Format(" {0}/{1}", consoleNames.IndexOf(console.Name) + 1, consoleNames.Count).PadRight(7);

            string footerData = string.Concat(
                (Console.CapsLock ? "-CAPS" : string.Empty).PadRight(6),
                (Console.NumberLock ? "-NUM" : string.Empty).PadRight(6),
                otherUpdate.PadRight(3)
                );

            int    spaceForName = ScreenWidth - counters.Length - footerData.Length;
            string name         = console.Name.Truncate(spaceForName).PadRight(spaceForName);

            string footerTemp = string.Concat(counters, name, footerData);

            changeFooter  = footerTemp != footerContent;
            footerContent = footerTemp;
        }
コード例 #2
0
        public ConsoleInstance CreateConsole(string consoleName)
        {
            if (string.IsNullOrEmpty(consoleName))
            {
                throw new Exception("Console name is null");
            }

            if (IsConsoleNameValid(consoleName) == false)
            {
                throw new Exception("Console name contains invalid chars");
            }

            ConsoleInstance console = new ConsoleInstance(consoleName);

            Consoles.Add(console);

            if (initializing)
            {
                actualConsoleName = consoleName;
                UpdateTitle();
            }

            initializing = false;
            return(console);
        }
コード例 #3
0
 public void Execute(ConsoleInstance console, Action <IConsoleWriter> action)
 {
     action(console.Writer);
     if (console.Name == actualConsoleName)
     {
         Renderer.Render(ActiveConsole);
     }
 }
コード例 #4
0
        public void DestroyConsole(string consoleName)
        {
            ConsoleInstance console = GetConsole(consoleName, false);

            if (console != null)
            {
                DestroyConsole(console);
            }
        }
コード例 #5
0
        internal ConsoleInstance GetConsole(string consoleName, bool withException = true)
        {
            ConsoleInstance console = Consoles.FirstOrDefault(c => c.Name == consoleName);

            if ((console == null) && withException)
            {
                throw new InvalidOperationException(string.Format("Console '{0}' not found!", consoleName));
            }
            return(console);
        }
コード例 #6
0
 public bool ExecuteCommand(ConsoleInstance console, string commandText)
 {
     foreach (ConsoleCommand command in console.Commands)
     {
         if (command.TryExecute(commandText))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #7
0
        public void Render(ConsoleInstance console)
        {
            if (HasValidDimension() == false)
            {
                return;
            }
            UpdateConsoleData();

            UpdateFooter(console);

            ScreenBuffer buffer = new ScreenBuffer(ScreenWidth, ScreenHeight);

            if (forceRender || console.Writer.HasUpdate)
            {
                RenderView(buffer, console);
            }
            if (forceRender || changeInput)
            {
                RenderInput(buffer);
            }
            if (forceRender || changeFooter)
            {
                RenderFooter(buffer);
            }

            IEnumerable <ShortRect> rects = GetRects(
                forceRender | console.Writer.HasUpdate,
                forceRender | changeInput,
                forceRender | changeFooter);

            foreach (ShortRect rect in rects)
            {
                ShortRect refRect = rect;
                bool      b       = WriteConsoleOutput(fileHandle, buffer.Buffer,
                                                       new Coord()
                {
                    X = (byte)ScreenWidth, Y = (byte)ScreenHeight
                },
                                                       new Coord()
                {
                    X = rect.Left, Y = rect.Top
                },
                                                       ref refRect);
            }

            console.Writer.ResetUpdate();
            changeFooter = false;
            changeInput  = false;
            forceRender  = false;
        }
コード例 #8
0
        private void RenderView(ScreenBuffer buffer, ConsoleInstance console)
        {
            RowCollection rows            = console.Writer.Lines;
            int           scrollRowOffset = console.Writer.OffsetRowIndex;
            int           totalRows       = (rows.Count < ViewHeight) ? rows.Count : ViewHeight;

            for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
            {
                Row row = (rows.Count < ViewHeight)
                    ? rows[rowIndex]
                    : rows[rows.Count - ViewHeight + rowIndex + scrollRowOffset];

                buffer.WriteAt(rowIndex, row);
            }

            buffer.SetColumnColors(ScrollColumnIndex, 0, ViewHeight, ConsoleColor.DarkGray, ConsoleColor.Black);
            buffer.WriteAt(ViewHeight - 1, ScrollColumnIndex, "\u25BC",
                           (scrollRowOffset < 0) ? ConsoleColor.White : ConsoleColor.DarkGray);
            buffer.WriteAt(0, ScrollColumnIndex, "\u25B2",
                           (rows.Count > ViewHeight - scrollRowOffset) ? ConsoleColor.White : ConsoleColor.DarkGray);

            int barSpace = ViewHeight - 2;
            int barStart = 0;
            int barEnd   = barSpace;

            if (rows.Count > ViewHeight)
            {
                int barHeight = (barSpace * ViewHeight) / rows.Count;
                if (barHeight < 1)
                {
                    barHeight = 1;
                }

                int positiveOffset = rows.Count + scrollRowOffset;
                barEnd = (barSpace * positiveOffset) / rows.Count;

                barStart = barEnd - barHeight;
            }

            for (int i = barStart; i < barEnd; i++)
            {
                buffer.WriteAt(i + 1, ScrollColumnIndex, "\u2588", ConsoleColor.DarkGray);
            }
        }
コード例 #9
0
        internal void DestroyConsole(ConsoleInstance console)
        {
            if (Consoles.Count < 2)
            {
                throw new InvalidOperationException("Cannot destroy last console. Use quit command instead");
            }

            if (ActiveConsole.Name == console.Name)
            {
                CicleConsole(-1);
            }

            Consoles.Remove(console);

            if (console.Workers.Count > 0)
            {
                Task.WaitAll(console.Workers.Select(w => w.StopAsync()).ToArray());
            }

            // ReSharper disable once RedundantAssignment
            console = null;
        }
コード例 #10
0
        private void ManageStandardOutput()
        {
            string update = consoleStandardOutput.ToString();

            consoleStandardOutput.Clear();

            if (ConsoleToStandardOutput == null)
            {
                return;
            }

            ConsoleInstance console = GetConsole(ConsoleToStandardOutput, false);

            if (console == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(update) == false)
            {
                console.GetWriter().Text(update);
            }
        }
コード例 #11
0
        public void ShowConsole(string consoleName)
        {
            ConsoleInstance console = GetConsole(consoleName);

            actualConsoleName = console.Name;
        }
コード例 #12
0
ファイル: GlobalInput.cs プロジェクト: 3HMonkey/consoleasync
        public void Execute(ConsoleInstance console, long elapsedMillisecond)
        {
            string issuedCommand = null;

            while (Console.KeyAvailable)
            {
                ConsoleKeyInfo input  = Console.ReadKey(true);
                var            ignore = console.ApplyKeyFilter(input);
                if (ignore)
                {
                    continue;
                }

                if (input.Key == ConsoleKey.Enter) // Send Command
                {
                    if (string.IsNullOrWhiteSpace(text) == false)
                    {
                        issuedCommand = text;
                        AddPreviousCommand(text);
                        SetUncompletedCommand(string.Empty);
                        ResetCommandIndex();
                        text      = string.Empty;
                        cursorPos = 0;
                    }
                }
                else if (functionKeyArray.Contains(input.Key)) // Function Keys
                {
                    KeyCommandDefinition fk = console.CommandKeys.GetCommand(input.Key);
                    if (fk != null)
                    {
                        if (fk.AutoEnter)
                        {
                            issuedCommand = fk.Command;
                        }
                        else
                        {
                            text      = fk.Command;
                            cursorPos = text.Length;
                        }
                    }
                }
                else if (input.Key == ConsoleKey.Tab) // Cicle Console
                {
                    OnCicleConsole((input.Modifiers == ConsoleModifiers.Shift) ? -1 : 1);
                }
                else if (input.Key == ConsoleKey.Escape) // Escape Pressed
                {
                    OnEscapePressed();
                }
                else if (input.Key == ConsoleKey.Delete) // Delete Char
                {
                    if (text.Length > 0)
                    {
                        text = text.RemoveChar(cursorPos);
                    }
                }
                else if (input.Key == ConsoleKey.Backspace) // Delete Previous Char
                {
                    if ((text.Length > 0) && (cursorPos > 0))
                    {
                        cursorPos--;
                        text = text.RemoveChar(cursorPos);
                    }
                }
                else if (input.Key == ConsoleKey.LeftArrow) // Move cursor to the Left
                {
                    if (cursorPos > 0)
                    {
                        if (input.Modifiers == ConsoleModifiers.Control)
                        {
                            FindWord(false);
                        }
                        else
                        {
                            cursorPos--;
                        }
                    }
                }
                else if (input.Key == ConsoleKey.RightArrow) // Move cursor to the Right
                {
                    if (cursorPos < text.Length)
                    {
                        if (input.Modifiers == ConsoleModifiers.Control)
                        {
                            FindWord(true);
                        }
                        else
                        {
                            cursorPos++;
                        }
                    }
                }
                else if ((input.Key == ConsoleKey.PageUp) && (input.Modifiers.HasFlag(ConsoleModifiers.Control))) // Get Previous Command
                {
                    text      = GetPreviousCommand();
                    cursorPos = text.Length;
                }
                else if ((input.Key == ConsoleKey.PageDown) && (input.Modifiers.HasFlag(ConsoleModifiers.Control))) // Get Next Command
                {
                    text      = GetNextCommand();
                    cursorPos = text.Length;
                }
                else if ((input.Key == ConsoleKey.Home) && (!input.Modifiers.HasFlag(ConsoleModifiers.Control)))  // Move cursor at Start
                {
                    cursorPos = 0;
                }
                else if ((input.Key == ConsoleKey.End) && (!input.Modifiers.HasFlag(ConsoleModifiers.Control)))  // Move cursor at End
                {
                    cursorPos = text.Length;
                }
                else if (console.Writer.ApplyKey(input))
                {
                    // Exclude scroll keys
                }
                else if (ConsoleAsync.AvailableInputChars.Contains(input.KeyChar.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()) == false)
                {
                    // Exclude non allowed chars
                }
                else
                {
                    if ((input.Key != ConsoleKey.Spacebar) || (cursorPos != 0))
                    {
                        if (cursorPos == text.Length)
                        {
                            text     += input.KeyChar;
                            cursorPos = text.Length;
                        }
                        else
                        {
                            text = string.Concat(text.Left(cursorPos), input.KeyChar, text.LeftRest(cursorPos));
                            cursorPos++;
                        }

                        SetUncompletedCommand(text);
                        ResetCommandIndex();
                    }
                }

                renderer.InputCommand = text;
                renderer.InputCommandCursorPosition = cursorPos;
            }


            if (string.IsNullOrEmpty(issuedCommand) == false)
            {
                OnCommandReceived(issuedCommand);
                // ReSharper disable once RedundantAssignment
                issuedCommand = null;
            }
        }