示例#1
0
        public static void Echo(
            this IDataObject dataObject,
            EchoEvaluationContext ctx)
        {
            var(@out, context, opts) = ctx;
            if (context.EchoMap.MappedCall(dataObject, ctx))
            {
                return;
            }

            var options = opts as TableFormattingOptions;

            options ??= (TableFormattingOptions)
            context.ShellEnv.GetValue <TableFormattingOptions>(ShellEnvironmentVar.display_tableFormattingOptions)
            .InitFrom(opts);
            options = new TableFormattingOptions(options)
            {
                PadLastColumn = false
            };
            var attrs = dataObject.GetAttributes();

            attrs.Sort((x, y) => x.Name.CompareTo(y.Name));

            object container = null;

            if (dataObject is DataValue dataValue &&
                !(dataValue.Value is IDataObject))
            {
                container = dataValue.Value;
            }

            var dt = GetVarsDataTable(context, container, attrs, options);

            dt.Echo(new EchoEvaluationContext(@out, context, options));
        }
示例#2
0
        static void AddIDataObjectToTable(
            CommandEvaluationContext context,
            Table table,
            IDataObject value,
            TableFormattingOptions options,
            int level = 0
            )
        {
            if (value == null)
            {
                table.Rows.Add(DumpAsText(context, null), DumpAsText(context, null), DumpAsText(context, null));
            }
            else
            {
                var tab        = "".PadLeft((level * 4), ' ');
                var dv         = value as DataValue;
                var valueType  = (dv != null) ? dv.ValueType?.UnmangledName() : value?.GetType().UnmangledName();
                var val        = dv?.Value;
                var foldSymbol = options.UnfoldCategories ? "[-]" : "[+]";
                var valnprefix = (dv == null) ? (context.ShellEnv.Colors.Highlight + foldSymbol + " ") : "";
                var attrs      = GetIDataOjbectContraintsText(context, value);

                var str = tab + valnprefix + value.Name + attrs;

                if (val == null)
                {
                    table.Rows.Add(
                        str,
                        valueType,
                        new StringWrapper(
                            (dv == null) ? "" : DumpAsText(context, null)
                            ));
                }
                else
                {
                    table.Rows.Add(
                        str,
                        valueType,
                        val
                        );

                    if (options.UnfoldItems && val != null)
                    {
                        AddObjectToTable(context, table, val, options, level + 1);
                    }
                }

                if ((value is DataObject dao) && options.UnfoldCategories)
                {
                    foreach (var attr in dao.GetAttributes())
                    {
                        AddIDataObjectToTable(context, table, attr, options, level + 1);
                    }
                }
            }
        }
示例#3
0
        public static void DumpObject(
            object obj,
            EchoEvaluationContext ctx)
        {
            var(@out, context, opts) = ctx;
            if (context.EchoMap.MappedCall(obj, ctx))
            {
                return;
            }

            var options = opts as TableFormattingOptions;

            options ??= (TableFormattingOptions)
            context.ShellEnv.GetValue <TableFormattingOptions>(ShellEnvironmentVar.display_tableFormattingOptions)
            .InitFrom(opts);
            options = new TableFormattingOptions(options)
            {
                PadLastColumn = false
            };
            var dt = GetVarsDataTable(context, obj, new List <IDataObject>(), options);

            dt.Echo(new EchoEvaluationContext(@out, context, options));
        }
示例#4
0
        static Table GetVarsDataTable(
            CommandEvaluationContext context,
            object container,
            List <IDataObject> values,
            TableFormattingOptions options)
        {
            var table = new Table();

            table.AddColumns("name", "type");
            table.Columns.Add("value", typeof(object));
            table.SetFormat("name", context.ShellEnv.Colors.Label + "{0}" + Rsf);
            table.SetFormat("type", context.ShellEnv.Colors.MediumDarkLabel + "{0}" + Tab + Rsf);
            table.SetHeaderFormat("type", "{0}" + Tab);
            foreach (var value in values)
            {
                AddIDataObjectToTable(context, table, value, options);
            }
            if (container != null && options.UnfoldItems)
            {
                AddObjectToTable(context, table, container, options);
            }
            return(table);
        }
示例#5
0
        static void AddObjectToTable(
            CommandEvaluationContext context,
            Table table,
            object obj,
            TableFormattingOptions options,
            int level = 0
            )
        {
            var tab  = "".PadLeft((level * 4), ' ');
            var prfx = context.ShellEnv.Colors.HalfDarkLabel;

            foreach (var(name, value, inf) in obj.GetMemberValues())
            {
                var attrs = GetMemberConstraintsText(context, inf);
                if (value == null)
                {
                    table.Rows.Add(tab + prfx + name + attrs + Rdc, inf.GetMemberValueType().UnmangledName(), DumpAsText(context, null));
                }
                else
                {
                    table.Rows.Add(tab + prfx + name + attrs + Rdc, inf.GetMemberValueType().UnmangledName(), value);
                }
            }
        }
示例#6
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;
        }