Skip to content

Commit

Permalink
feat: build tabs in standings page
Browse files Browse the repository at this point in the history
  • Loading branch information
dylantientcheu committed Dec 18, 2022
1 parent dcd2891 commit 9e62136
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 40 deletions.
7 changes: 4 additions & 3 deletions cmd/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

// args
var date = ""
var gameID = ""

// var gameID = ""

var hasYesterday = false
var hasTomorrow = false
Expand Down Expand Up @@ -54,10 +55,10 @@ var StandingCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(gameCmd)
rootCmd.AddCommand(StandingCmd)
rootCmd.PersistentFlags().StringVarP(&date, "date", "d", "", "Date to get the schedule for (YYYYMMDD)")
rootCmd.PersistentFlags().BoolVarP(&hasYesterday, "yesterday", "y", false, "Get yesterday's games")
rootCmd.PersistentFlags().BoolVarP(&hasTomorrow, "tomorrow", "t", false, "Get tomorrow's games")

rootCmd.MarkFlagsMutuallyExclusive("yesterday", "tomorrow", "date")

rootCmd.AddCommand(StandingCmd)
}
50 changes: 48 additions & 2 deletions ui/constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var BaseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#874BFD"))
BorderForeground(Accent)

var (
// P the current tea program
Expand Down Expand Up @@ -45,15 +45,54 @@ var (

InnerDivider: "│",
}

Accent = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}

activeTabBorder = lipgloss.Border{
Top: "─",
Bottom: " ",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "┘",
BottomRight: "└",
}

tabBorder = lipgloss.Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "┴",
BottomRight: "┴",
}

TabStyle = lipgloss.NewStyle().
Border(tabBorder, true).
BorderForeground(Accent).
Padding(0, 1)

ActiveTabStyle = lipgloss.NewStyle().
Border(activeTabBorder, true).
BorderForeground(Accent).
Padding(0, 1)

BleedSpaceWidth = 4
)

/* STYLING */

// DocStyle styling for viewports
var DocStyle = lipgloss.NewStyle().Margin(1, 2)

// TitleStyle styling for titles
var TitleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Background(Accent).Padding(0, 2)

// HelpStyle styling for help context menu
var HelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
var HelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))

// ErrStyle provides styling for error messages
var ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#bd534b")).Render
Expand Down Expand Up @@ -92,3 +131,10 @@ var Keymap = keymap{
key.WithHelp("ctrl+c/q", "quit"),
),
}

