Skip to content

Commit

Permalink
feat: add UI element for periodic arguments (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantac authored Apr 28, 2024
1 parent 72f188f commit 1790091
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 53 deletions.
2 changes: 0 additions & 2 deletions accounting/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func NewFilter(filterType FilterType, value interface{}) Filter {
return NewAccountFilter(value.(string))
case FilterTypeDate:
return NewDateFilter()
case FilterTypePeriod:
return NewPeriodFilter()
case FilterTypeAccountDepth:
return NewAccountDepthFilter()
case FilterTypeStartDate:
Expand Down
40 changes: 0 additions & 40 deletions accounting/filter_period.go

This file was deleted.

7 changes: 6 additions & 1 deletion ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ type keyMap struct {
ResetFilters key.Binding
AcctDepthDecr key.Binding
AcctDepthIncr key.Binding
Yearly key.Binding
Monthly key.Binding
Quarterly key.Binding
Yearly key.Binding
}

var allKeys = keyMap{
Monthly: key.NewBinding(
key.WithKeys("m", "m"),
key.WithHelp("m", "monthly period"),
),
Quarterly: key.NewBinding(
key.WithKeys("u", "u"),
key.WithHelp("u", "quarterly period"),
),
Yearly: key.NewBinding(
key.WithKeys("y", "y"),
key.WithHelp("y", "yearly period"),
Expand Down
21 changes: 11 additions & 10 deletions ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type model struct {
isFormDisplay bool
filterGroup *filterGroup
spinner spinner.Model
period *Period

searchFilter accounting.Filter
periodFilter accounting.Filter
acctDepth accounting.AccountDepthFilter
isTxnsSortedByMostRecent bool

Expand All @@ -53,8 +53,8 @@ func newModel(hlcmd accounting.HledgerCmd) *model {
quitting: false,
isFormDisplay: false,
filterGroup: newFilterGroup(),
period: newPeriod(),
searchFilter: accounting.NoFilter{},
periodFilter: accounting.NewPeriodFilter().Yearly(),
acctDepth: accounting.NewAccountDepthFilter(),
isTxnsSortedByMostRecent: true,
width: 0,
Expand Down Expand Up @@ -121,12 +121,14 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.help.keys.Search):
form := newFilterForm(m, searchFilter)
return form.Update(nil)
case key.Matches(msg, m.help.keys.Yearly):
m.periodFilter = accounting.NewPeriodFilter().Yearly()
return m, m.refresh()
case key.Matches(msg, m.help.keys.Monthly):
m.periodFilter = accounting.NewPeriodFilter().Monthly()

case key.Matches(msg, m.help.keys.Yearly),
key.Matches(msg, m.help.keys.Monthly),
key.Matches(msg, m.help.keys.Quarterly):
p, _ := m.period.Update(msg)
m.period = p.(*Period)
return m, m.refresh()

case key.Matches(msg, m.help.keys.ResetFilters):
m.resetFilters()
return m, m.refresh()
Expand Down Expand Up @@ -229,6 +231,7 @@ func (m *model) View() string {
lipgloss.Right,
m.tabs.View(),
m.filterGroup.View(),
m.period.View(),
),
activeItemStyle.Render(v),
),
Expand Down Expand Up @@ -259,8 +262,6 @@ func (m *model) updateAllModels(msg tea.Msg) {
func (m *model) refresh() tea.Cmd {
m.msgError = nil // reset the msgError

pf := m.periodFilter.(accounting.PeriodFilter)

registerOpts := hledger.NewOptions().
WithAccount(m.filterGroup.account.Value()).
WithStartDate(m.filterGroup.startDate.Value()).
Expand All @@ -271,7 +272,7 @@ func (m *model) refresh() tea.Cmd {
WithStartDate(m.filterGroup.startDate.Value()).
WithEndDate(m.filterGroup.endDate.Value()).
WithAccountDepth(m.acctDepth.RawValue()).
WithPeriod(hledger.PeriodType(pf.RawValue()))
WithPeriod(hledger.PeriodType(m.period.periodType))

optsPretty := opts.WithPretty().WithLayout(hledger.LayoutBare).WithAccountDrop(1)

Expand Down
82 changes: 82 additions & 0 deletions ui/period.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ui

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/siddhantac/hledger"
)

type periodType string

const (
monthly periodType = "monthly"
quarter periodType = "quarterly"
yearly periodType = "yearly"
)

type Period struct {
periodType hledger.PeriodType
}

func newPeriod() *Period {
return &Period{periodType: hledger.PeriodYearly}
}

func (p *Period) Init() tea.Cmd { return nil }

func (p *Period) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "m":
p.periodType = hledger.PeriodMonthly
case "u":
p.periodType = hledger.PeriodQuarterly
case "y":
p.periodType = hledger.PeriodYearly
case "q", "esc":
return p, tea.Quit
}
}
return p, nil
}

func (p *Period) View() string {
sectionTitleStyle := lipgloss.NewStyle().
MarginTop(1).
MarginRight(1).
PaddingRight(1).
PaddingLeft(1).
Foreground(theme.Accent)
sectionTitle := sectionTitleStyle.Render("PERIOD")

inactiveTextStyle := lipgloss.NewStyle().
Foreground(theme.PrimaryForeground).
MarginRight(2)
textStyle := lipgloss.NewStyle().
MarginRight(2)

var monthView, quarterView, yearView string
switch p.periodType {
case hledger.PeriodMonthly:
monthView = textStyle.Render("monthly")
quarterView = inactiveTextStyle.Render("quarterly")
yearView = inactiveTextStyle.Render("yearly")
case hledger.PeriodQuarterly:
monthView = inactiveTextStyle.Render("monthly")
quarterView = textStyle.Render("quarterly")
yearView = inactiveTextStyle.Render("yearly")
case hledger.PeriodYearly:
monthView = inactiveTextStyle.Render("monthly")
quarterView = inactiveTextStyle.Render("quarterly")
yearView = textStyle.Render("yearly")
}

return lipgloss.JoinVertical(
lipgloss.Right,
sectionTitle,
monthView,
quarterView,
yearView,
)
}

0 comments on commit 1790091

Please sign in to comment.