Skip to content

Commit

Permalink
feat: add cammie chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 21, 2024
1 parent e649034 commit 9aad0cf
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

echo "Backend linting"
echo "Linting"
golangci-lint run
if [ $? -ne 0 ]; then
echo "golangci-lint failed. Please fix the errors before committing."
Expand Down
16 changes: 16 additions & 0 deletions db/queries/message.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ RETURNING *;
-- name: DeleteMessage :execrows
DELETE FROM message
WHERE id = ?;


-- Other


-- name: GetLastMessage :one
SELECT *
FROM message
ORDER BY id DESC
LIMIT 1;

-- name: GetMessageSinceID :many
SELECT *
FROM message
WHERE id > ?
ORDER BY created_at ASC;
2 changes: 2 additions & 0 deletions db/queries/tap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ RETURNING *;
DELETE FROM tap
WHERE id = ?;


-- Other


-- name: GetTapByOrderID :one
SELECT *
FROM tap
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TUI(db *db.DB) (*tea.Program, error) {

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

program := tea.NewProgram(tui)
program := tea.NewProgram(tui, tea.WithAltScreen())

return program, nil
}
59 changes: 59 additions & 0 deletions internal/pkg/db/sqlc/message.sql.go

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

1 change: 1 addition & 0 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.

17 changes: 11 additions & 6 deletions ui/screen/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ package screen
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/ui/view"
)

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

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

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

// Update updates the cammie screen
func (c *Cammie) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
return c, nil
func (c *Cammie) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cammie, cmd := c.cammie.Update(msg)
c.cammie = cammie.(*view.MessageModel)

return c, cmd
}

// View returns the cammie screen view
func (c *Cammie) View() string {
return ""
return c.cammie.View()
}
2 changes: 1 addition & 1 deletion ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New(screen tea.Model) *TUI {

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

// Update updates the tui
Expand Down
109 changes: 109 additions & 0 deletions ui/view/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package view

import (
"context"
"fmt"
"hash/fnv"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/list"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/db/sqlc"
"go.uber.org/zap"
)

// MessageModel represents the model for the message view
type MessageModel struct {
db *db.DB
lastMessageID int64
messages []string
}

// MessageMsg represents the message to update the message view
type MessageMsg struct {
lastMessageID int64
messages []string
}

var messageColor = []string{
"#800000", "#008000", "#808000", "#000080", "#800080", "#008080", "#c0c0c0",
"#ff0000", "#00ff00", "#ffff00", "#0000ff", "#ff00ff", "#00ffff", "#ffffff",
}

// NewMessageModel creates a new message model view
func NewMessageModel(db *db.DB) *MessageModel {
return &MessageModel{db: db, lastMessageID: -1, messages: []string{}}
}

// Init initializes the message model view
func (c *MessageModel) Init() tea.Cmd {
return updateMessages(c.db, c.lastMessageID)
}

// Update updates the message model view
func (c *MessageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case MessageMsg:
c.lastMessageID = msg.lastMessageID
c.messages = append(c.messages, msg.messages...)

return c, updateMessages(c.db, c.lastMessageID)
}

return c, nil
}

// View returns the view for the message model
func (c *MessageModel) View() string {
l := list.New(c.messages).Enumerator(func(_ list.Items, _ int) string { return "" })
return l.String()
}

func updateMessages(db *db.DB, lastMessageID int64) tea.Cmd {
return tea.Tick(1*time.Second, func(_ time.Time) tea.Msg {
message, err := db.Queries.GetLastMessage(context.Background())
if err != nil {
zap.S().Error("DB: Failed to get last message", err)
return MessageMsg{lastMessageID: lastMessageID, messages: []string{}}
}

if message.ID <= lastMessageID {
return MessageMsg{lastMessageID: lastMessageID, messages: []string{}}
}

messages, err := db.Queries.GetMessageSinceID(context.Background(), lastMessageID)
if err != nil {
zap.S().Error("DB: Failed to get messages", err)
return MessageMsg{lastMessageID: lastMessageID, messages: []string{}}
}

formattedMessages := make([]string, 0, len(messages))
for _, message := range messages {
formattedMessages = append(formattedMessages, formatMessage(message))
}

return MessageMsg{lastMessageID: message.ID, messages: formattedMessages}
})
}

func hashColor(s string) string {
h := fnv.New32a()
h.Write([]byte(s))
hash := h.Sum32()
return messageColor[hash%uint32(len(messageColor))]
}

func formatMessage(msg sqlc.Message) string {
dateStyle := lipgloss.NewStyle().Faint(true)
date := dateStyle.Render(fmt.Sprintf("%s | ", msg.CreatedAt.Format("02/01")))

color := hashColor(msg.Name)
colorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(color))

sender := fmt.Sprintf("%s %s %s ", colorStyle.Render("["), colorStyle.Bold(true).Render(msg.Name), colorStyle.Render("]"))
message := colorStyle.Render(msg.Message)

return fmt.Sprintf("%s%s%s", date, sender, message)
}
14 changes: 7 additions & 7 deletions ui/view/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type TapModel struct {
food float64
}

// TapMessage represents a tap message
type TapMessage struct {
// TapMsg represents a tap message
type TapMsg struct {
lastOrderID int64
items []tapItem
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (t *TapModel) Init() tea.Cmd {
// Update updates the tap model
func (t *TapModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case TapMessage:
case TapMsg:
t.lastOrderID = msg.lastOrderID

for _, msg := range msg.items {
Expand Down Expand Up @@ -123,17 +123,17 @@ func updateOrders(db *db.DB, lastOrderID int64) tea.Cmd {
order, err := db.Queries.GetLastOrderByOrderID(context.Background())
if err != nil {
zap.S().Error("DB: Failed to get last order", err)
return TapMessage{lastOrderID: lastOrderID, items: nil}
return TapMsg{lastOrderID: lastOrderID, items: nil}
}

if order.OrderID <= lastOrderID {
return TapMessage{lastOrderID: lastOrderID, items: nil}
return TapMsg{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 TapMessage{lastOrderID: lastOrderID, items: nil}
return TapMsg{lastOrderID: lastOrderID, items: nil}
}

mate, soft, beer, food := 0.0, 0.0, 0.0, 0.0
Expand Down Expand Up @@ -164,6 +164,6 @@ func updateOrders(db *db.DB, lastOrderID int64) tea.Cmd {
messages = append(messages, tapItem{"Food", food})
}

return TapMessage{lastOrderID: order.OrderID, items: messages}
return TapMsg{lastOrderID: order.OrderID, items: messages}
})
}

0 comments on commit 9aad0cf

Please sign in to comment.