Ejemplo n.º 1
0
        public static void Log(string s)
        {
            if (ForwardLogsToSystemDiagnostics)
            {
                System.Diagnostics.Debug.WriteLine(s);
            }
            var ls = (s + "").Split(_crlf, StringSplitOptions.None)
                     .Select(x => Colors.Log + x);

            //Out.Echoln(ls);
            foreach (var l in ls)
            {
                Out.Echoln(l);
            }
        }
Ejemplo n.º 2
0
        static void _Echo(
            this DataTable table,
            ConsoleTextWriterWrapper @out,
            CommandEvaluationContext context,
            TableFormattingOptions options = null)
        {
            options ??= context.ShellEnv.GetValue <TableFormattingOptions>(ShellEnvironmentVar.display_tableFormattingOptions);
            @out.EnableFillLineFromCursor = false;
            @out.HideCur();
            var colLengths = new int[table.Columns.Count];

            foreach (var rw in table.Rows)
            {
                var cols = ((DataRow)rw).ItemArray;
                for (int i = 0; i < cols.Length; i++)
                {
                    string s, s2;
                    if (table is Table t)
                    {
                        s             = @out.GetPrint(t.GetFormatedValue(context, table.Columns[i].ColumnName, cols[i]?.ToString())) ?? "";
                        colLengths[i] = Math.Max(s.Length, colLengths[i]);
                        s2            = @out.GetPrint(t.GetFormatedHeader(table.Columns[i].ColumnName)) ?? "";
                        colLengths[i] = Math.Max(s2.Length, colLengths[i]);
                        if (i == cols.Length - 1)
                        {
                            colLengths[i] = s.Length + 2;
                        }
                    }
                    else
                    {
                        s             = @out.GetPrint(cols[i]?.ToString()) ?? "";
                        colLengths[i] = Math.Max(s.Length, colLengths[i]);
                        colLengths[i] = Math.Max(table.Columns[i].ColumnName.Length, colLengths[i]);
                        if (i == cols.Length - 1)
                        {
                            colLengths[i] = 1;
                        }
                        if (i == cols.Length - 1)
                        {
                            colLengths[i] = s.Length + 2;
                        }
                    }
                }
            }
            var colsep = options.NoBorders ?
                         " ".PadLeft(Math.Max(1, options.ColumnRightMargin))
                : (context.ShellEnv.Colors.TableBorder + " | " + context.ShellEnv.Colors.Default);


            var colseplength = options.NoBorders ? 0 : 3;
            var tablewidth   = options.NoBorders ? 0 : 3;

            for (int i = 0; i < table.Columns.Count; i++)
            {
                tablewidth += table.Columns[i].ColumnName.PadRight(colLengths[i], ' ').Length + colseplength;
            }

            var line = options.NoBorders ? "" : (context.ShellEnv.Colors.TableBorder + "".PadRight(tablewidth, '-'));

            if (!options.NoBorders)
            {
                @out.Echoln(line);
            }
            string fxh(string header) => (table is Table t) ? t.GetFormatedHeader(header) : header;

            // headers

            for (int i = 0; i < table.Columns.Count; i++)
            {
                if (!options.NoBorders && i == 0)
                {
                    @out.Echo(colsep);
                }

                var col     = table.Columns[i];
                var colName = (i == table.Columns.Count - 1 && !options.PadLastColumn) ?
                              fxh(col.ColumnName)
                    : fxh(col.ColumnName).PadRight(colLengths[i], ' ');
                var prfx = (options.NoBorders) ? Uon : "";
                var pofx = (options.NoBorders) ? Tdoff : "";
                @out.Echo(context.ShellEnv.Colors.TableColumnName + prfx + colName + colsep + pofx);
            }
            @out.Echoln();
            if (!options.NoBorders)
            {
                @out.Echoln(line);
            }

            // rows

            string fhv(string header, string value) => (table is Table t) ? t.GetFormatedValue(context, header, value) : value;

            foreach (var rw in table.Rows)
            {
                if (context.CommandLineProcessor.CancellationTokenSource.IsCancellationRequested)
                {
                    @out.EnableFillLineFromCursor = true;
                    @out.ShowCur();
                    @out.Echoln(context.ShellEnv.Colors.Default + "");
                    return;
                }
                var row = (DataRow)rw;
                var arr = row.ItemArray;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (!options.NoBorders && i == 0)
                    {
                        @out.Echo(colsep);
                    }

                    var txt    = (arr[i] == null) ? "" : arr[i].ToString();
                    var fvalue = fhv(table.Columns[i].ColumnName, txt);
                    var o      = arr[i];

                    MethodInfo mi = null;
                    if (((!(o is string)) || table.Columns[i].DataType == typeof(object)) &&
                        o != null && (mi = o.GetEchoMethod()) != null)
                    {
                        // value dump via Echo primitive
                        @out.Echo("" + context.ShellEnv.Colors.Default);
                        var p0 = @out.CursorPos;
                        mi.InvokeEcho(o, new EchoEvaluationContext(@out, context, new FormatingOptions(options)
                        {
                            LineBreak = false
                        }));
                        var p1 = @out.CursorPos;
                        if (p1.Y == p0.Y)
                        {
                            var l   = p1.X - p0.X;
                            var spc = (i == arr.Length - 1 && !options.PadLastColumn) ? "" : ("".PadRight(Math.Max(0, colLengths[i] - l), ' '));
                            @out.Echo(spc);
                        }
                        @out.Echo(colsep);
                    }
                    else
                    {
                        // value dump by ToString
                        var l   = @out.GetPrint(fvalue).Length;
                        var spc = (i == arr.Length - 1 && !options.PadLastColumn) ? "" : ("".PadRight(Math.Max(0, colLengths[i] - l), ' '));
                        @out.Echo("" + context.ShellEnv.Colors.Default);
                        @out.Echo(fvalue);
                        @out.Echo(spc + colsep);
                    }
                }
                @out.Echoln();
            }
            @out.Echo(line + context.ShellEnv.Colors.Default.ToString());
            @out.ShowCur();
            @out.EnableFillLineFromCursor = true;
        }
        public CommandVoidResult Run(
            [Option("w", "width", "width in characters", true, true)] int width = 100,
            [Option("h", "height", "height in characters", true, true)] int height = 40,
            [Option("d", "decay-delta", "decay delta", true, true)] int decayDelta = 3,
            [Option("g", "gray", "gray mode - no colors")] bool gray = false,
            [Option("s", "slow", "0 max speed - pause time between images in ms", true, true)] int slow = 0,
            [Option("t", "no-text", "do not add text info above anim")] bool noText = false,
            [Option(null, "color-palette", "color palette. 36 symbols separated by " + PalettePatternSeparator, true, true)] string firePattern = DefaultFirePattern
        )
        {
            firePattern = firePattern.ToLower();

            var firePallete = firePattern.Split(PalettePatternSeparator);
            var sb = new StringBuilder(width * height * 20);

            if (!gray)
            {
                // add colors
                var n = firePallete.Length;
                var stp = 256 / n;
                int r = 0;
                int g = 0;
                int b = 0;
                for (int i = 0; i < firePallete.Length; i++)
                {
                    firePallete[i] = ANSI.SGR_SetForegroundColor24bits(r, g, b) + firePallete[i];
                    r += stp;
                    if (r > 100) g += stp;
                }
            }

            var o = new ConsoleTextWriterWrapper(System.Console.Out);
            var pixels = width * height;
            var pixelsArray = new int[pixels];
            var random = new Random();

            void createPixelsStructure()
            {
                for (var i = 0; i < pixels; i++)
                    pixelsArray[i] = 0;
            };

            void calculatePropagation()
            {
                for (var column = 0; column < width; column++)
                {
                    for (var row = 0; row < height; row++)
                    {
                        var index = column + (width * row);
                        updatePixelIntensity(index);
                    }
                }
            };

            void updatePixelIntensity(int index)
            {
                var belowIndex = index + width;
                if (belowIndex < width * height)
                {
                    var decay = (int)Math.Floor(random.NextDouble() * decayDelta);
                    var belowIntensity = pixelsArray[belowIndex];
                    var newIntensity = belowIntensity - decay;

                    if (index - decay > 0)
                        pixelsArray[index - decay] = newIntensity > 0 ? newIntensity : 0;
                }
            };

            void createSource()
            {
                for (var column = 0; column < width; column++)
                {
                    var overflowPixel = width * height;
                    var index = (overflowPixel - width) + column;

                    pixelsArray[index] = firePallete.Length - 1;
                }
            };

            void render()
            {
                sb.Clear();
                sb.Append(ANSI.CUP());
                try { o.HideCur(); } catch (Exception) { }

                for (var row = 0; row < height; row++)
                {
                    for (var column = 0; column < width; column++)
                    {
                        var index = column + (width * row);
                        var intensity = pixelsArray[index];
                        sb.Append(firePallete[intensity]);
                    }
                    sb.AppendLine();
                }
                o.WriteAsync(sb.ToString());
            };

            void start()
            {
                o.ClearScreen();
                try { o.HideCur(); } catch (Exception) { }
                createPixelsStructure();
                createSource();
            }

            start();

            var end = false;
            System.Console.CancelKeyPress += (o, e) => {
                System.Environment.Exit(0);
            };
            while (!end)
            {
                var sw0 = Stopwatch.StartNew();
                calculatePropagation();
                sw0.Stop();
                var sw1 = Stopwatch.StartNew();
                render();
                sw1.Stop();
                if (!noText) o.Echoln($"(rdc)Doom fire algo - {Math.Round(1d / (sw0.ElapsedMilliseconds + sw1.ElapsedMilliseconds) * 1000, 2)} fps");
                if (slow > 0) Thread.Sleep(slow);
            }

            try { o.ShowCur(); } catch (Exception) { };

            return CommandVoidResult.Instance;
        }
