コード例 #1
0
        private static void DisplayNews(ITopNewsResponse response)
        {
            CellFormat headerFormat = new CellFormat()
            {
                ForegroundColor = Color.LightSeaGreen
            };
            IList <string> columns = new List <string>()
            {
                "Name", "Top News ID", "Revision ID", "Revision Date"
            };

            foreach (var category in response.Data.Categories)
            {
                Console.WriteLine($"Category: {category.Key}");

                var builder = new TableBuilder(headerFormat);
                foreach (var name in columns)
                {
                    builder.AddColumn(name);
                }

                var table = builder.Build();
                table.Config = TableConfig.Unicode();

                table.Config.wrapText      = true;
                table.Config.textWrapLimit = 40;

                IList <object> rowData = new List <object>();
                foreach (var page in category.Value)
                {
                    rowData.Clear();
                    rowData.Add(page.Page);
                    rowData.Add(page.TopNewsID);
                    rowData.Add(page.RevisionID);
                    rowData.Add(page.RevisionDate);

                    table.AddRow(rowData.ToArray());
                }

                Console.Write(table);
            }
        }
        private static void DisplayNews(ITopNewsHeadlinesResponse response)
        {
            CellFormat headerFormat = new CellFormat()
            {
                ForegroundColor = Color.LightSeaGreen
            };

            var builder = new TableBuilder(headerFormat);

            foreach (var name in new List <string>()
            {
                "Publish Date", "Headline", "Story ID"
            })
            {
                builder.AddColumn(name);
            }

            var table = builder.Build();

            table.Config = TableConfig.Unicode();

            table.Config.wrapText      = true;
            table.Config.textWrapLimit = 40;

            IList <object> rowData = new List <object>();

            foreach (var headline in response.Data.Headlines)
            {
                rowData.Clear();
                rowData.Add(headline.VersionCreated);
                rowData.Add(headline.Text);
                rowData.Add(headline.StoryId);

                table.AddRow(rowData.ToArray());
            }

            Console.Write(table);
        }
コード例 #3
0
        // DisplayRows
        // Generic routine to display the rows of a table
        public static void DisplayRows(DataColumnCollection columns, IEnumerable <DataRow> rows, int maxCols = 0, int maxRows = 0)
        {
            CellFormat headerFormat = new CellFormat()
            {
                ForegroundColor = Color.LightSeaGreen
            };
            var builder = new TableBuilder(headerFormat);

            var displayCols = maxCols <= 0 ? columns.Count : maxCols;

            foreach (DataColumn col in columns)
            {
                builder.AddColumn(col.ColumnName);
                if (--displayCols == 0)
                {
                    break;
                }
            }

            if (maxCols > 0 && maxCols < columns.Count)
            {
                builder.AddColumn($"{columns.Count - maxCols} more");
            }

            var table = builder.Build();

            table.Config = TableConfig.Unicode();

            table.Config.wrapText      = true;
            table.Config.textWrapLimit = 40;

            var rowCount    = rows.ToArray().Length;
            var displayRows = maxRows <= 0 ? rowCount : maxRows;

            displayCols = maxCols <= 0 ? columns.Count : maxCols;
            IList <object> rowData = new List <object>();

            foreach (DataRow dataRow in rows)
            {
                foreach (object item in dataRow.ItemArray)
                {
                    if (rowData.Count == displayCols)
                    {
                        rowData.Add("...");
                        break;
                    }

                    switch (item)
                    {
                    case null:
                        break;

                    case JValue val:
                        var str = val.ToString();
                        rowData.Add(str);
                        break;

                    case IEnumerable <JToken> list:
                        rowData.Add($"[{string.Join(", ", list.Select(node => $"{node}"))}]");
                        break;

                    default:
                        rowData.Add(item);
                        break;
                    }
                }

                table.AddRow(rowData.ToArray());
                rowData.Clear();

                if (--displayRows == 0)
                {
                    break;
                }
            }

            if (maxRows > 0 && maxRows < rowCount)
            {
                table.AddRow($"<{rowCount - maxRows} more>...");
            }

            Console.Write(table);
        }