Skip to content

Commit

Permalink
feat: tap view
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 18, 2024
1 parent 25031a6 commit 086b13b
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 32 deletions.
10 changes: 8 additions & 2 deletions db/queries/tap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ SELECT *
FROM tap
WHERE category = ?;

-- name: GetLastOrder :one
-- name: GetLastOrderByOrderID :one
SELECT *
FROM tap
ORDER BY id DESC
ORDER BY order_id DESC
LIMIT 1;

-- name: GetOrderCount :many
SELECT category, COUNT(*)
FROM tap
GROUP BY category;

-- name: GetOrderCountByCategorySinceOrderID :many
SELECT category, COUNT(*)
FROM tap
WHERE order_id >= ?
GROUP BY category;
27 changes: 24 additions & 3 deletions internal/cmd/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@
package cmd

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/pkg/util"
tui "github.com/zeusWPI/scc/ui"
"github.com/zeusWPI/scc/ui/screen"
)

var screens = map[string]func(*db.DB) tea.Model{
"cammie": screen.NewCammie,
}

// TUI starts the terminal user interface
func TUI(db *db.DB) *tea.Program {
tui := tui.New(db)
func TUI(db *db.DB) (*tea.Program, error) {
args := os.Args
if len(args) < 2 {
return nil, fmt.Errorf("No screen specified. Options are %v", util.Keys(screens))
}

screen := args[1]

val, ok := screens[screen]
if !ok {
return nil, fmt.Errorf("Screen %s not found. Options are %v", screen, util.Keys(screens))
}

tui := tui.New(val(db))

program := tea.NewProgram(tui)

return program
return program, nil
}
43 changes: 39 additions & 4 deletions internal/pkg/db/sqlc/tap.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/pkg/tap/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New(db *db.DB) *Tap {
// Update gets all new orders from tap
func (t *Tap) Update() error {
// Get latest order
lastOrder, err := t.db.Queries.GetLastOrder(context.Background())
lastOrder, err := t.db.Queries.GetLastOrderByOrderID(context.Background())
if err != nil {
if err != sql.ErrNoRows {
return err
Expand Down
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ build: clean backend tui
run: backend tui
@./backend & ./tui

run-backend: backend
@./backend

run-tui: tui
@read -p "Enter screen name: " screen; \
./tui $$screen

backend:
@[ -f backend ] || (echo "Building backend..." && go build -o backend cmd/backend/backend.go)

Expand Down
7 changes: 4 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
package logger

import (
"fmt"
"os"

"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

// New returns a new logger instance
func New() (*zap.Logger, error) {
func New(logFile string) (*zap.Logger, error) {
// Create logs directory
err := os.Mkdir("logs", os.ModePerm)
if err != nil && !os.IsExist(err) {
Expand All @@ -24,8 +25,8 @@ func New() (*zap.Logger, error) {
} else {
zapConfig = zap.NewProductionConfig()
}
zapConfig.OutputPaths = []string{"logs/scc.log"}
zapConfig.ErrorOutputPaths = []string{"logs/scc.log"}
zapConfig.OutputPaths = []string{fmt.Sprintf("logs/%s.log", logFile)}
zapConfig.ErrorOutputPaths = []string{fmt.Sprintf("logs/%s.log", logFile)}

logger := zap.Must(zapConfig.Build())

Expand Down
10 changes: 10 additions & 0 deletions pkg/util/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

// Keys returns the keys of a map
func Keys[T comparable, U any](m map[T]U) []T {
keys := make([]T, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
32 changes: 32 additions & 0 deletions ui/screen/cammie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Package screen provides difference screens for the tui
package screen

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/zeusWPI/scc/internal/pkg/db"
)

// Cammie represents the cammie screen
type Cammie struct {
db *db.DB
}

// NewCammie creates a new cammie screen
func NewCammie(db *db.DB) tea.Model {
return &Cammie{db: db}
}

// Init initializes the cammie screen
func (c *Cammie) Init() tea.Cmd {
return nil
}

// Update updates the cammie screen
func (c *Cammie) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
return c, nil
}

// View returns the cammie screen view
func (c *Cammie) View() string {
return ""
}
32 changes: 13 additions & 19 deletions ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,33 @@ package tui

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/ui/views"
"go.uber.org/zap"
)

// TUI represent a terminal instance
type TUI struct {
db *db.DB
tap tea.Model
screen tea.Model
}

// New creates a new tty instance
func New(db *db.DB) *TUI {
return &TUI{
db: db,
tap: views.NewTapModel(db),
}
// New creates a new tui instance
func New(screen tea.Model) *TUI {
return &TUI{screen: screen}
}

// Init initializes the tty
// Init initializes the tui
func (t *TUI) Init() tea.Cmd {
return tea.Batch(tea.EnterAltScreen, t.tap.Init())
return tea.Batch(tea.EnterAltScreen, t.screen.Init())
}

// Update updates the tty
// Update updates the tui
func (t *TUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

tapModel, tapCmd := t.tap.Update(msg)
if tapCmd != nil {
cmds = append(cmds, tapCmd)
model, cmd := t.screen.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
t.tap = tapModel
t.screen = model

switch msg := msg.(type) {
case tea.KeyMsg:
Expand All @@ -50,7 +44,7 @@ func (t *TUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return t, tea.Batch(cmds...)
}

// View returns the tty view
// View returns the ttuity view
func (t *TUI) View() string {
return t.tap.View()
return t.screen.View()
}
Loading

0 comments on commit 086b13b

Please sign in to comment.