Skip to content

Commit

Permalink
Fixing import
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Feb 3, 2024
1 parent c477966 commit 9fb3589
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ linters-settings:
- "github.com/drewstinnett/letseat/pkg"
- "github.com/drewstinnett/letseat/cmd/letseat/cmd"
- "github.com/drewstinnett/go-output-format/v2/gout"

- "github.com/charmbracelet/bubbletea"
- "github.com/charmbracelet/bubbles/progress"
tests:
files:
- "$test"
Expand Down
108 changes: 101 additions & 7 deletions cmd/letseat/cmd/import.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cmd

import (
"fmt"
"log/slog"
"os"
"strings"

"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
letseat "github.com/drewstinnett/letseat/pkg"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand All @@ -27,14 +32,103 @@ func runImport(cmd *cobra.Command, args []string) error {
return err
}
var entries letseat.Entries
if err := yaml.Unmarshal(eb, &entries); err != nil {
return err
if yerr := yaml.Unmarshal(eb, &entries); yerr != nil {
return yerr
}
for _, entry := range entries {
entry := entry
if err := diary.Log(entry); err != nil {
return err
}
zero := 0
pb := pbar{
progress: progress.New(progress.WithDefaultGradient()),
diary: diary,
entries: entries,
current: &zero,
}
_, rerr := tea.NewProgram(pb).Run()
if rerr != nil {
slog.Error("error running progressbar", "error", rerr)
}
/*
for _, entry := range entries {
entry := entry
if err := diary.Log(entry); err != nil {
return err
}
}
*/
return nil
}

type pbar struct {
progress progress.Model
diary *letseat.Diary
entries letseat.Entries
percent float64
current *int
}

// Init satisfies the bubble interface
func (p pbar) Init() tea.Cmd {
return p.log()
}

// View shows the current bar
func (p pbar) View() string {
pad := strings.Repeat(" ", padding)
return "\n" +
pad + p.progress.View() + "\n\n" +
// pad + fmt.Sprintf("%.2f percent complete", p.percent*100) + "\n\n" +
pad + fmt.Sprintf("Importing %v of %v", *p.current-1, len(p.entries)) + "\n\n" +
pad + helpStyle("Press any key to quit")
}

func (p pbar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
return p, tea.Quit

case tea.WindowSizeMsg:
p.progress.Width = msg.Width - padding*2 - 4
if p.progress.Width > maxWidth {
p.progress.Width = maxWidth
}
return p, nil

case logged:
if p.progress.Percent() == 1.0 {
return p, tea.Quit
}

// p.state = msg
p.percent = float64(*p.current) / float64(len(p.entries))
cmd := p.progress.SetPercent(p.percent)

return p, tea.Batch(p.log(), cmd)
// return p, cmd

// FrameMsg is sent when the progress bar wants to animate itself
case progress.FrameMsg:
progressModel, cmd := p.progress.Update(msg)
p.progress = progressModel.(progress.Model)
return p, cmd

default:
return p, nil
}
}

type logged bool

func (p *pbar) log() tea.Cmd {
return func() tea.Msg {
var res bool
var c int
if p == nil {
return logged(false)
}
c = min(*p.current, len(p.entries)-1)
if err := p.diary.Log(p.entries[c]); err != nil {
slog.Error("error logging entriy")
}
*p.current++
return logged(res)
}
}
15 changes: 15 additions & 0 deletions cmd/letseat/cmd/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"path"
"testing"

"github.com/stretchr/testify/require"
)

