diff --git a/makefile b/makefile index 671e929..bb70fdb 100644 --- a/makefile +++ b/makefile @@ -1,40 +1,64 @@ +# Variables +BACKEND_BIN := backend +TUI_BIN := tui +BACKEND_SRC := cmd/backend/backend.go +TUI_SRC := cmd/tui/tui.go +DB_DIR := ./db/migrations +DB_FILE := ./sqlite.db + +# Phony targets +.PHONY: all build clean run run-backend run-tui sqlc create-migration goose migrate watch + +# Default target: build everything all: build -build: clean backend tui +# Build targets +build: clean build-backend build-tui + +build-backend: + @echo "Building $(BACKEND_BIN)..." + @rm -f $(BACKEND_BIN) + @go build -o $(BACKEND_BIN) $(BACKEND_SRC) -run: backend tui - @./backend & ./tui +build-tui: + @echo "Building $(TUI_BIN)..." + @rm -f $(TUI_BIN) + @go build -o $(TUI_BIN) $(TUI_SRC) -run-backend: backend - @./backend +# Run targets +run: run-backend run-tui -run-tui: tui +run-backend: + @[ -f $(BACKEND_BIN) ] || $(MAKE) build-backend + @./$(BACKEND_BIN) + +run-tui: + @[ -f $(TUI_BIN) ] || $(MAKE) build-tui @read -p "Enter screen name: " screen; \ - ./tui $$screen + ./$(TUI_BIN) $$screen -backend: - @[ -f backend ] || (echo "Building backend..." && go build -o backend cmd/backend/backend.go) +# Clean targets +clean: clean-backend clean-tui -tui: - @[ -f tui ] || (echo "Building tui..." && go build -o tui cmd/tui/tui.go) +clean-backend: + @echo "Cleaning $(BACKEND_BIN)..." + @rm -f $(BACKEND_BIN) -clean: - @rm -f backend tui +clean-tui: + @echo "Cleaning $(TUI_BIN)..." + @rm -f $(TUI_BIN) +# SQL and migration targets sqlc: sqlc generate create-migration: @read -p "Enter migration name: " name; \ - goose -dir ./db/migrations create $$name sql + goose -dir $(DB_DIR) create $$name sql goose: @read -p "Action: " action; \ - goose -dir ./db/migrations sqlite3 ./sqlite.db $$action + goose -dir $(DB_DIR) sqlite3 $(DB_FILE) $$action migrate: - @goose -dir ./db/migrations sqlite3 ./sqlite.db up - -watch: - @echo "Starting the watch script..." - ./watch.sh + @goose -dir $(DB_DIR) sqlite3 $(DB_FILE) up diff --git a/ui/view/tap.go b/ui/view/tap.go index f5aac2f..e654030 100644 --- a/ui/view/tap.go +++ b/ui/view/tap.go @@ -52,22 +52,27 @@ func (t *TapModel) Init() tea.Cmd { // Update updates the tap model func (t *TapModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - t.lastOrderID = msg.(TapMessage).lastOrderID - - for _, msg := range msg.(TapMessage).items { - switch msg.category { - case "Mate": - t.mate += msg.amount - case "Soft": - t.soft += msg.amount - case "Beer": - t.beer += msg.amount - case "Food": - t.food += msg.amount + switch msg := msg.(type) { + case TapMessage: + t.lastOrderID = msg.lastOrderID + + for _, msg := range msg.items { + switch msg.category { + case "Mate": + t.mate += msg.amount + case "Soft": + t.soft += msg.amount + case "Beer": + t.beer += msg.amount + case "Food": + t.food += msg.amount + } } + + return t, updateOrders(t.db, t.lastOrderID) } - return t, updateOrders(t.db, t.lastOrderID) + return t, nil } // View returns the tap view @@ -85,7 +90,7 @@ func (t *TapModel) View() string { barSoft := barchart.BarData{ Label: "Soft", Values: []barchart.BarValue{{ - Name: "Item1", + Name: "Soft", Value: t.soft, Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Soft"]), }}, @@ -93,7 +98,7 @@ func (t *TapModel) View() string { barBeer := barchart.BarData{ Label: "Beer", Values: []barchart.BarValue{{ - Name: "Item1", + Name: "Beer", Value: t.beer, Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Beer"]), }}, @@ -101,7 +106,7 @@ func (t *TapModel) View() string { barFood := barchart.BarData{ Label: "Food", Values: []barchart.BarValue{{ - Name: "Item1", + Name: "Food", Value: t.food, Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Food"]), }}, @@ -114,21 +119,21 @@ func (t *TapModel) View() string { } func updateOrders(db *db.DB, lastOrderID int64) tea.Cmd { - return tea.Tick(time.Second, func(_ time.Time) tea.Msg { + return tea.Tick(60*time.Second, func(_ time.Time) tea.Msg { order, err := db.Queries.GetLastOrderByOrderID(context.Background()) if err != nil { zap.S().Error("DB: Failed to get last order", err) - return nil + return TapMessage{lastOrderID: lastOrderID, items: nil} } if order.OrderID <= lastOrderID { - return nil + return TapMessage{lastOrderID: lastOrderID, items: nil} } orders, err := db.Queries.GetOrderCountByCategorySinceOrderID(context.Background(), lastOrderID) if err != nil { zap.S().Error("DB: Failed to get tap orders", err) - return nil + return TapMessage{lastOrderID: lastOrderID, items: nil} } mate, soft, beer, food := 0.0, 0.0, 0.0, 0.0 diff --git a/watch.sh b/watch.sh deleted file mode 100755 index 71f9720..0000000 --- a/watch.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - - -# Directories to exclude -EXCLUDE_DIRS=(".githooks" ".github" "logs") - -# Build the exclude patterns for inotifywait -EXCLUDE_PATTERN=$(printf "|%s" "${EXCLUDE_DIRS[@]}") -EXCLUDE_PATTERN=${EXCLUDE_PATTERN:1} # Remove leading | - -# Kill background jobs on exit -cleanup() { - if [[ -n $RUN_PID ]]; then - kill $RUN_PID - wait $RUN_PID 2>/dev/null - fi - exit -} -trap cleanup SIGINT SIGTERM - -# Function to restart the program -restart_program() { - echo "Change detected. Restarting program..." - # Stop the running program - if [[ -n $RUN_PID ]]; then - kill $RUN_PID - wait $RUN_PID 2>/dev/null - fi - - # Rebuild and restart - make build || { echo "Build failed"; return; } - make run & - RUN_PID=$! -} - -# Start the program initially -restart_program - -# Watch for file changes, ignoring excluded directories -while true; do - CHANGED_FILE=$(inotifywait -re modify --exclude "(${EXCLUDE_PATTERN})" . 2>/dev/null) - if [[ $? -eq 0 ]]; then - restart_program - fi -done