func Max(a, b int) int {
if a > b {
return a
}
return b
}
28 changes: 3 additions & 25 deletions ui/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,7 @@ import (

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#874BFD"))

var (
customBorder = table.Border{
Top: "─",
Left: "│",
Right: "│",
Bottom: "─",

TopRight: "╮",
TopLeft: "╭",
BottomRight: "╯",
BottomLeft: "╰",

TopJunction: "┬",
LeftJunction: "├",
RightJunction: "┤",
BottomJunction: "┴",
InnerJunction: "┼",

InnerDivider: "│",
}
)
BorderForeground(constants.Accent)

type keyMap struct {
Down key.Binding
Expand Down Expand Up @@ -101,14 +79,14 @@ func (m GameModel) View() string {
Up: key.NewBinding(key.WithKeys("up"), key.WithHelp("↑", "highlight previous row")),
Previous: key.NewBinding(key.WithKeys("esc", "q"), key.WithHelp("q/esc", "back to games list")),
}

helpContainer := lipgloss.NewStyle().
SetString(m.help.View(keyMap)).
Width(m.width).
Align(lipgloss.Center).
PaddingTop(1).
String()

// helpText :=
return scoretext.RenderScoreText(m.activeGame.ArenaName, m.activeGame.GameDate, m.activeGame.HomeTeamScore, m.activeGame.VisitorTeamScore, m.activeGame.HomeTeamName, m.activeGame.VisitorTeamName) + table + helpContainer
}

Expand All @@ -133,7 +111,7 @@ func InitGameView(activeGameID string, activeGame nba.BoxScoreSummary, previousM

t := table.New(columns).WithRows(rows).
Focused(true).
Border(customBorder).WithBaseStyle(baseStyle).WithPageSize(constants.WindowSize.Height / 3)
Border(constants.CustomTableBorder).WithBaseStyle(baseStyle).WithPageSize(constants.WindowSize.Height / 3)

m := GameModel{t, activeGameID, activeGame, previousModel, help.New(), constants.WindowSize.Height, constants.WindowSize.Width, 3}
return &m
Expand Down
3 changes: 2 additions & 1 deletion ui/gameboard/scoretext/scoretext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/dylantientcheu/nbacli/ui/constants"
"golang.org/x/term"
)

var (
subtle = lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#212121"}
dialogBoxStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#874BFD")).
BorderForeground(constants.Accent).
Padding(1, 1).
BorderTop(true).
BorderLeft(true).
Expand Down
58 changes: 49 additions & 9 deletions ui/standings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ui

import (
"strings"

"github.com/dylantientcheu/nbacli/nag/params"
"github.com/dylantientcheu/nbacli/nba"
"github.com/dylantientcheu/nbacli/ui/constants"

Expand All @@ -16,12 +19,13 @@ type StandingsModel struct {
easternConfTable table.Model
westernConfTable table.Model
help help.Model
selectedTab int
width, height, margin int
}

func (m *StandingsModel) recalculateTable() {
m.easternConfTable = m.easternConfTable.WithTargetWidth(m.width)
m.westernConfTable = m.westernConfTable.WithTargetWidth(m.width)
m.easternConfTable = m.easternConfTable.WithTargetWidth(m.width - constants.BleedSpaceWidth)
m.westernConfTable = m.westernConfTable.WithTargetWidth(m.width - constants.BleedSpaceWidth)
}

func (m StandingsModel) Init() tea.Cmd { return nil }
Expand All @@ -31,13 +35,22 @@ func (m StandingsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "tab":
m.selectedTab = (m.selectedTab + 1) % 2
if (m.selectedTab) == 0 {
m.easternConfTable = m.easternConfTable.Focused(true)
m.westernConfTable = m.westernConfTable.Focused(false)
} else {
m.easternConfTable = m.easternConfTable.Focused(false)
m.westernConfTable = m.westernConfTable.Focused(true)
}
case "q", "esc":
return m, tea.Quit
case "ctrl+c":
return m, tea.Quit
case "enter":
// TODO: to team view
return m, tea.Batch()
// return m, tea.Batch()
}
case tea.WindowSizeMsg:
m.width = msg.Width
Expand All @@ -59,17 +72,44 @@ func (m StandingsModel) View() string {
Up: key.NewBinding(key.WithKeys("up"), key.WithHelp("↑", "highlight previous row")),
Previous: key.NewBinding(key.WithKeys("esc", "q"), key.WithHelp("q/esc", "back to games list")),
}

helpContainer := lipgloss.NewStyle().
SetString(m.help.View(keyMap)).
Width(m.width).
Align(lipgloss.Center).
PaddingTop(1).
String()

easternConfText := lipgloss.NewStyle().AlignHorizontal(lipgloss.Center).Padding(1, 3).Background(lipgloss.AdaptiveColor{Light: "214", Dark: "#181818"}).Render("EASTERN CONFERENCE")
westernConfText := lipgloss.NewStyle().AlignHorizontal(lipgloss.Center).Padding(1, 3).Background(lipgloss.AdaptiveColor{Light: "214", Dark: "#181818"}).Render("WESTERN CONFERENCE")
tabGap := constants.TabStyle.Copy().
BorderTop(false).
BorderLeft(false).
BorderRight(false)

tabRow := lipgloss.JoinHorizontal(
lipgloss.Top,
constants.ActiveTabStyle.Render("EASTERN CONFERENCE"),
constants.TabStyle.Render("WESTERN CONFERENCE"),
)

renderedTable := easternTable

if m.selectedTab == 1 {
tabRow = lipgloss.JoinHorizontal(
lipgloss.Top,
constants.TabStyle.Render("EASTERN CONFERENCE"),
constants.ActiveTabStyle.Render("WESTERN CONFERENCE"),
)
renderedTable = westernTable
}

gap := tabGap.Render(strings.Repeat(" ", constants.Max(0, m.width-lipgloss.Width(tabRow))))

tabRow = lipgloss.JoinHorizontal(lipgloss.Bottom, tabRow, gap)

// title
title := constants.TitleStyle.Render("NBA Standings: " + params.CurrentSeason)

return easternConfText + "\n" + easternTable + "\n" + westernConfText + "\n" + westernTable + "\n" + helpContainer
return constants.DocStyle.Render(title + "\n\n" + tabRow + "\n" + renderedTable + "\n" + helpContainer)
}

func InitStandingsView() *StandingsModel {
Expand All @@ -89,10 +129,10 @@ func InitStandingsView() *StandingsModel {

easternRows, westernRows := newStandingsBoard(constants.St)

tEast := table.New(columns).WithRows(easternRows).Focused(true).Border(constants.CustomTableBorder).WithBaseStyle(constants.BaseStyle).WithPageSize(constants.WindowSize.Height / 3)
tWest := table.New(columns).WithRows(westernRows).Border(constants.CustomTableBorder).WithBaseStyle(constants.BaseStyle).WithPageSize(constants.WindowSize.Height / 3)
tEast := table.New(columns).WithRows(easternRows).Focused(true).Border(constants.CustomTableBorder).WithBaseStyle(constants.BaseStyle).WithPageSize(10)
tWest := table.New(columns).WithRows(westernRows).Border(constants.CustomTableBorder).WithBaseStyle(constants.BaseStyle).WithPageSize(10)

m := StandingsModel{tEast, tWest, help.New(), constants.WindowSize.Height, constants.WindowSize.Width, 3}
m := StandingsModel{tEast, tWest, help.New(), 0, constants.WindowSize.Height, constants.WindowSize.Width, 3}
return &m
}

Expand Down

0 comments on commit 9e62136

Please sign in to comment.