-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
- Season schedule | ||
- Race results | ||
- Qualifying results | ||
- Driver results | ||
|
||
## Install | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,11 @@ Sleep 500ms | |
Enter | ||
Sleep 3s | ||
|
||
Down@500ms 4 | ||
Sleep 1.5s | ||
Enter | ||
Sleep 5s | ||
|
||
Type "c" | ||
Sleep 3s | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package driver | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/acifani/formula1-go/internal/ui" | ||
"github.com/acifani/formula1-go/internal/ui/page" | ||
"github.com/acifani/formula1-go/pkg/api" | ||
) | ||
|
||
type model struct { | ||
data *api.RaceTable | ||
table table.Model | ||
styles ui.Styles | ||
err error | ||
} | ||
|
||
type LoadDone struct { | ||
err error | ||
data *api.RaceTable | ||
} | ||
|
||
type BackMsg struct{} | ||
|
||
func New(styles ui.Styles) page.Model { | ||
columns := []table.Column{ | ||
{Title: "#", Width: 4}, | ||
{Title: "Race", Width: 25}, | ||
{Title: "Pos", Width: 4}, | ||
{Title: "Pts", Width: 4}, | ||
{Title: "Status", Width: 15}, | ||
} | ||
t := table.New(table.WithColumns(columns)) | ||
t.SetStyles(styles.Table) | ||
|
||
return &model{table: t, styles: styles} | ||
} | ||
|
||
func (m model) GetPageTitle() string { | ||
if m.data != nil { | ||
driver := m.data.Races[0].Results[0].Driver | ||
team := m.data.Races[0].Results[0].Constructor | ||
return driver.PermanentNumber + " " + driver.GivenName + " " + driver.FamilyName + " - " + team.Name | ||
} | ||
return "" | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (page.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "esc": | ||
return m, backMsg | ||
} | ||
case LoadDone: | ||
if msg.err != nil { | ||
m.err = msg.err | ||
} else { | ||
m.data = msg.data | ||
rows := generateRows(msg.data) | ||
m.table.SetHeight(len(rows)) | ||
m.table.SetRows(rows) | ||
} | ||
} | ||
|
||
m.table, cmd = m.table.Update(msg) | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
if m.err != nil { | ||
return m.styles.Paragraph.Render(m.err.Error()) | ||
} | ||
|
||
return m.table.View() | ||
} | ||
|
||
func LoadResults(year, driverID string) tea.Cmd { | ||
return func() tea.Msg { | ||
results, err := api.GetDriverRaceResults(year, driverID) | ||
return LoadDone{data: results, err: err} | ||
} | ||
} | ||
|
||
func generateRows(data *api.RaceTable) []table.Row { | ||
rows := make([]table.Row, len(data.Races)) | ||
for i, race := range data.Races { | ||
rows[i] = table.Row{ | ||
race.Round, | ||
race.RaceName, | ||
race.Results[0].PositionText, | ||
race.Results[0].Points, | ||
race.Results[0].Status, | ||
} | ||
} | ||
return rows | ||
} | ||
|
||
func backMsg() tea.Msg { | ||
return BackMsg{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters