示例#1
0
    /* Default constructor */

    /// <summary>
    /// Creates a new console instance, asynchronously initializing the console.
    /// </summary>
    /// <param name="enabled">True if console is enabled, else false.</param>
    /// <param name="logger">The logger associated with the console.</param>
    /// <param name="proxy">Proxy to the system console behind the scenes.</param>
    public Console(bool enabled, Logger logger, IConsoleProxy proxy)
    {
        IsEnabled = enabled;
        if (!IsEnabled)
        {
            return;
        }

        _consoleProxy = proxy;
        _logger       = logger;
        _logger.WaitForConsoleInitFunc = WaitForConsoleInit;
        _logger.OnWriteLine           += OnWriteLine;
        _logger.OnWrite += OnWrite;

        Task.Run(() =>
        {
            var consoleAllocated = ConsoleAllocator.Alloc();
            if (!consoleAllocated)
            {
                return;
            }

            _consoleCtrlDelegate = NotifyOnConsoleClose;
            Kernel32.SetConsoleCtrlHandler(_consoleCtrlDelegate, true);
            _consoleProxy.SetBackColor(_logger.BackgroundColor);
            _consoleProxy.SetForeColor(_logger.TextColor);
            _consoleProxy.Clear();
            ReloadedBannerLogger.PrintBanner(proxy, logger);
            IsReady = true;

            FlushQueuedMessages();
        });
    }
 private static void WriteLinesCentered(IConsoleProxy proxy, FormattedLine[] lines)
 {
     foreach (var line in lines)
     {
         WriteLineCentered(proxy, line);
     }
 }
        /// <summary>
        ///     Instructs the renderer to render a clear line from the current position to the end of the window overwriting
        ///     anything in its way.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            proxy.GetPosition(out var consoleProxy);
            var count = Math.Max(proxy.WindowWidth - consoleProxy.Left, 0);

            proxy.Write(new string(' ', count));
        }
示例#4
0
 /// <summary>
 ///     Instructs the renderer to render to the supplied proxy.
 /// </summary>
 /// <param name="proxy">The proxy to render to.</param>
 /// <param name="arg">The object to render.</param>
 /// <param name="culture">The culture to use for the render.</param>
 public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
 {
     foreach (var renderer in this.renderers)
     {
         renderer.Render(proxy, arg, culture);
     }
 }
        /// <summary>
        ///     Instructs the renderer to render to the supplied proxy.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            if (arg == null)
            {
                return;
            }

            object value;

            if (string.IsNullOrEmpty(this.Config))
            {
                value = arg;
            }
            else
            {
                var property = arg.GetType().GetRuntimeProperty(this.Config);
                value = property?.GetValue(arg);
            }

            if (!(value is IEnumerable enumerable))
            {
                foreach (var subRenderer in this.SubRenderes)
                {
                    subRenderer.Render(proxy, value, culture);
                }
            }
            public void WriteLine(IConsoleProxy proxy)
            {
                foreach (var segment in Segments)
                {
                    proxy.Write(segment.Text, segment.Color);
                }

                proxy.Write("\n");
            }
        /// <summary>
        ///     Writes the supplied object using the specified template.
        /// </summary>
        /// <param name="proxy">The proxy.</param>
        /// <param name="template">The template.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The Console Proxy.</returns>
        public static IConsoleProxy WriteTemplate(
            this IConsoleProxy proxy,
            Template template,
            object arg          = null,
            CultureInfo culture = null)
        {
            template.Render(proxy, arg, culture);

            return(proxy);
        }
示例#8
0
        /// <summary>
        ///     Instructs the renderer to render to the supplied proxy.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            proxy.GetPosition(out var consoleProxy);
            if (consoleProxy.Left != 0)
            {
                proxy.WriteLine();
            }

            proxy.Write(new string('-', proxy.WindowWidth));
        }
        /// <summary>
        ///     Writes the supplied object using the specified template.
        /// </summary>
        /// <param name="proxy">The proxy.</param>
        /// <param name="template">The template as a string. The template is parsed using the default template parser.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The Console Proxy.</returns>
        public static IConsoleProxy WriteTemplate(
            this IConsoleProxy proxy,
            string template,
            object arg          = null,
            CultureInfo culture = null)
        {
            var parsed = TemplateParser.Default.Parse(template);

            parsed.Render(proxy, arg, culture);
            return(proxy);
        }
