Skip to content

Commit

Permalink
feat: add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jul 19, 2022
1 parent c52bd7d commit e3137af
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
5 changes: 5 additions & 0 deletions entry/entry_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func FormattedOutputFromEntries(Entries []Entry) []byte {
return []byte(output)
}

// FormatEntry return the entry details as a formatted string
func FormatEntry(entry Entry) string {
return fmt.Sprintf("ID: %d\nCreated: %s\nMessage:\n\n %s\n %s\n", entry.ID, entry.CreatedAt.Format("2006-01-02"), entry.Message, divider)
}

// OutputEntriesToMarkdown create an output file that contains the given entries in a formatted string
func OutputEntriesToMarkdown(entries []Entry) error {
file, err := os.OpenFile("./output.md", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3
github.com/charmbracelet/bubbles v0.10.3 h1:fKarbRaObLn/DCsZO4Y3vKCwRUzynQD9L+gGev1E/ho=
github.com/charmbracelet/bubbles v0.10.3/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/bubbletea v0.20.1-0.20220412151435-14e58aa1f92f h1:/6cBUPF3UlMueuKtQay4IepufjiMfwHwLyVfU9UDFWo=
github.com/charmbracelet/bubbletea v0.20.1-0.20220412151435-14e58aa1f92f/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.21.0 h1:f3y+kanzgev5PA916qxmDybSHU3N804uOnKnhRPXTcI=
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/glamour v0.5.0 h1:wu15ykPdB7X6chxugG/NNfDUbyyrCLV9XBalj5wdu3g=
Expand Down Expand Up @@ -110,7 +108,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gorm.io/driver/sqlite v1.3.2 h1:nWTy4cE52K6nnMhv23wLmur9Y3qWbZvOBz+V4PrGAxg=
gorm.io/driver/sqlite v1.3.2/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U=
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0=
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
41 changes: 31 additions & 10 deletions tui/entryui/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package entryui

import (
"log"

"github.com/bashbunni/project-management/entry"
"github.com/bashbunni/project-management/tui/constants"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
Expand All @@ -12,7 +15,6 @@ import (

var cmd tea.Cmd

// TODO: clean up your project PLEASE
// BackMsg change state back to project view
type BackMsg bool

Expand All @@ -25,6 +27,8 @@ type Model struct {
error string
alerts string
windowSize tea.WindowSizeMsg
paginator paginator.Model
entries []entry.Entry
}

// Init run any intial IO on program start
Expand All @@ -38,20 +42,32 @@ func New(er *entry.GormRepository, activeProjectID uint, p *tea.Program, windowS
m.p = p
m.viewport = viewport.New(windowSize.Width, calculateHeight(windowSize.Height))
m.viewport.Style = lipgloss.NewStyle().
BorderStyle(lipgloss.DoubleBorder()).
BorderForeground(lipgloss.Color("62")).
Align(lipgloss.Bottom)

// init paginator
m.paginator = paginator.New()
m.paginator.Type = paginator.Dots
m.paginator.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
m.paginator.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")

// get entries
var err error
if m.entries, err = er.GetEntriesByProjectID(m.activeProjectID); err != nil {
log.Fatalf("failed to get entries: %v", err)
}
m.paginator.SetTotalPages(len(m.entries))

// set content
m.setViewportContent()
return &m
}

func (m *Model) setViewportContent() {
content, err := getEntryMessagesByProjectIDAsSingleString(m.activeProjectID, m.er)
if content == "" {
var content string
if len(m.entries) == 0 {
content = "There are no entries for this project :)"
}
if err != nil {
m.error = "cannot get entry messages as single string"
} else {
content = entry.FormatEntry(m.entries[m.paginator.Page])
}
str, err := glamour.Render(content, "dark")
if err != nil {
Expand Down Expand Up @@ -88,9 +104,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case msg.String() == "q":
return m, tea.Quit
default:
m.viewport, cmd = m.viewport.Update(msg)
// m.viewport, cmd = m.viewport.Update(msg)
// cmds = append(cmds, cmd)
}
}
m.paginator, cmd = m.paginator.Update(msg)
// TODO: bug: not going to next page despite still having lots of pages left
return m, cmd
}

Expand All @@ -104,12 +123,14 @@ func (m Model) errorView() string {

// View return the text UI to be output to the terminal
func (m Model) View() string {
formatted := lipgloss.JoinVertical(lipgloss.Left, m.viewport.View(), m.helpView(), m.errorView())
m.setViewportContent()
formatted := lipgloss.JoinVertical(lipgloss.Left, m.viewport.View(), m.helpView(), m.errorView(), m.paginator.View())
return constants.DocStyle.Render(formatted)
}

/* helpers */

// TODO: delete this
func getEntryMessagesByProjectIDAsSingleString(id uint, er *entry.GormRepository) (string, error) {
entries, err := er.GetEntriesByProjectID(id)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions tui/projectui/commands.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package projectui

import (
"log"

"github.com/bashbunni/project-management/project"
tea "github.com/charmbracelet/bubbletea"
)
Expand Down Expand Up @@ -42,7 +40,6 @@ func deleteProjectCmd(id uint, pr *project.GormRepository) tea.Cmd {

func selectProjectCmd(ActiveProjectID uint) tea.Cmd {
return func() tea.Msg {
log.Println("in selectProjectCmd: ", ActiveProjectID)
return SelectMsg{ActiveProjectID: ActiveProjectID}
}
}
7 changes: 2 additions & 5 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type MainModel struct {

// StartTea the entry point for the UI. Initializes the model.
func StartTea(pr project.GormRepository, er entry.GormRepository) {
// if os.Getenv("HELP_DEBUG") != "" {
if f, err := tea.LogToFile("debug.log", "help"); err != nil {
fmt.Println("Couldn't open a file for logging:", err)
os.Exit(1)
Expand All @@ -46,7 +45,6 @@ func StartTea(pr project.GormRepository, er entry.GormRepository) {
}
}()
}
// }

m := New(&pr, &er)
p = tea.NewProgram(m)
Expand Down Expand Up @@ -83,6 +81,7 @@ func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.state = projectView
case projectui.SelectMsg:
m.activeProjectID = msg.ActiveProjectID
m.entry = entryui.New(m.er, m.activeProjectID, p, m.windowSize)
m.state = entryView
}

Expand All @@ -96,14 +95,12 @@ func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.project = projectModel
cmd = newCmd
case entryView:
m.entry = entryui.New(m.er, m.activeProjectID, p, m.windowSize)
newEntry, newCmd := m.entry.Update(msg)
entryModel, ok := newEntry.(entryui.Model)
if !ok {
panic("could not perform assertion on entryui model")
}
m.entry = entryModel
cmd = newCmd
return entryModel, newCmd
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
Expand Down

0 comments on commit e3137af

Please sign in to comment.