Skip to content

Commit

Permalink
include some help
Browse files Browse the repository at this point in the history
  • Loading branch information
bluntelk committed Sep 29, 2023
1 parent 9bf12b0 commit deec455
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/ingest_tap/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/viewport"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -36,6 +37,8 @@ type (

focusIcaoList []string

help help.Model

statsTable table.Model
selectedTable table.Model
planesTable table.Model
Expand Down Expand Up @@ -81,6 +84,7 @@ func initialModel(natsURL, wsURL string) (*model, error) {
logger: logger.With().Str("app", "model").Logger(),
startTime: time.Now(),
tapper: NewPlaneWatchTapper(WithLogger(logger)),
help: help.New(),
tickDuration: time.Millisecond * 16,
focusIcaoList: make([]string, 0),
source: planesSourceWSLow,
Expand Down
47 changes: 43 additions & 4 deletions cmd/ingest_tap/tui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -22,6 +23,22 @@ const (
planesSourceWSHigh
)

type keyMap map[string]key.Binding

var keyBindings = keyMap{
"Up": key.NewBinding(key.WithKeys("up"), key.WithHelp("↑", "Move up in the aircraft list")),
"Down": key.NewBinding(key.WithKeys("up"), key.WithHelp("↓", "Move up in the aircraft list")),
"PageUp": key.NewBinding(key.WithKeys("up"), key.WithHelp("PgUp", "Move a page up in the aircraft list")),
"PageDown": key.NewBinding(key.WithKeys("up"), key.WithHelp("PgDn", "Move a page down in the aircraft list")),

"Source": key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "Switch Plane List Data Source")),
"Select": key.NewBinding(key.WithKeys(tea.KeyEnter.String()), key.WithHelp(tea.KeyEnter.String(), "Select a plane")),

"Quit": key.NewBinding(key.WithKeys("q", "ctrl+c"), key.WithHelp("q/ctrl+c", "Exit")),

"Help": key.NewBinding(key.WithKeys("h", "?"), key.WithHelp("h/?", "Show Help")),
}

func runTui(c *cli.Context) error {
m, err := initialModel(c.String(natsURL), c.String(websocketURL))
if err != nil {
Expand Down Expand Up @@ -67,18 +84,24 @@ func (m *model) Init() tea.Cmd {
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch tMsg := msg.(type) {
case tea.KeyMsg:
switch tMsg.String() {
case "ctrl+c", "q":
switch {
case key.Matches(tMsg, keyBindings["Quit"]):
return m, tea.Quit
case "s":
case key.Matches(tMsg, keyBindings["Source"]):
if m.source == planesSourceWSHigh {
m.source = planesSourceIngest
} else {
m.source++
}
case tea.KeyEnter.String():
case key.Matches(tMsg, keyBindings["Select"]):
m.selectedIcao = m.planesTable.SelectedRow()[0]
m.selectedCallSign = m.planesTable.SelectedRow()[2]
m.logger.Info().
Str("icao", m.selectedIcao).
Str("callsign", m.selectedCallSign).
Msg("Selecting Aircraft")
case key.Matches(tMsg, keyBindings["Help"]):
m.help.ShowAll = !m.help.ShowAll
}
case tea.WindowSizeMsg:
m.width = tMsg.Width
Expand Down Expand Up @@ -106,6 +129,7 @@ func (m *model) handleWindowSizing() {
m.statsTable.SetWidth(m.width)
m.selectedTable.SetWidth(m.width)
m.planesTable.SetWidth(m.width)
m.help.Width = m.width

headingHeight := lipgloss.Height(m.heading.Render("test"))
statsTableHeight := 3
Expand Down Expand Up @@ -208,6 +232,7 @@ func (m *model) selectedTableRow(source planesSource, data *sourceInfo) table.Ro
loc.LonStr(),
loc.AltitudeStr(),
loc.VerticalRateStr(),
loc.HeadingStr(),
}
}

Expand Down Expand Up @@ -276,6 +301,8 @@ func (m *model) View() string {
view += m.heading.Render("Logs") + "\n"
view += m.logView.View() + "\n"
}

view += m.help.View(keyBindings)
return view
}

Expand All @@ -284,3 +311,15 @@ func (m *model) tickCmd() tea.Cmd {
return timerTick(t)
})
}

func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k["Help"], k["Quit"]}
}

func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k["Up"], k["Down"], k["PgUp"], k["PgDn"]},
{k["Source"], k["Select"]},
{k["Help"], k["Quit"]},
}
}
3 changes: 3 additions & 0 deletions lib/export/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ func (pl *PlaneLocation) SquawkStr() string {
if nil == pl {
return ""
}
if pl.Squawk == "0" {
return ""
}
return pl.Squawk
}

Expand Down

0 comments on commit deec455

Please sign in to comment.