示例#10
0
        private static void CenterCursor(IConsoleProxy proxy, int textLength)
        {
            // Get center, accounting for overflow.
            int consolePointer = (System.Console.WindowWidth - textLength) / 2;

            if (consolePointer < 0)
            {
                consolePointer = 0;
            }

            proxy.SetCursorPosition(consolePointer, System.Console.CursorTop);
        }
示例#11
0
        public static void PrintBanner(IConsoleProxy proxy, ILogger logger)
        {
            proxy.Write("\n\n");

            var lines = new[]
            {
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMMMMMMMMMMMMMMMMdo`    ", logger.TextColor), new FormattedLineSegment("    hMMM+ +MMMh", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMMMMMMMMMMMMMMMMMMh    ", logger.TextColor), new FormattedLineSegment("   `MMMM` dMMM/", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-          `yMMMN    ", logger.TextColor), new FormattedLineSegment("   +MMMh .MMMN`", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-          `sMMMN    ", logger.TextColor), new FormattedLineSegment("   dMMM/ sMMMy ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM- .sMMMMMMMMMMMMd    ", logger.TextColor), new FormattedLineSegment("  -MMMN  NMMM: ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-   `sNMMMMMMMdo`    ", logger.TextColor), new FormattedLineSegment("  sMMMy :MMMm  ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-     `oNMMMMy-      ", logger.TextColor), new FormattedLineSegment("  NMMM- yMMMo  ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-       `oNMMMMh-    ", logger.TextColor), new FormattedLineSegment(" :MMMm `MMMM.  ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-         `+mMMMMh:  ", logger.TextColor), new FormattedLineSegment(" yMMMo /MMMd   ", logger.ColorRed) }
                },
                new FormattedLine()
                {
                    Segments = new [] { new FormattedLineSegment("MMMM-            /mMMMMd:", logger.TextColor), new FormattedLineSegment("`MMMM. dMMM+   ", logger.ColorRed) }
                },
            };

            WriteLinesCentered(proxy, lines);
            proxy.Write("\n");
            PrintCoreVersion(proxy, logger);
            proxy.Write("\n\n");
        }
        /// <summary>
        ///     Instructs the renderer to render to the supplied proxy.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            var o = this.GetValueFromPropertyString(arg, this.Config);

            var shouldBeRendered = this.ShouldBeRendered(o);

            if (shouldBeRendered)
            {
                foreach (var subRenderer in this.SubRenderes)
                {
                    subRenderer.Render(proxy, arg, culture);
                }
            }
        }
        /// <summary>
        ///     Initializes a new instance of the Controller class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="proxy">The proxy.</param>
        /// <param name="setup">The setup. Optional overwrite of the extensions added to the console. Is not specified the Default setup is applied.</param>
        internal Controller(object model, IConsoleProxy proxy, Action <Controller> setup = null)
        {
            this.Model          = model;
            this.Proxy          = proxy;
            this.TemplateParser = new TemplateParser();

            this.ModelMap = ModelParser.Parse(this.Model);

            setup = setup ?? this.DefaultSetup;

            setup(this);

            this.resultTemplate = this.TemplateParser.Parse("{}");
        }
示例#14
0
        private static void PrintCoreVersion(IConsoleProxy proxy, ILogger logger)
        {
            var version     = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
            var coreVersion = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;

            if (Environment.IsWine)
            {
                coreVersion += " (via Wine)";
            }

            WriteLineCentered(proxy, new FormattedLine()
            {
                Segments = new [] { new FormattedLineSegment($"{version} // ", logger.TextColor), new FormattedLineSegment($"{coreVersion}", logger.ColorRed) }
            });
        }
        /// <summary>
        ///     Instructs the renderer to render to the supplied proxy.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            if (this.Template.Styles.TryGetValue(this.Config, out var style))
            {
                proxy.GetStyle(out var original);
                proxy.Style(style);
                foreach (var subRenderer in this.SubRenderes)
                {
                    subRenderer.Render(proxy, arg, culture);
                }

                proxy.Style(original);
            }
            else
            {
                foreach (var subRenderer in this.SubRenderes)
                {
                    subRenderer.Render(proxy, arg, culture);
                }
            }
        }
