From c30e6351e56cdaf769365288829646f9453b00a1 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 24 Dec 2021 16:05:04 -0500 Subject: [PATCH] border --- border.go | 45 ------------------------------------------ border/border.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ game.go | 16 +++++++-------- 3 files changed, 59 insertions(+), 53 deletions(-) delete mode 100644 border.go create mode 100644 border/border.go diff --git a/border.go b/border.go deleted file mode 100644 index 082beaa..0000000 --- a/border.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "strings" - - "github.com/maaslalani/gambit/board" -) - -const ( - cellHeight = 2 - cellWidth = 4 - marginLeft = 3 - marginTop = 1 - - vertical = "│" - horizontal = "─" -) - -// withMarginLeft returns a string with a prepended left margin -func withMarginLeft(s string) string { - return strings.Repeat(" ", marginLeft) + s -} - -// buildBorder returns a string with a border for a given row (top, middle, bottom) -func buildBorder(left, middle, right string) string { - border := left + horizontal + strings.Repeat(horizontal+horizontal+middle+horizontal, board.LastRow) - border += horizontal + horizontal + right + "\n" - return withMarginLeft(border) -} - -func topBorder() string { - return buildBorder("┌", "┬", "┐") -} - -func middleBorder() string { - return buildBorder("├", "┼", "┤") -} - -func bottomBorder() string { - return buildBorder("└", "┴", "┘") -} - -func bottomLabels() string { - return withMarginLeft(" A B C D E F G H\n") -} diff --git a/border/border.go b/border/border.go new file mode 100644 index 0000000..d10a51c --- /dev/null +++ b/border/border.go @@ -0,0 +1,51 @@ +package border + +import ( + "strings" + + "github.com/maaslalani/gambit/board" +) + +const ( + cellHeight = 2 + cellWidth = 4 + marginLeft = 3 + marginTop = 1 + + Vertical = "│" + Horizontal = "─" +) + +func Cell(x, y int) (int, int) { + col := (x - marginLeft) / cellWidth + row := board.LastRow - (y-marginTop)/cellHeight + return col, row +} + +// withMarginLeft returns a string with a prepended left margin +func withMarginLeft(s string) string { + return strings.Repeat(" ", marginLeft) + s +} + +// Build returns a string with a border for a given row (top, middle, bottom) +func Build(left, middle, right string) string { + border := left + Horizontal + strings.Repeat(Horizontal+Horizontal+middle+Horizontal, board.LastRow) + border += Horizontal + Horizontal + right + "\n" + return withMarginLeft(border) +} + +func Top() string { + return Build("┌", "┬", "┐") +} + +func Middle() string { + return Build("├", "┼", "┤") +} + +func Bottom() string { + return Build("└", "┴", "┘") +} + +func BottomLabels() string { + return withMarginLeft(" A B C D E F G H\n") +} diff --git a/game.go b/game.go index 84d73f0..0b01588 100644 --- a/game.go +++ b/game.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/lipgloss" dt "github.com/dylhunn/dragontoothmg" "github.com/maaslalani/gambit/board" + "github.com/maaslalani/gambit/border" "github.com/maaslalani/gambit/pieces" "github.com/maaslalani/gambit/position" ) @@ -39,14 +40,14 @@ func (m model) View() string { for r, rank := range ranks { if r == board.FirstRow { - s.WriteString(topBorder()) + s.WriteString(border.Top()) } count := 0 for c, cell := range rank { if c == board.FirstCol { label := fmt.Sprintf(" %d ", board.LastRow-r+1) - s.WriteString(Faint.Render(label) + vertical) + s.WriteString(Faint.Render(label) + border.Vertical) } if isNumeric(cell) { @@ -59,7 +60,7 @@ func (m model) View() string { break } } - s.WriteString(display + vertical) + s.WriteString(display + border.Vertical) count += 1 } } else { @@ -75,16 +76,16 @@ func (m model) View() string { } } } - s.WriteString(" " + style.Render(pieces.Display[string(cell)]) + " " + vertical) + s.WriteString(" " + style.Render(pieces.Display[string(cell)]) + " " + border.Vertical) count += 1 } } s.WriteRune('\n') if r != board.LastRow { - s.WriteString(middleBorder()) + s.WriteString(border.Middle()) } else { - s.WriteString(bottomBorder() + Faint.Render(bottomLabels())) + s.WriteString(border.Bottom() + Faint.Render(border.BottomLabels())) } } s.WriteRune('\n') @@ -99,8 +100,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } - col := (msg.X - marginLeft) / cellWidth - row := board.LastRow - (msg.Y-marginTop)/cellHeight + col, row := border.Cell(msg.X, msg.Y) if m.selected != "" { from := m.selected