|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +import 'package:commander_ui/src/application/utils/terminal_tools.dart'; |
| 5 | +import 'package:commander_ui/src/domains/models/component.dart'; |
| 6 | +import 'package:mansion/mansion.dart'; |
| 7 | + |
| 8 | +final class Table with TerminalTools implements Component<void> { |
| 9 | + final List<List<String>> data; |
| 10 | + final List<String> columns; |
| 11 | + final bool lineSeparator; |
| 12 | + final bool columnSeparator; |
| 13 | + |
| 14 | + /// Creates a new instance of [Table]. |
| 15 | + Table({ |
| 16 | + this.data = const [], |
| 17 | + this.columns = const [], |
| 18 | + this.lineSeparator = false, |
| 19 | + this.columnSeparator = false, |
| 20 | + }); |
| 21 | + |
| 22 | + @override |
| 23 | + void handle() { |
| 24 | + _render(); |
| 25 | + } |
| 26 | + |
| 27 | + void _render() async { |
| 28 | + saveCursorPosition(); |
| 29 | + |
| 30 | + final buffer = StringBuffer(); |
| 31 | + |
| 32 | + _drawLineSeparator(buffer, |
| 33 | + left: '┌', |
| 34 | + middle: columnSeparator ? '┬' : '─', |
| 35 | + right: '┐', |
| 36 | + separator: '─'); |
| 37 | + _drawHeader(buffer); |
| 38 | + _drawLineSeparator(buffer, |
| 39 | + left: '├', |
| 40 | + middle: columnSeparator ? '┼' : '─', |
| 41 | + right: '┤', |
| 42 | + separator: '─'); |
| 43 | + |
| 44 | + for (var row in data) { |
| 45 | + final currentIndex = data.indexOf(row); |
| 46 | + _drawLine(buffer, currentIndex, row); |
| 47 | + } |
| 48 | + |
| 49 | + _drawLineSeparator(buffer, |
| 50 | + left: '└', |
| 51 | + middle: columnSeparator ? '┴' : '─', |
| 52 | + right: '┘', |
| 53 | + separator: '─'); |
| 54 | + |
| 55 | + clearFromCursorToEnd(); |
| 56 | + restoreCursorPosition(); |
| 57 | + saveCursorPosition(); |
| 58 | + |
| 59 | + stdout.write(buffer.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + List<int> getMaxCellWidths() { |
| 63 | + final List combined = [...columns, ...data]; |
| 64 | + return List.generate(columns.length, (col) { |
| 65 | + int maxWidth = 0; |
| 66 | + for (var row in combined) { |
| 67 | + if (row is List<String>) { |
| 68 | + for (final cell in row) { |
| 69 | + maxWidth = max(maxWidth, cell.length); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (row is String) { |
| 74 | + maxWidth = max(maxWidth, row.length); |
| 75 | + } |
| 76 | + } |
| 77 | + return maxWidth; |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + void _drawLineSeparator( |
| 82 | + StringBuffer buffer, { |
| 83 | + required String left, |
| 84 | + required String middle, |
| 85 | + required String right, |
| 86 | + required String separator, |
| 87 | + }) { |
| 88 | + final maxColWidths = getMaxCellWidths(); |
| 89 | + |
| 90 | + String line = left; |
| 91 | + for (int i = 0; i < maxColWidths.length; i++) { |
| 92 | + final isLast = i == maxColWidths.length - 1; |
| 93 | + |
| 94 | + line += separator * (maxColWidths[i] + 2); |
| 95 | + line += isLast ? right : middle; |
| 96 | + } |
| 97 | + |
| 98 | + buffer.writeln(line); |
| 99 | + } |
| 100 | + |
| 101 | + void _drawHeader(StringBuffer buffer) { |
| 102 | + final maxColWidths = getMaxCellWidths(); |
| 103 | + final headerBuffer = StringBuffer(); |
| 104 | + |
| 105 | + headerBuffer.write('│'); |
| 106 | + |
| 107 | + for (var i = 0; i < columns.length; i++) { |
| 108 | + headerBuffer.writeAnsiAll([ |
| 109 | + SetStyles(Style.bold), |
| 110 | + Print(' ${columns[i].padRight(maxColWidths[i])}'), |
| 111 | + SetStyles.reset, |
| 112 | + ]); |
| 113 | + |
| 114 | + if (i == columns.length - 1) { |
| 115 | + headerBuffer.writeAnsi(Print(' │')); |
| 116 | + } else { |
| 117 | + headerBuffer.writeAnsi(Print(columnSeparator ? ' │' : ' ')); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + buffer.writeln(headerBuffer.toString()); |
| 122 | + } |
| 123 | + |
| 124 | + void _drawLine(StringBuffer buffer, int currentIndex, List<String> row) { |
| 125 | + final maxColWidths = getMaxCellWidths(); |
| 126 | + |
| 127 | + if (![0, data.length].contains(currentIndex) && lineSeparator) { |
| 128 | + _drawLineSeparator(buffer, |
| 129 | + left: '├', |
| 130 | + middle: lineSeparator |
| 131 | + ? columnSeparator |
| 132 | + ? '┼' |
| 133 | + : '─' |
| 134 | + : '┼', |
| 135 | + right: '┤', |
| 136 | + separator: '─'); |
| 137 | + } |
| 138 | + |
| 139 | + String rowLine = '│'; |
| 140 | + for (var i = 0; i < columns.length; i++) { |
| 141 | + rowLine += ' ${row[i].padRight(maxColWidths[i])}'; |
| 142 | + |
| 143 | + if (i == columns.length - 1) { |
| 144 | + // headerBuffer.writeAnsi(Print(' │')); |
| 145 | + rowLine += ' │'; |
| 146 | + } else { |
| 147 | + // headerBuffer.writeAnsi(Print(columnSeparator ? ' │' : ' ')); |
| 148 | + rowLine += columnSeparator ? ' │' : ' '; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + buffer.writeln(rowLine); |
| 153 | + } |
| 154 | +} |
0 commit comments