func TestImport(t *testing.T) {
dbf := path.Join(t.TempDir(), "data.db")
cmd := newRootCmd()
cmd.SetArgs([]string{"import", "../testdata/import.yaml", "--data", dbf})
require.NoError(t, cmd.Execute())
}
3 changes: 3 additions & 0 deletions cmd/letseat/cmd/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var (
// special = lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"}
titleStyle = lipgloss.NewStyle().Bold(true).Foreground(highlight)
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render
padding = 2
maxWidth = 80

/*
infoStyle = lipgloss.NewStyle().
Expand Down
17 changes: 17 additions & 0 deletions cmd/letseat/testdata/import.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# this is a thing
---
- place: Biggy Wings
date: 2023-12-21
takeout: true
ratings:
andrei: 3
- place: McDonuoughs Pub
date: 2023-12-16
ratings:
andrei: 4
- place: Franks Place
date: 2023-12-14
takeout: true
ratings:
andrei: 4
jeymes: 3
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ go 1.21

require (
github.com/adrg/xdg v0.4.0
github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/huh v0.2.3
github.com/charmbracelet/huh v0.3.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/log v0.3.1
github.com/drewstinnett/go-output-format/v2 v2.1.0
Expand All @@ -25,8 +27,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/bubbles v0.17.1 // indirect
github.com/charmbracelet/bubbletea v0.25.0 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3
github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/charmbracelet/bubbles v0.17.1 h1:0SIyjOnkrsfDo88YvPgAWvZMwXe26TP6drRvmkjyUu4=
github.com/charmbracelet/bubbles v0.17.1/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o=
github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6 h1:6nVCV8pqGaeyxetur3gpX3AAaiyKgzjIoCPV3NXKZBE=
github.com/charmbracelet/bubbles v0.17.2-0.20240108170749-ec883029c8e6/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM273bISc=
github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc=
github.com/charmbracelet/huh v0.2.3 h1:fZaqnd/fiO7jlfcLqhP2iwpLt670IaHQfL/7Qu+fBm0=
github.com/charmbracelet/huh v0.2.3/go.mod h1:XmADLRnJs/Jqw7zIbi9BTss5gXbOkR6feyVoNAp19rA=
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/huh v0.3.0 h1:CxPplWkgW2yUTDDG0Z4S5HH8SJOosWHd4LxCvi0XsKE=
github.com/charmbracelet/huh v0.3.0/go.mod h1:fujUdKX8tC45CCSaRQdw789O6uaCRwx8l2NDyKfC4jA=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMTYw=
Expand Down
13 changes: 9 additions & 4 deletions pkg/diary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@ package letseat

import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"slices"
"sort"
"time"

"github.com/montanaflynn/stats"
bolt "go.etcd.io/bbolt"
"gopkg.in/yaml.v2"
)

// Diary is the thing holding all of your visits and info
type Diary struct {
// Deprecated: Use db instead of unfiltered entries
unfilteredEntries Entries
entries *Entries
filter EntryFilter
fn string
db *bolt.DB
}

Expand All @@ -45,6 +42,12 @@ func (d *Diary) Log(es ...Entry) error {
}
*d.entries = append(*d.entries, e)
}
// Now save the person info
for _, person := range d.entries.PeopleEnhanced() {
if err := tx.Bucket([]byte(PeopleBucket)).Put([]byte(person.Name), []byte("true")); err != nil {
slog.Warn("error logging person", "error", err)
}
}
return nil
}); err != nil {
return err
Expand Down Expand Up @@ -166,6 +169,7 @@ func initDB(db *bolt.DB) error {
return nil
}

/*
// WriteEntries write the entries back to a yaml file
func (d Diary) WriteEntries() error {
if d.fn == "" {
Expand All @@ -186,6 +190,7 @@ func (d Diary) WriteEntries() error {
}
return nil
}
*/

// Entries is multiple DiaryEntry objects
type Entries []Entry
Expand Down
15 changes: 15 additions & 0 deletions pkg/diary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,18 @@ func TestWithDB(t *testing.T) {
require.NotNil(t, got.db)
require.NotNil(t, New(WithDBFilename(path.Join(t.TempDir(), "test-fn.db"))))
}

func TestLogDB(t *testing.T) {
diary := New(WithDB(newTestDB(t)))
require.NotNil(t, diary)
require.NoError(t, diary.Log(
Entry{
Place: "Mamacitas",
Date: toPTR(time.Date(2024, time.January, 15, 0, 0, 0, 0, time.UTC)),
IsTakeout: true, Ratings: map[string]int{
"drew": 5,
"james": 3,
},
},
))
}
13 changes: 12 additions & 1 deletion pkg/person.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package letseat

import "sort"
import (
"encoding/json"
"sort"
)

// Person represents a person who ate and rated something at a restaurant
type Person struct {
Name string
PlaceAvgRatings map[string]float64
}

func (p Person) mustMarshal() []byte {
b, err := json.Marshal(p)
if err != nil {
panic(err)
}
return b
}

// FavoriteN returns the persons N favorite restaurants
func (p *Person) FavoriteN(n int) []string {
type kv struct {
Expand Down

0 comments on commit 9fb3589

Please sign in to comment.