Ejemplo n.º 4
0
        public static void Print(
            ConsoleTextWriterWrapper @out,
            CancellationTokenSource cancellationTokenSource,
            Table table,
            bool noBorders     = false,
            bool padLastColumn = true)
        {
            @out.EnableFillLineFromCursor = false;
            @out.HideCur();
            var colLengths = new int[table.Columns.Count];

            foreach (var rw in table.Rows)
            {
                var cols = ((DataRow)rw).ItemArray;
                for (int i = 0; i < cols.Length; i++)
                {
                    var s = @out.GetPrint(table.GetFormatedValue(table.Columns[i].ColumnName, cols[i]?.ToString())) ?? "";
                    colLengths[i] = Math.Max(s.Length, colLengths[i]);
                    var s2 = @out.GetPrint(table.GetFormatedHeader(table.Columns[i].ColumnName)) ?? "";
                    colLengths[i] = Math.Max(s2.Length, colLengths[i]);
                }
            }
            var colsep       = noBorders ? " " : (ColorSettings.TableBorder + " | " + ColorSettings.Default);
            var colseplength = noBorders ? 0 : 3;
            var tablewidth   = noBorders ? 0 : 3;

            for (int i = 0; i < table.Columns.Count; i++)
            {
                tablewidth += table.Columns[i].ColumnName.PadRight(colLengths[i], ' ').Length + colseplength;
            }
            var line = noBorders ? "" : (ColorSettings.TableBorder + "".PadRight(tablewidth, '-'));

            if (!noBorders)
            {
                @out.Echoln(line);
            }
            for (int i = 0; i < table.Columns.Count; i++)
            {
                if (i == 0)
                {
                    @out.Echo(colsep);
                }
                var col     = table.Columns[i];
                var colName = (i == table.Columns.Count - 1 && !padLastColumn) ?
                              table.GetFormatedHeader(col.ColumnName)
                    : table.GetFormatedHeader(col.ColumnName).PadRight(colLengths[i], ' ');
                @out.Echo(ColorSettings.TableColumnName + colName + colsep);
            }
            @out.Echoln();
            if (!noBorders)
            {
                @out.Echoln(line);
            }

            foreach (var rw in table.Rows)
            {
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    @out.EnableFillLineFromCursor = true;
                    @out.ShowCur();
                    @out.Echoln(ColorSettings.Default + "");
                    return;
                }
                var row = (DataRow)rw;
                var arr = row.ItemArray;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i == 0)
                    {
                        Out.Echo(colsep);
                    }
                    var txt    = (arr[i] == null) ? "" : arr[i].ToString();
                    var fvalue = table.GetFormatedValue(table.Columns[i].ColumnName, txt);
                    var l      = Out.GetPrint(fvalue).Length;
                    var spc    = (i == arr.Length - 1 && !padLastColumn) ? "" : ("".PadRight(Math.Max(0, colLengths[i] - l), ' '));
                    @out.Echo(ColorSettings.Default + fvalue + spc + colsep);
                }
                @out.Echoln();
            }
            @out.Echoln(line + ColorSettings.Default.ToString());
            @out.ShowCur();
            @out.EnableFillLineFromCursor = true;
        }