Files
terminalShop-old/REFACTORING.md

3.7 KiB

Refactoring Complete!

The codebase has been refactored from a single model.go file (~650 lines) into a clean, modular structure.

New Structure

terminalShop/
├── main.go                    # Entry point (~20 lines)
├── pkg/
│   ├── models/
│   │   └── coffee.go         # Data models (~25 lines)
│   └── tui/
│       ├── model.go          # Core model & state (~160 lines)
│       ├── views.go          # Main View() function (~60 lines)
│       ├── shop.go           # Shop view (~90 lines)
│       ├── cart.go           # Cart view (~90 lines)
│       ├── account.go        # Account view (~75 lines)
│       ├── header.go         # Header rendering (~95 lines)
│       └── footer.go         # Footer rendering (~75 lines)
├── model.go.old              # Backup of old file
└── server.go.old             # Backup of old file

What Changed

Before

  • 1 file: model.go with 650+ lines
  • All code mixed together
  • Hard to navigate and maintain
  • Difficult to find specific features

After

  • 8 focused files averaging ~70 lines each
  • Clear separation of concerns
  • Easy to find and modify features
  • Follows Go best practices

File Responsibilities

File Purpose Key Functions
main.go App entry point main()
pkg/models/coffee.go Data structures Coffee, CartItem, AccountMenuItems
pkg/tui/model.go State management Model, Init(), Update(), NewModel()
pkg/tui/views.go Layout assembly View()
pkg/tui/shop.go Shop view BuildShopView()
pkg/tui/cart.go Cart view BuildCartView()
pkg/tui/account.go Account views BuildAccountView(), buildAccountSubpage()
pkg/tui/header.go Header/tabs BuildHeader()
pkg/tui/footer.go Footer/keybinds BuildFooter()

Benefits

  1. Easier to Navigate: Find shop code in shop.go, cart code in cart.go, etc.
  2. Easier to Test: Each component can be tested independently
  3. Easier to Extend: Add new views by creating new files
  4. Better Organization: Related code stays together
  5. Cleaner Git Diffs: Changes to cart don't affect shop code
  6. Follows Standards: Matches Go project conventions

Running the App

Same as before:

# Run the app
go run main.go

# Or build and run
go build -o terminalshop main.go
./terminalshop

Adding New Features

Adding a New View (e.g., "Settings")

  1. Create pkg/tui/settings.go:
package tui

func (m Model) BuildSettingsView() string {
    // Your settings view code
}
  1. Update model.go:
  • Add viewingSettings bool to Model struct
  • Add keybind case in Update()
  1. Update views.go:
  • Add condition for settings view
  1. Update header.go and footer.go:
  • Add settings tab/keybinds

Adding New Data Models

Add to pkg/models/ directory:

// pkg/models/order.go
package models

type Order struct {
    ID string
    Items []CartItem
    Total float64
}

Migration Notes

  • Old files backed up as .old
  • All functionality preserved
  • No behavior changes
  • Imports use terminalShop/pkg/...

Next Steps

Now that the code is organized, you can easily:

  • Add new views (checkout, payment, orders)
  • Implement API client in pkg/api/
  • Add database models in pkg/models/
  • Create tests for each component
  • Continue with Phase 2 of the roadmap (Backend API)

Rollback

If needed, restore the old structure:

mv model.go.old model.go
mv server.go.old server.go
rm -rf pkg/
# Edit main.go to use InitialModel again

Refactored on: 2026-02-12 Lines of code: Same functionality, better organized!