table.d 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. module renderable.table;
  2. import app;
  3. import renderable.rect;
  4. import renderable.renderable;
  5. import vector;
  6. import color;
  7. import cell;
  8. import std.stdio;
  9. import std.algorithm;
  10. import renderable.cellcachecontainer;
  11. import stacklayouttype;
  12. class Table : Renderable {
  13. int columns;
  14. int rows;
  15. int columnWidth;
  16. int rowHeight;
  17. Color tableColor;
  18. this(int columns, int rows, Color tableColor) {
  19. this(columns, rows, 7, 1, tableColor);
  20. }
  21. this(int columns, int rows, int columnWidth = 7, int rowHeight = 1, Color tableColor = Color.terminal()) {
  22. this.dimension = Vector((columns * columnWidth) + 1, (rows * rowHeight) + 1);
  23. this.columns = columns;
  24. this.rows = rows;
  25. this.columnWidth = columnWidth;
  26. this.rowHeight = rowHeight;
  27. this.tableColor = tableColor;
  28. }
  29. override Cell[] render() {
  30. Cell[] cells;
  31. for (int y = 0; y < this.dimension.y; ++y) {
  32. for (int x = 0; x < this.dimension. x ; ++x) {
  33. if ((x % this.columnWidth == 0) && (y % this.rowHeight != 0)) {
  34. cells ~= Cell(Vector(x, y), '│', this.tableColor);
  35. }
  36. if (y % rowHeight == 0) {
  37. wchar content;
  38. if ((x == 0) && (y == 0)) {
  39. content = '┌';
  40. } else if ((x == this.dimension. x - 1) && (y == 0)) {
  41. content = '┐';
  42. } else if ((x == 0) && (y == this.dimension. y - 1)) {
  43. content = '└';
  44. } else if ((x == this.dimension. x - 1) && (y == this.dimension. y - 1)) {
  45. content = '┘';
  46. } else if ((x == 0) && (y != 0)) {
  47. content = '├';
  48. } else if ((x == this.dimension. x - 1) && (y != 0)) {
  49. content = '┤';
  50. } else if ((x > 0) && (x < this.dimension. x ) && (x % this.columnWidth == 0) && (y != 0) && (y < this.dimension. y - 1)) {
  51. content ='┼';
  52. } else if ((x > 0) && (x < this.dimension. x ) && (x % this.columnWidth == 0) && (y == 0)) {
  53. content = '┬';
  54. } else if ((x > 0) && (x < this.dimension. x ) && (x % this.columnWidth == 0)) {
  55. content = '┴';
  56. } else {
  57. content = '─';
  58. }
  59. cells ~= Cell(Vector(x, y), content, this.tableColor);
  60. }
  61. }
  62. }
  63. return cells;
  64. }
  65. }