From 25031a6825265c4430069998d222d5f8647a99a9 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 18 Nov 2024 17:00:29 +0100 Subject: [PATCH] feat: add tui template --- .gitignore | 3 +- cmd/tty/scc.go | 8 ----- db/queries/tap.sql | 5 +++ go.mod | 18 ++++++++++- go.sum | 39 +++++++++++++++++++++++ internal/cmd/api.go | 3 +- internal/cmd/root.go | 52 ------------------------------ internal/cmd/spotify.go | 13 ++++++++ internal/cmd/tap.go | 7 +++-- internal/cmd/tui.go | 17 ++++++++++ internal/pkg/db/sqlc/tap.sql.go | 34 ++++++++++++++++++++ makefile | 15 ++++++--- ui/tui.go | 56 +++++++++++++++++++++++++++++++++ 13 files changed, 200 insertions(+), 70 deletions(-) delete mode 100644 cmd/tty/scc.go delete mode 100644 internal/cmd/root.go create mode 100644 internal/cmd/spotify.go create mode 100644 internal/cmd/tui.go create mode 100644 ui/tui.go diff --git a/.gitignore b/.gitignore index 0c6ad57..52db340 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,8 @@ tmp/ !.env.example # Project build -scc +backend +tui .data/ /public/ diff --git a/cmd/tty/scc.go b/cmd/tty/scc.go deleted file mode 100644 index 38b1424..0000000 --- a/cmd/tty/scc.go +++ /dev/null @@ -1,8 +0,0 @@ -// Main entry point for the scc application -package main - -import "github.com/zeusWPI/scc/internal/cmd" - -func main() { - cmd.Execute() -} diff --git a/db/queries/tap.sql b/db/queries/tap.sql index 3d2e123..6d78deb 100644 --- a/db/queries/tap.sql +++ b/db/queries/tap.sql @@ -41,3 +41,8 @@ SELECT * FROM tap ORDER BY id DESC LIMIT 1; + +-- name: GetOrderCount :many +SELECT category, COUNT(*) +FROM tap +GROUP BY category; diff --git a/go.mod b/go.mod index 738cd96..96f85c1 100644 --- a/go.mod +++ b/go.mod @@ -9,25 +9,41 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/bubbles v0.18.0 // indirect + github.com/charmbracelet/bubbletea v0.25.0 // indirect + github.com/charmbracelet/lipgloss v1.0.0 // indirect + github.com/charmbracelet/x/ansi v0.4.2 // indirect + github.com/containerd/console v1.0.4 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/google/uuid v1.5.0 // indirect github.com/klauspost/compress v1.17.2 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/rivo/uniseg v0.4.3 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/net v0.23.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/term v0.24.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) require ( + github.com/NimbleMarkets/ntcharts v0.1.2 github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-playground/validator/v10 v10.22.1 github.com/gofiber/contrib/fiberzap v1.0.2 diff --git a/go.sum b/go.sum index 6863115..a8270f5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,21 @@ +github.com/NimbleMarkets/ntcharts v0.1.2 h1:iW1aiOif/Dm74sQd18opi10RMED5589cVhy9SGp98Tw= +github.com/NimbleMarkets/ntcharts v0.1.2/go.mod h1:WcHS7kc8oQctN1543DeV9a+gOrS4DDVfKp1N9RZFUqc= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= +github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= +github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM= +github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg= +github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= +github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= +github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg= +github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo= +github.com/charmbracelet/x/ansi v0.4.2 h1:0JM6Aj/g/KC154/gOP4vfxun0ff6itogDYk41kof+qk= +github.com/charmbracelet/x/ansi v0.4.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro= +github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -41,6 +57,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195 h1:zcxmFnwisGZSaEzgvkOrs4belfcRlKyIUfa3sOQSttQ= +github.com/lrstanley/bubblezone v0.0.0-20240125042004-b7bafc493195/go.mod h1:v5lEwWaguF1o2MW/ucO0ZIA/IZymdBYJJ+2cMRLE7LU= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -48,20 +68,34 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= @@ -107,10 +141,15 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= +golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/cmd/api.go b/internal/cmd/api.go index 0e76d3d..9a9e508 100644 --- a/internal/cmd/api.go +++ b/internal/cmd/api.go @@ -13,7 +13,8 @@ import ( "go.uber.org/zap" ) -func apiCmd(db *db.DB, spotify *spotify.Spotify) { +// API starts the API server +func API(db *db.DB, spotify *spotify.Spotify) { app := fiber.New(fiber.Config{ BodyLimit: 1024 * 1024 * 1024, }) diff --git a/internal/cmd/root.go b/internal/cmd/root.go deleted file mode 100644 index f7c0368..0000000 --- a/internal/cmd/root.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package cmd package contains all the commands to start the application -package cmd - -import ( - "github.com/zeusWPI/scc/internal/pkg/db" - "github.com/zeusWPI/scc/internal/pkg/spotify" - "github.com/zeusWPI/scc/internal/pkg/tap" - "github.com/zeusWPI/scc/pkg/config" - "github.com/zeusWPI/scc/pkg/logger" - "go.uber.org/zap" -) - -// Execute starts the entire application -func Execute() { - zap.S().Info("Initializing application") - - // Config - err := config.Init() - if err != nil { - panic(err) - } - - // Logger - zapLogger, err := logger.New() - if err != nil { - panic(err) - } - zap.ReplaceGlobals(zapLogger) - - // Database - db, err := db.New() - if err != nil { - zap.S().Fatal("DB: Fatal error\n", err) - } - - // Spotify - spotify, err := spotify.New(db) - if err != nil { - zap.S().Error("Spotify: Initiating error, integration will not work.\n", err) - } - - // Tap - tap := tap.New(db) - - // Everything that needs to be initialized is done - // Time to start all parts of the application - zap.S().Info("Starting application") - - _ = tapCmd(tap) - - apiCmd(db, spotify) -} diff --git a/internal/cmd/spotify.go b/internal/cmd/spotify.go new file mode 100644 index 0000000..64e4072 --- /dev/null +++ b/internal/cmd/spotify.go @@ -0,0 +1,13 @@ +package cmd + +import ( + "github.com/zeusWPI/scc/internal/pkg/db" + "github.com/zeusWPI/scc/internal/pkg/spotify" +) + +// Spotify starts the Spotify integration +func Spotify(db *db.DB) (*spotify.Spotify, error) { + spotify, err := spotify.New(db) + + return spotify, err +} diff --git a/internal/cmd/tap.go b/internal/cmd/tap.go index 87057a0..867434f 100644 --- a/internal/cmd/tap.go +++ b/internal/cmd/tap.go @@ -3,17 +3,20 @@ package cmd import ( "time" + "github.com/zeusWPI/scc/internal/pkg/db" "github.com/zeusWPI/scc/internal/pkg/tap" "github.com/zeusWPI/scc/pkg/config" "go.uber.org/zap" ) -func tapCmd(tap *tap.Tap) chan bool { +// Tap starts the tap +func Tap(db *db.DB) (*tap.Tap, chan bool) { + tap := tap.New(db) done := make(chan bool) go tapPeriodicUpdate(tap, done) - return done + return tap, done } func tapPeriodicUpdate(tap *tap.Tap, done chan bool) { diff --git a/internal/cmd/tui.go b/internal/cmd/tui.go new file mode 100644 index 0000000..73400e0 --- /dev/null +++ b/internal/cmd/tui.go @@ -0,0 +1,17 @@ +// Package cmd provides all the commands to start parts of the application +package cmd + +import ( + tea "github.com/charmbracelet/bubbletea" + "github.com/zeusWPI/scc/internal/pkg/db" + tui "github.com/zeusWPI/scc/ui" +) + +// TUI starts the terminal user interface +func TUI(db *db.DB) *tea.Program { + tui := tui.New(db) + + program := tea.NewProgram(tui) + + return program +} diff --git a/internal/pkg/db/sqlc/tap.sql.go b/internal/pkg/db/sqlc/tap.sql.go index 209c882..5096e03 100644 --- a/internal/pkg/db/sqlc/tap.sql.go +++ b/internal/pkg/db/sqlc/tap.sql.go @@ -113,6 +113,40 @@ func (q *Queries) GetLastOrder(ctx context.Context) (Tap, error) { return i, err } +const getOrderCount = `-- name: GetOrderCount :many +SELECT category, COUNT(*) +FROM tap +GROUP BY category +` + +type GetOrderCountRow struct { + Category string + Count int64 +} + +func (q *Queries) GetOrderCount(ctx context.Context) ([]GetOrderCountRow, error) { + rows, err := q.db.QueryContext(ctx, getOrderCount) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetOrderCountRow + for rows.Next() { + var i GetOrderCountRow + if err := rows.Scan(&i.Category, &i.Count); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getTapByCategory = `-- name: GetTapByCategory :many SELECT id, order_id, order_created_at, name, category, created_at FROM tap diff --git a/makefile b/makefile index 42ba382..a70118f 100644 --- a/makefile +++ b/makefile @@ -1,13 +1,18 @@ all: build -build: clean - @go build -o scc cmd/tty/scc.go +build: clean backend tui -run: - @./scc +run: backend tui + @./backend & ./tui + +backend: + @[ -f backend ] || (echo "Building backend..." && go build -o backend cmd/backend/backend.go) + +tui: + @[ -f tui ] || (echo "Building tui..." && go build -o tui cmd/tui/tui.go) clean: - @rm -f scc + @rm -f backend tui sqlc: sqlc generate diff --git a/ui/tui.go b/ui/tui.go new file mode 100644 index 0000000..664794a --- /dev/null +++ b/ui/tui.go @@ -0,0 +1,56 @@ +// Package tui provides utilities for working with the terminal. +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 +} + +// New creates a new tty instance +func New(db *db.DB) *TUI { + return &TUI{ + db: db, + tap: views.NewTapModel(db), + } +} + +// Init initializes the tty +func (t *TUI) Init() tea.Cmd { + return tea.Batch(tea.EnterAltScreen, t.tap.Init()) +} + +// Update updates the tty +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) + } + t.tap = tapModel + + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC: + zap.S().Info("Exiting") + cmds = append(cmds, tea.ExitAltScreen) + cmds = append(cmds, tea.Quit) + } + } + + return t, tea.Batch(cmds...) +} + +// View returns the tty view +func (t *TUI) View() string { + return t.tap.View() +}