Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
rtmigo committed Mar 20, 2021
1 parent 33b7de5 commit 5497243
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions lib/src/inner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@ import 'dart:math';

import 'package:quiver/iterables.dart' show enumerate;

enum Side { left, right, center }

class Align {
Align(this.column, this.side);

final dynamic column;
final Side side;
}

enum Side { left, right, center }
class Sort {
Sort(this.column, [this.ascending = true]);

int maxCellLength(List<String> row) => row.map((cell) => cell.length).reduce(max);
dynamic column;
bool ascending;
}

String alignCell(String text, int targetWidth, Side align) {
String alignText(String text, int targetWidth, Side align) {
switch (align) {
case Side.left:
return text.padRight(targetWidth);
case Side.right:
return text.padLeft(targetWidth);
case Side.center:
return alignCenter(text, targetWidth);
return alignTextCenter(text, targetWidth);
}
}

String alignCenter(String text, int targetWidth) {
String alignTextCenter(String text, int targetWidth) {
final half = (targetWidth - text.length) >> 1;
text = text.padLeft(text.length + half);
text = text.padRight(targetWidth);
Expand Down Expand Up @@ -125,11 +130,17 @@ class CellsColumn {
class CellsMatrix {
CellsMatrix(List<List<dynamic>> rawRows) {
if (rawRows.length <= 1) {
throw ArgumentError.value(rawRows.length, 'rawRows.length');
throw ArgumentError.value(rawRows.length, 'rawRows.length',
'Must contain at least two items: the header and the first row.');
}

// determining the maximum count of cells in each row
this.columnsCount = rawRows.map<int>((r) => r.length).reduce(max);
final columnsCount = rawRows.map<int>((r) => r.length).reduce(max);

if (columnsCount <= 0) {
throw ArgumentError('rawRows contains zero columns.');
}

// creating [rows] field
for (final srcRow in rawRows) {
// copying raw cell data into list on Cells
Expand All @@ -148,10 +159,9 @@ class CellsMatrix {
assert(this.rows.isNotEmpty);
}

List<Cell> get headerRow => rows[0];
List<Cell> get header => rows[0];

List<List<Cell>> rows = <List<Cell>>[];
late int columnsCount;

Iterable<Cell> iterCellsByColumn(int columnIdx) sync* {
for (var i = 0; i < rows.length; ++i) {
Expand All @@ -166,7 +176,7 @@ class CellsMatrix {
return col;
}
String colStr = col.toString();
for (var me in enumerate(headerRow)) {
for (var me in enumerate(header)) {
if (me.value.toString() == colStr) {
return me.index;
}
Expand All @@ -175,7 +185,7 @@ class CellsMatrix {
}

void sortBy(List<int> columnIndexes1based) {
if (this.columnsCount <= 0) {
if (this.columns.isEmpty) {
return;
}

Expand Down Expand Up @@ -265,25 +275,6 @@ Iterable<dynamic> enumerateColumn(List<List<dynamic>> rows, int colIndex) sync*
}
}

class Sort {
Sort(this.column, [this.ascending = true]);

dynamic column;
bool ascending;
}

String generateBar(int width, Side? align) {
switch (align) {
case null:
case Side.left:
return '-' * (width + 2);
case Side.right:
return '-' * (width + 1) + ':';
case Side.center:
return ':' + '-' * width + ':';
}
}

/// @param sorting Determines the sorting order.
String tabular(List<List<dynamic>> rows,
{List<Side>? headerAlign,
Expand Down Expand Up @@ -362,7 +353,7 @@ String tabular(List<List<dynamic>> rows,

iCol++;

formatted += alignCell(
formatted += alignText(
cell.toString(), matrix.columns[iCol].textWidth, matrix.columns[iCol].guessAlign());
}

Expand Down

0 comments on commit 5497243

Please sign in to comment.