private Stack <ConsoleSheet> GenerateSheets(ViewPort viewPort, IEnumerable <ColumnDetails> columns, int displayMinWidth)
        {
            var pages = new Stack <ConsoleSheet>();
            IEnumerable <ColumnDetails> firstPage = FitColumns(viewPort, columns, displayMinWidth);
            var sheet = new ConsoleSheet {
                Columns = firstPage, IndexStart = 0, IndexEnd = firstPage.Count()
            };

            pages.Push(sheet);

            int runningTotal = firstPage.Count();

            while (runningTotal != columns.Count())
            {
                IEnumerable <ColumnDetails> nextPage = FitColumns(viewPort, columns.Skip(runningTotal), displayMinWidth);
                var nextSheet = new ConsoleSheet {
                    Columns = nextPage, IndexStart = runningTotal, IndexEnd = runningTotal + nextPage.Count()
                };
                runningTotal += nextPage.Count();
                pages.Push(nextSheet);
            }

            int originalPageCount = pages.Count;
            var unReadPages       = new Stack <ConsoleSheet>();

            for (int i = 0; i < originalPageCount; i++)
            {
                unReadPages.Push(pages.Pop());
            }
            return(unReadPages);
        }
        private TableRow GenerateHeaderFromSheet(ViewModel viewModel, ConsoleSheet currentSheet)
        {
            var row = new TableRow();
            List <TableCell> headers = new List <TableCell>();

            for (int i = currentSheet.IndexStart; i < currentSheet.IndexEnd; i++)
            {
                ColumnDetails column = currentSheet.Columns.ElementAt(i - currentSheet.IndexStart);

                var content = new List <ICellContent>();
                content.Add(
                    new BasicCellContent
                {
                    Value = column.columnName
                });
                if (_settings.displayTypes)
                {
                    content.Add(new BreakingRuleContentArea());
                    content.Add(new BasicCellContent {
                        ForegroundColor = ConsoleColor.Blue, Value = column.type.ToString()
                    });
                }
                headers.Add(new BasicTableCell
                {
                    ContentAreas = content.ToArray()
                });
            }
            row.Cells = headers.ToArray();
            return(row);
        }
 private void DrawLine(ConsoleSheet columnHeaderSizes)
 {
     Console.Write(verticalSeparator);
     foreach (int item in columnHeaderSizes.Columns.Select(d => d.columnWidth))
     {
         for (int i = 0; i < item; i++)
         {
             Console.Write(horizontalSeparator);
         }
         Console.Write(verticalSeparator);
     }
     Console.Write(Environment.NewLine);
 }
        private void WriteSummary(ViewModel viewModel, ConsoleSheet currentSheet, ConsoleFold currentFold, bool displayNulls)
        {
            Console.Write("Showing {0} to {1} of {2} Columns. Use Arrow Keys to Navigate. ", currentSheet.IndexStart, currentSheet.IndexEnd, viewModel.Columns.Count());

            if (displayNulls)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write(".NET type name. ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("? denotes Nullable=true.{0}", Environment.NewLine);
                Console.ResetColor();
            }
            Console.WriteLine("Showing {0} to {1} of {2} Rows Total. Press ENTER to quit;", currentFold.IndexStart, currentFold.IndexEnd, viewModel.RowCount);
        }
        public void Draw(ViewModel viewModel, ViewSettings settings)
        {
            _settings = settings;
            Console.Clear();

            unreadSheets = GenerateSheets(viewPort, viewModel.Columns, settings.displayMinWidth);
            unreadFolds  = GenerateFolds(viewPort, (int)viewModel.RowCount);

            _currentSheet = unreadSheets.Pop();
            _currentFold  = unreadFolds.Pop();
            DrawSheet(viewModel, _currentSheet, _currentFold, viewPort, settings.displayTypes, settings.displayNulls, settings.truncationIdentifier, settings.displayReferences);
            Console.Clear();
            Console.WriteLine("Exiting...");
        }
 private void WriteHeaderLine(ConsoleSheet columnDetails)
 {
     Console.Write(verticalSeparator);
     foreach (string name in columnDetails.Columns.Select(cd => cd.columnName))
     {
         if (name.Length < AppSettings.Instance.DisplayMinWidth.Value)
         {
             for (int i = 0; i < AppSettings.Instance.DisplayMinWidth.Value - name.Length; i++)
             {
                 Console.Write(" ");
             }
         }
         Console.Write(name);
         Console.Write(verticalSeparator);
     }
     Console.Write(Environment.NewLine);
 }
 private void WriteValues(ViewModel viewModel, ConsoleSheet columnsFitToScreen, ConsoleFold foldedRows)
 {
     for (int i = 0; i < foldedRows.Rows.Count(); i++)
     {
         var row = foldedRows.Rows.ElementAt(i);
         Console.Write(verticalSeparator);
         for (int j = 0; j < row.Length; j++)
         {
             var header = viewModel.Columns.ElementAt(j);
             if (columnsFitToScreen.Columns.Contains(header))
             {
                 Console.Write(header.GetFormattedValue(row[j]));
                 Console.Write(verticalSeparator);
             }
         }
         Console.WriteLine();
     }
 }
        private void DrawSheet(ViewModel viewModel, ConsoleSheet currentSheet, ConsoleFold currentFold, ViewPort viewPort, bool displayTypes, bool displayNulls, string truncationIdentifier, bool displayRefs)
        {
            Console.Clear();
            DisplayTable displayTable = new DisplayTable();

            displayTable.Header        = GenerateHeaderFromSheet(viewModel, currentSheet);
            displayTable.ColumnDetails = currentSheet.Columns.ToArray();
            displayTable.Rows          = GenerateRowsFromFold(viewModel, currentFold, currentSheet);
            new Tablular.TableWriter(viewPort).Draw(displayTable);

            WriteSummary(viewModel, currentSheet, currentFold, displayNulls);

            Input input = AwaitInput();

            switch (input)
            {
            case Input.NoOp:
                DrawSheet(viewModel, currentSheet, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                break;

            case Input.Quit:
                break;

            case Input.NextSheet:
                if (unreadSheets.Any())
                {
                    readSheets.Push(currentSheet);
                    ConsoleSheet nextPage = unreadSheets.Pop();
                    _currentSheet = nextPage;

                    DrawSheet(viewModel, nextPage, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                break;

            case Input.PrevSheet:
                if (readSheets.Any())
                {
                    unreadSheets.Push(currentSheet);
                    ConsoleSheet lastPage = readSheets.Pop();
                    _currentSheet = lastPage;

                    DrawSheet(viewModel, lastPage, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                break;

            case Input.NextFold:
                if (unreadFolds.Any())
                {
                    readFolds.Push(currentFold);
                    ConsoleFold nextFold = unreadFolds.Pop();
                    _currentFold = nextFold;

                    this.Update(viewModel, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                break;

            case Input.PrevFold:
                if (readFolds.Any())
                {
                    unreadFolds.Push(currentFold);
                    ConsoleFold lastFold = readFolds.Pop();
                    _currentFold = lastFold;

                    this.Update(viewModel, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort, displayTypes, displayNulls, truncationIdentifier, displayRefs);
                }
                break;
            }
        }
        private TableRow[] GenerateRowsFromFold(ViewModel viewModel, ConsoleFold currentFold, ConsoleSheet currentSheet)
        {
            List <TableRow> rows = new List <TableRow>();

            for (int i = currentFold.IndexStart; i < currentFold.IndexEnd; i++)
            {
                var row = new TableRow();
                List <TableCell> cells = new List <TableCell>();
                for (int j = currentSheet.IndexStart; j < currentSheet.IndexEnd; j++)
                {
                    cells.Add(new BasicTableCell
                    {
                        ContentAreas = new[] { new BasicCellContent
                                               {
                                                   Value = viewModel.Rows[i][j]
                                               } }
                    });
                }
                row.Cells = cells.ToArray();
                rows.Add(row);
            }
            return(rows.ToArray());
        }
        private void DrawSheet(ViewModel viewModel, ConsoleSheet currentSheet, ConsoleFold currentFold, ViewPort viewPort)
        {
            Console.Clear();
            DrawLine(currentSheet);
            WriteHeaderLine(currentSheet);
            DrawLine(currentSheet);
            WriteValues(viewModel, currentSheet, currentFold);
            DrawLine(currentSheet);
            WriteSummary(viewModel, currentSheet, currentFold);

            var input = AwaitInput();

            switch (input)
            {
            case Input.NoOp:
                DrawSheet(viewModel, currentSheet, currentFold, viewPort);
                break;

            case Input.Quit:
                break;

            case Input.NextSheet:
                if (unreadSheets.Any())
                {
                    readSheets.Push(currentSheet);
                    var nextPage = unreadSheets.Pop();
                    DrawSheet(viewModel, nextPage, currentFold, viewPort);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort);
                }
                break;

            case Input.PrevSheet:
                if (readSheets.Any())
                {
                    unreadSheets.Push(currentSheet);
                    var lastPage = readSheets.Pop();
                    DrawSheet(viewModel, lastPage, currentFold, viewPort);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort);
                }
                break;

            case Input.NextFold:
                if (unreadFolds.Any())
                {
                    readFolds.Push(currentFold);
                    var nextFold = unreadFolds.Pop();
                    DrawSheet(viewModel, currentSheet, nextFold, viewPort);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort);
                }
                break;

            case Input.PrevFold:
                if (readFolds.Any())
                {
                    unreadFolds.Push(currentFold);
                    var lastFold = readFolds.Pop();
                    DrawSheet(viewModel, currentSheet, lastFold, viewPort);
                }
                else
                {
                    DrawSheet(viewModel, currentSheet, currentFold, viewPort);
                }
                break;
            }
        }
 private void WriteSummary(ViewModel viewModel, ConsoleSheet currentSheet, ConsoleFold currentFold)
 {
     Console.WriteLine("Showing {0} to {1} of {2} Columns. Use Arrow Keys to Navigate.", currentSheet.IndexStart, currentSheet.IndexEnd, viewModel.Columns.Count());
     Console.WriteLine("Showing {0} to {1} of {2} Rows Total. Press ENTER to quit;", currentFold.IndexStart, currentFold.IndexEnd, viewModel.RowCount);
 }