Beispiel #1
0
        // special for tables
        public TextValue PrettyPrint(TypedValue value)
        {
            Logger.Assert(value.DataType.IsVariable, "pp non-var");
            Logger.WriteLine(3, "PrettyPrint {0}", value);
            var sb = new StringBuilder();

            if (value.DataType is DataTypeRelation)
            {
                var dss = DataSinkStream.Create(value.AsTable(), new StringWriter(sb));
                dss.OutputTable();
            }
            else
            {
                sb.Append($"{value.Format()} : {value.DataType}");
            }
            var ret = TextValue.Create(sb.ToString());

            Logger.WriteLine(3, "[PP]");
            return(ret);
        }
        // Internal table writer, called recursively
        void WriteTable()
        {
            if (_table.Degree == 0)
            {
                WriteLine(_table.GetCount() == 0 ? T0 : T1);
                return;
            }
            var rowvalues = new List <object[]>();

            int[]  widths = _table.Heading.Columns.Select(c => ColumnWidth(c)).ToArray();
            bool[] rjust  = _table.Heading.Columns.Select(c => RightJustify(c.DataType.Name)).ToArray();

            foreach (var row in _table.GetRows())
            {
                var rowval = new object[_table.Degree];
                for (var col = 0; col < _table.Degree; ++col)
                {
                    var dtype = _table.Heading.Columns[col].DataType;
                    var value = row.Values[col];
                    if (dtype is DataTypeRelation)
                    {
                        var sink = DataSinkStream.Create(value.AsTable(), _writer);
                        sink.WriteTable();
                        rowval[col] = sink;
                        widths[col] = Math.Max(widths[col], sink._width);
                    }
                    else
                    {
                        var val = value.ToString();
                        if (val.Length > MaxColumnWidth)
                        {
                            var s = String.Format("(...{0})", val.Length);
                            val = val.Substring(0, MaxColumnWidth - s.Length) + s;
                        }
                        rowval[col] = val;
                        widths[col] = Math.Max(widths[col], val.Length);
                    }
                }
                rowvalues.Add(rowval);
            }

            WriteLine(String.Join(_colsep,
                                  _table.Heading.Columns.Zip(widths, (c, w) => c.Name.PadRight(w))));
            WriteLine(new string(_rowsep[0], widths.Sum() + (widths.Length - 1) * _colsep.Length));

            foreach (var rowvalue in rowvalues)
            {
                var more = true;
                for (int subrow = 0; more; subrow++)
                {
                    more = false;
                    string[] vv = new string[_table.Degree];
                    for (int col = 0; col < _table.Degree; ++col)
                    {
                        if (rowvalue[col] is string)
                        {
                            vv[col] = (subrow == 0) ? rowvalue[col] as string : "";
                        }
                        else
                        {
                            var dss = rowvalue[col] as DataSinkStream;
                            vv[col] = (subrow < dss.Lines.Count) ? dss.Lines[subrow] : "";
                            if (subrow + 1 < dss.Lines.Count)
                            {
                                more = true;
                            }
                        }
                    }
                    WriteLine(String.Join(_colsep, Enumerable.Range(0, vv.Length).Select(x => PadToWidth(vv[x], widths[x], rjust[x]))));
                }
            }
        }