Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 26 additions & 45 deletions src/MnkeyFog.CommandLine/BoardRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,16 @@ public string DrawBoards(

while (nextDrawnBoardIndex < gameView.BoardsCount) {
sbyte drawBoardIndex = nextDrawnBoardIndex;
var cellRowSeparatorChar = Configuration.CellRowSeparator ?? ' ';
var cellColumnSeparatorChar = Configuration.CellColumnSeparator ?? ' ';
var middleIntersection = (Configuration.HasColumnSeparators && Configuration.HasRowSeparators)
? Configuration.MiddleIntersection
: Configuration.HasColumnSeparators
? cellColumnSeparatorChar.ToString()
: Configuration.HasRowSeparators
? cellRowSeparatorChar.ToString()
: "";

var doShowBoardName = doShowBoardNameForAllBoards;
if (Configuration.HasTopBorder) {
DrawBorderRow(
gameView,
drawBoardIndex,
Configuration.HasLeftBorder ? Configuration.TopLeftCorner! : "",
Configuration.TopIntersection ?? cellRowSeparatorChar.ToString(),
Configuration.HasRightBorder ? Configuration.TopRightCorner! : "",
new string(cellRowSeparatorChar, Configuration.CellWidth),
Configuration.TopLeftCorner,
Configuration.TopIntersection,
Configuration.TopRightCorner,
Configuration.CellRowSeparator,
doShowBoardName,
maxRenderWidth,
sb
Expand All @@ -64,10 +55,10 @@ public string DrawBoards(
DrawBorderRow(
gameView,
drawBoardIndex,
Configuration.HasLeftBorder ? Configuration.LeftIntersection! : "",
middleIntersection!,
Configuration.HasRightBorder ? Configuration.RightIntersection! : "",
new string(cellRowSeparatorChar, Configuration.CellWidth),
Configuration.LeftIntersection,
Configuration.MiddleIntersection,
Configuration.RightIntersection,
Configuration.CellRowSeparator,
false,
maxRenderWidth,
sb
Expand All @@ -77,11 +68,6 @@ public string DrawBoards(
nextDrawnBoardIndex = DrawBoardSpacesRow(
gameView,
drawBoardIndex,
Configuration.HasLeftBorder,
Configuration.HasColumnSeparators,
Configuration.HasRightBorder,
cellColumnSeparatorChar.ToString(),
Configuration.CellWidth,
row,
boardRenderWidth,
doShowBoardName,
Expand All @@ -94,10 +80,10 @@ public string DrawBoards(
DrawBorderRow(
gameView,
drawBoardIndex,
Configuration.HasLeftBorder ? Configuration.BottomLeftCorner! : "",
Configuration.BottomIntersection ?? cellRowSeparatorChar.ToString(),
Configuration.HasRightBorder ? Configuration.BottomRightCorner! : "",
new string(cellRowSeparatorChar, Configuration.CellWidth),
Configuration.BottomLeftCorner,
Configuration.BottomIntersection,
Configuration.BottomRightCorner,
Configuration.CellRowSeparator,
false,
maxRenderWidth,
sb
Expand All @@ -123,10 +109,10 @@ public int GetBoardRenderWidth(BoardView board)
private sbyte DrawBorderRow(
GameView gameView,
sbyte startBoardIndex,
string startBarString,
string midBarString,
string endBarString,
string spanString,
string cellStartString,
string cellIntersectionString,
string cellEndString,
string cellSpanString,
bool doShowBoardName,
int maxRenderWidth,
StringBuilder sb
Expand All @@ -143,12 +129,12 @@ StringBuilder sb

DrawBoardLeader(doShowBoardName, sb, boardIndex, board);

sb.Append($"{startBarString}{spanString}");
sb.Append($"{cellStartString}{cellSpanString}");

for (sbyte col = 0; col < board.ColumnCount - 1; col += 1) {
sb.Append($"{midBarString}{spanString}");
sb.Append($"{cellIntersectionString}{cellSpanString}");
}
sb.Append(endBarString);
sb.Append(cellEndString);
}
sb.AppendLine();
return boardIndex;
Expand All @@ -161,11 +147,6 @@ StringBuilder sb
private sbyte DrawBoardSpacesRow(
GameView gameView,
sbyte startBoardIndex,
bool hasLeftBorder,
bool hasColumnSeparators,
bool hasRightBorder,
string columnSeparator,
int cellWidth,
sbyte rowIndex,
int boardRenderWidth,
bool doShowBoardName,
Expand All @@ -184,18 +165,18 @@ StringBuilder sb

DrawBoardLeader(doShowBoardName, sb, boardIndex, board);

if (hasLeftBorder) {
sb.Append(columnSeparator);
if (Configuration.HasLeftBorder) {
sb.Append(Configuration.CellColumnSeparator);
}
for (sbyte col = 0; col < board.ColumnCount; col += 1) {
if (col > 0 && hasColumnSeparators) {
sb.Append(columnSeparator);
if (col > 0 && Configuration.HasColumnSeparators) {
sb.Append(Configuration.CellColumnSeparator);
}
var body = CommandNameTool.SpaceCommandName(gameView, boardIndex, col, rowIndex);
DrawSpaceBody(body, cellWidth, sb);
DrawSpaceBody(body, Configuration.CellWidth, sb);
}
if (hasRightBorder) {
sb.Append(columnSeparator);
if (Configuration.HasRightBorder) {
sb.Append(Configuration.CellColumnSeparator);
}
}
sb.AppendLine();
Expand Down
206 changes: 185 additions & 21 deletions src/MnkeyFog.CommandLine/BoardRendererConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,204 @@

namespace MnkeyFog.CommandLine;

public record class BoardRendererConfiguration(
int CellWidth,
char? CellColumnSeparator, char? CellRowSeparator,
string? TopLeftCorner, string? TopIntersection, string? TopRightCorner,
string? LeftIntersection, string? MiddleIntersection, string? RightIntersection,
string? BottomLeftCorner, string? BottomIntersection, string? BottomRightCorner,
string EmptyBoardLeader, string DoneBoardLeader, Func<string, string> BoardNameMappingToBoardLeader
) {
public record class BoardRendererConfiguration {
#region constructors
public BoardRendererConfiguration(
int cellWidth,
char? cellColumnSeparator, char? cellRowSeparator,
char? topLeftCorner, char? topIntersection, char? topRightCorner,
char? leftIntersection, char? middleIntersection, char? rightIntersection,
char? bottomLeftCorner, char? bottomIntersection, char? bottomRightCorner,
string emptyBoardLeader, string doneBoardLeader, Func<string, string> boardNameMappingToBoardLeader
) {
CellWidth = cellWidth;
CellColumnSeparatorChar = cellColumnSeparator;
CellRowSeparatorChar = cellRowSeparator;
TopLeftCornerChar = topLeftCorner;
TopIntersectionChar = topIntersection;
TopRightCornerChar = topRightCorner;
LeftIntersectionChar = leftIntersection;
MiddleIntersectionChar = middleIntersection;
RightIntersectionChar = rightIntersection;
BottomLeftCornerChar = bottomLeftCorner;
BottomIntersectionChar = bottomIntersection;
BottomRightCornerChar = bottomRightCorner;
EmptyBoardLeader = emptyBoardLeader;
DoneBoardLeader = doneBoardLeader;
BoardNameMappingToBoardLeader = boardNameMappingToBoardLeader;
}
#endregion

#region Data members
public int CellWidth { get; }
public char? CellColumnSeparatorChar { get; }
public char? CellRowSeparatorChar { get; }
public char? TopLeftCornerChar { get; }
public char? TopIntersectionChar { get; }
public char? TopRightCornerChar { get; }
public char? LeftIntersectionChar { get; }
public char? MiddleIntersectionChar { get; }
public char? RightIntersectionChar { get; }
public char? BottomLeftCornerChar { get; }
public char? BottomIntersectionChar { get; }
public char? BottomRightCornerChar { get; }
public string EmptyBoardLeader { get; }
public string DoneBoardLeader { get; }
public Func<string, string> BoardNameMappingToBoardLeader { get; }
#endregion

private readonly string FinalFailoverString = " ";

#region Calculated members

[JsonIgnore]
public string TopLeftCorner {
get {
if (HasLeftBorder && HasTopBorder) {
return TopLeftCornerChar?.ToString() ?? FinalFailoverString;
}
return "";
}
}

[JsonIgnore]
public string TopIntersection {
get {
if (HasTopBorder) {
if (HasColumnSeparators) {
return TopIntersectionChar?.ToString() ?? FinalFailoverString;
} else {
return CellRowSeparatorChar?.ToString() ?? FinalFailoverString;
}
}
return "";
}
}

[JsonIgnore]
public string TopRightCorner {
get {
if (HasRightBorder && HasTopBorder) {
return TopRightCornerChar?.ToString() ?? FinalFailoverString;
}
return "";
}
}

[JsonIgnore]
public string LeftIntersection {
get {
if (HasLeftBorder) {
if (HasRowSeparators) {
return LeftIntersectionChar?.ToString() ?? FinalFailoverString;
} else {
return CellColumnSeparatorChar?.ToString() ?? FinalFailoverString;
}
}
return "";
}
}

[JsonIgnore]
public string MiddleIntersection {
get {
if (HasColumnSeparators && HasRowSeparators) {
return MiddleIntersectionChar?.ToString() ?? FinalFailoverString;
} else if (HasRowSeparators) {
return CellRowSeparatorChar?.ToString() ?? FinalFailoverString;
} else if (HasColumnSeparators) {
return CellColumnSeparatorChar?.ToString() ?? FinalFailoverString;
}
return "";
}
}

[JsonIgnore]
public string RightIntersection {
get {
if (HasRightBorder) {
if (HasRowSeparators) {
return RightIntersectionChar?.ToString() ?? FinalFailoverString;
} else {
return CellColumnSeparatorChar?.ToString() ?? FinalFailoverString;
}
}
return "";
}
}


[JsonIgnore]
public string BottomLeftCorner {
get {
if (HasLeftBorder && HasBottomBorder) {
return BottomLeftCornerChar?.ToString() ?? FinalFailoverString;
}
return "";
}
}

[JsonIgnore]
public string BottomIntersection {
get {
if (HasBottomBorder) {
if (HasColumnSeparators) {
return BottomIntersectionChar?.ToString() ?? FinalFailoverString;
} else {
return CellRowSeparatorChar?.ToString() ?? FinalFailoverString;
}
}
return "";
}
}

[JsonIgnore]
public string BottomRightCorner {
get {
if (HasRightBorder && HasBottomBorder) {
return BottomRightCornerChar?.ToString() ?? FinalFailoverString;
}
return "";
}
}

[JsonIgnore]
public string CellRowSeparator
=> HasRowSeparators
? new string(CellRowSeparatorChar ?? FinalFailoverString[0], CellWidth)
: "";

[JsonIgnore]
public string CellColumnSeparator
=> HasColumnSeparators
? (CellColumnSeparatorChar.ToString() ?? FinalFailoverString)
: "";

[JsonIgnore]
public bool HasTopBorder => TopLeftCorner != null || TopRightCorner != null;
public bool HasTopBorder => TopLeftCornerChar != null || TopRightCornerChar != null;

[JsonIgnore]
public bool HasBottomBorder => BottomLeftCorner != null || BottomRightCorner != null;
public bool HasBottomBorder => BottomLeftCornerChar != null || BottomRightCornerChar != null;

[JsonIgnore]
public bool HasLeftBorder => TopLeftCorner != null || BottomLeftCorner != null;
public bool HasLeftBorder => TopLeftCornerChar != null || BottomLeftCornerChar != null;

[JsonIgnore]
public bool HasRightBorder => TopRightCorner != null || BottomRightCorner != null;
public bool HasRightBorder => TopRightCornerChar != null || BottomRightCornerChar != null;

[JsonIgnore]
public bool HasColumnSeparators => TopIntersection != null || MiddleIntersection != null || BottomIntersection != null;
public bool HasColumnSeparators => TopIntersectionChar != null || MiddleIntersectionChar != null || BottomIntersectionChar != null;

[JsonIgnore]
public bool HasRowSeparators => LeftIntersection != null || MiddleIntersection != null || RightIntersection != null;
public bool HasRowSeparators => LeftIntersectionChar != null || MiddleIntersectionChar != null || RightIntersectionChar != null;
#endregion

// Factory methods for static configurations
public static BoardRendererConfiguration FullPipes { get; } = new BoardRendererConfiguration(
3, '│', '─',
"┌", "┬", "┐",
"├", "┼", "┤",
"└", "┴", "┘",
//leaders
'┌', '┬', '┐',
'├', '┼', '┤',
'└', '┴', '┘',
" ", " ✓", boardName => boardName.PadLeft(2)
);

Expand All @@ -42,16 +208,14 @@ public record class BoardRendererConfiguration(
null, null, null,
null, null, null,
null, null, null,
//leaders
" ", " ✓", boardName => boardName.PadLeft(2)
);

public static BoardRendererConfiguration HashPipes { get; } = new BoardRendererConfiguration(
3, '│', '─',
null, null, null,
null, "┼", null,
null, '┼', null,
null, null, null,
//leaders
" ", " ✓", boardName => boardName.PadLeft(2)
);
}