示例#16
0
        /// <summary>
        ///     Instructs the renderer change color to the specified value, render all SubRenderes and the change the color back to the original color.
        /// </summary>
        /// <param name="proxy">The proxy to render to.</param>
        /// <param name="arg">The object to render.</param>
        /// <param name="culture">The culture to use for the render.</param>
        public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
        {
            var consoleColor = this.GetColorFromConfigValue();

            if (consoleColor == null)
            {
                foreach (var subRenderer in this.SubRenderes)
                {
                    subRenderer.Render(proxy, arg, culture);
                }

                return;
            }

            proxy.GetStyle(out var original);
            proxy.Style(new ConsoleStyle("temp", consoleColor));
            foreach (var subRenderer in this.SubRenderes)
            {
                subRenderer.Render(proxy, arg, culture);
            }

            proxy.Style(original);
        }
示例#17
0
 static Program()
 {
     Logger     = new Logger();
     Console    = new ConsoleProxy();
     WebService = new WebService();
 }
 /// <summary>
 ///     Instructs the renderer to render to the supplied proxy.
 /// </summary>
 /// <param name="proxy">The proxy to render to.</param>
 /// <param name="arg">The object to render.</param>
 /// <param name="culture">The culture to use for the render.</param>
 public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
 {
 }
示例#19
0
 private static void WriteLineCentered(IConsoleProxy proxy, FormattedLine line)
 {
     CenterCursor(proxy, line.GetLength());
     line.WriteLine(proxy);
 }
示例#20
0
 public StreetNumberFileParser(IConsoleProxy consoleProxy)
 {
     this.consoleProxy = consoleProxy;
 }
示例#21
0
 public DoorNumberService(IParseFileToInt parser, IConsoleProxy consoleProxy)
 {
     this.parser       = parser;
     this.consoleProxy = consoleProxy;
 }
 /// <summary>
 ///     Instructs the renderer to render to the supplied proxy.
 /// </summary>
 /// <param name="proxy">The proxy to render to.</param>
 /// <param name="arg">The object to render.</param>
 /// <param name="culture">The culture to use for the render.</param>
 public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
 {
     proxy.WriteLine();
 }
示例#23
0
 /// <summary>
 ///     Instructs the renderer to render to the supplied proxy.
 /// </summary>
 /// <param name="proxy">The proxy to render to.</param>
 /// <param name="arg">The object to render.</param>
 /// <param name="culture">The culture to use for the render.</param>
 public abstract void Render(IConsoleProxy proxy, object arg, CultureInfo culture);
 /// <summary>
 ///     Instructs the renderer to render to the supplied proxy.
 /// </summary>
 /// <param name="proxy">The proxy to render to.</param>
 /// <param name="arg">The object to render.</param>
 /// <param name="culture">The culture to use for the render.</param>
 public override void Render(IConsoleProxy proxy, object arg, CultureInfo culture)
 {
     proxy.Write(this.value);
 }
示例#25
0
 public Calculator(ILogger logger, IWebService webService, IConsoleProxy consoleProxy)
 {
     _logger       = logger;
     _webService   = webService;
     _consoleProxy = consoleProxy;
 }
 /// <summary>
 ///     Renders the specified proxy.
 /// </summary>
 /// <param name="proxy">The proxy.</param>
 /// <param name="arg">The argument.</param>
 /// <param name="culture">The culture.</param>
 public void Render(IConsoleProxy proxy, object arg, CultureInfo culture = null)
 {
     this.RenderTree.Render(proxy, arg, culture);
 }